1. 导入jar包
一般在安装mysql时在mysql的文件夹下会有对应的数据库驱动jar包,可以自己找一下,然后手动导入jar包
或者使用maven导入jar包的依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
需要什么jar包以后可以在这个网址中自行搜索 https://mvnrepository.com
2. 创建JDBC工具类
import java.sql.Connection;
import java.sql.DriverManager;
public class JDBCUtil {
// Driver驱动类名
private static final String DRIVER="com.mysql.jdbc.Driver";
// jdbc:数据库类型//ip地址:端口号/数据库名称
private static final String URL="jdbc:mysql://localhost:3306/test";
// 数据库登录的用户名
private static final String USERNAME="root";
// 用户密码
private static final String PASSWORD="root";
// 静态代码块
static{
try {
// 加载驱动
Class.forName(DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取JDBC连接
* @return 数据库连接对象
*/
public static final Connection getConnection() throws SQLException {
// 获取与数据库的连接
return DriverManager.getConnection(URL,USERNAME,PASSWORD);
}
}
3. JDBC简单使用
以下例子使用Junit进行测试,仅作为学习参考时使用,里面有大量重复代码,可以自行的提出
3.1 增
@Test
private void insert() {
// 获取数据库连接
Connection connection = JDBCUtil.getConnection();
PreparedStatement preparedStatement = null;
// 需要执行的sql语句
String sql = "insert into user(id,name,sex) values(?,?,?)";
try {
preparedStatement = connection.prepareStatement(sql);
// 设置需要向数据库表添加的数据,添加的类型需要跟数据库字段的类型相一致
// 第一个参数是sql语句中字段对应的位置也就是对应第几个问号
// 第二个参数根据实际情况填入数值
preparedStatement.setInt(1, id);
preparedStatement.setString(2, name);
preparedStatement.setString(3, sex);
// 返回值为int型,是sql语句执行时影响数据库中数据更改的条数
int i = preparedStatement.executeUpdate();
System.out.println(i);
// 关闭资源时需要从里往外依次关闭
if(preparedStatement != null) {
preparedStatement.close();
}
if(connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
3.2 删
@Test
private void delete() {
// 获取数据库连接
Connection connection = JDBCUtil.getConnection();
PreparedStatement preparedStatement = null;
// 需要执行的sql语句
String sql = "delete from user where id = ?";
try {
preparedStatement = connection.prepareStatement(sql);
// 设置需要向数据库表添加的数据,添加的类型需要跟数据库字段的类型相一致
// 第一个参数是sql语句中字段对应的位置也就是对应第几个问号
// 第二个参数根据实际情况填入数值
preparedStatement.setInt(1, id);
int i = preparedStatement.executeUpdate();
System.out.println(i);
if(preparedStatement != null) {
preparedStatement.close();
}
if(connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
3.3 改
@Test
private void update() {
// 获取数据库连接
Connection connection = JDBCUtil.getConnection();
PreparedStatement preparedStatement = null;
// 需要执行的sql语句
String sql = "update user set name = ?,sex = ? where id = ?";
try {
preparedStatement = connection.prepareStatement(sql);
// 设置需要向数据库表添加的数据,添加的类型需要跟数据库字段的类型相一致
// 第一个参数是sql语句中字段对应的位置也就是对应第几个问号
// 第二个参数根据实际情况填入数值
preparedStatement.setString(1, name);
preparedStatement.setString(2, sex);
preparedStatement.setInt(3, id);
int i = preparedStatement.executeUpdate();
System.out.println(i);
if(preparedStatement != null) {
preparedStatement.close();
}
if(connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
3.4 查
@Test
private void query() {
// 获取数据库连接
Connection connection = JDBCUtil.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
// 需要执行的sql语句
String sql = "select * from user";
try {
preparedStatement = connection.prepareStatement(sql);
// 返回的结果为结果集
resultSet = preparedStatement.executeQuery();
int id;
String name = null,sex = null;
while(resultSet.next()) {
// resultSet通过数据库字段名从数据库中获取数据
// 获取的数据类型应与数据库字段类型保持一致
id = resultSet.getInt("id");
name = resultSet.getString("name");
sex = resultSet.getString("sex");
System.out.println("id:"+id+"name:"+name+"sex:"+sex);
}
if(resultSet != null) {
resultSet.close();
}
if(preparedStatement != null) {
preparedStatement.close();
}
if(connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}