当前位置:网站首页>MySQL——JDBC
MySQL——JDBC
2022-08-09 21:54:00 【Xiaolock830】
我们可以利用官方为java编写的数据库驱动包,利用idea来操作MySQL数据库
概念
其全称是java database connectivity,也就是提供了一系列接口,使java程序和Mysql连接,让sql语句在java中执行
准备
我们需要去下面这个网址下载数据库驱动包
https://mvnrepository.com/artifact/mysql/mysql-connector-java
选择的版本号需要和我们的Mysql的版本号相同,例如8.0的就选择8系列的
然后将这个jar包拷贝,创建一个新的项目,和一个文件夹
然后右键点击项目名称,点击Open Module Settings
然后点击Dependencies
然后点击加号,导入我们的lib文件夹的jar包
这样我们就配置好我们的项目了,接下来就可以编写代码了
代码
通过一下代码,可以连接到我们的数据库
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class test {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
}
}
前面的代码只要使用的java项目和自己的MySQL在同一台机器上,并且没有改过MySQL的默认属性,就是一样的代码,唯一不一样的是password是我们MySQL的密码,替换即可
插入操作
public class TestJDBC {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
String sql = "insert student values(1,'张三')";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
int row = preparedStatement.executeUpdate();
System.out.println(row);
preparedStatement.close();
connection.close();
}
}
需要注意的是,用完资源后,应该断开和数据库的连接
查询操作
public class TestJDBCSelect {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSl=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
String sql = "select * from student";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("id = " + id + " name = "+ name);
}
resultSet.close();
preparedStatement.close();
connection.close();
}
}
和插入操作不一样,更新操作是用这条语句来使操作生效
preparedStatement.executeQuery();
删除操作
public class TestJDBCDelete {
public static void main(String[] args) throws SQLException {
Scanner scanner = new Scanner(System.in);
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
System.out.println("input the id which you want to delete");
int id = scanner.nextInt();
String sql = "delete from student where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
int ret = statement.executeUpdate();
System.out.println("ret = " + ret);
statement.close();
connection.close();
}
}
边栏推荐
- Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN
- AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
- 好未来,想成为第二个新东方
- How to Make Your Company Content GDPR Compliant
- Activiti7审批流
- 一本通2074:【21CSPJ普及组】分糖果(candy)
- json case
- README_Albumentations
- Tensorflow模型整体构建流程
- FileZilla搭建FTP服务器图解教程
猜你喜欢
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
POWER SOURCE ETA ETA Power Repair FHG24SX-U Overview
Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
Bean生命周期
台风生成,广州公交站场积极开展台风防御安全隐患排查
肝通宵写了三万字把SQL数据库的所有命令,函数,运算符讲得明明白白讲解,内容实在丰富,建议收藏+三连好评!
Synchronization lock synchronized traces the source
js十五道面试题(含答案)
金山云地震,震源在字节?
Install win virtual machine on VMware
随机推荐
简单问题窥见数学
Bean生命周期
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
电脑系统重装后怎么用打印机扫描出文件?
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
一文让你快速了解隐式类型转换【整型提升】!
MLOps的演进历程
发送激活邮件「建议收藏」
unit test
Blender程序化建模简明教程【PCG】
TF中使用zeros(),ones(), fill()方法生成数据
Easyui 表单验证「建议收藏」
Space not freed after TRUNCATE table
Flask入门学习教程
PHP 2D array sorted by a field
Tensorflow中使用convert_to_tensor去指定数据的类型
In programming languages, the difference between remainder and modulo
UML类图五种关系的代码实现[通俗易懂]
AI识万物:从0搭建和部署手语识别系统
JSON 基本使用