🏹 封装版附送在文末哦~~ 🏹
目录
4. 执行SQL语句 executeQuery/Update
0. 导入jar包
导入详细方法见:JDBC编程(Java操作数据库 MySQL)
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.*;
import javax.sql.DataSource;
import java.util.Scanner;
1. 设置数据源 DataSource
// 设置数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("1230");
2. 建立连接 Connection
// 建立连接
Connection connection = dataSource.getConnection();
3. 构造SQL语句 PreparedStatement
(以Update为例)
// 构造SQL语句
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的id:");
int id = scanner.nextInt();
System.out.println("请输入要修改的名字:");
String name = scanner.next();
// 根据功能需要设置sql语句
String sql = "update student set name = ? where id = ?";
// 使用 PreparedStatement 构造语句
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,id);
(以Select为例)
// 构造SQL语句
String sql = "select * from student";
// 使用 PreparedStatement 构造语句
PreparedStatement statement = connection.prepareStatement(sql);
4. 执行SQL语句 executeQuery/Update
executeQuery返回类型是数据表,executeUpdate返回一个整数,表示影响到的行数
(以Update为例)
// 执行SQL语句
int ret = statement.executeUpdate();
System.out.println("ret: "+ ret);
(以Select为例)ResultSet结果集合
// 执行SQL语句
ResultSet resultSet = statement.executeQuery();
// 遍历结果集合,结果集合非常类似于迭代器
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("id: "+id+",name: "+name);
}
5. 释放资源 .close();
// 释放资源
// Select操作需要释放结果集合resulSet
// resultSet.close();
statement.close();
connection.close();
🏹封装版!!(可直接复制使用~)
类名:DBUtil.java
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
// 通过 DBUtil.java 这个类,把数据库的连接操作封装起来
public class DBUtil {
private static String url = "jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false"; //默认IP、端口地址
private static String username = "root";
private static String password = "1230"; //数据库密码
// 创建 DataSource 实例
private static DataSource dataSource = new MysqlDataSource();
// 静态代码块,执行时机:类加载阶段
static {
((MysqlDataSource)dataSource).setURL(url);
((MysqlDataSource)dataSource).setUser(username);
((MysqlDataSource)dataSource).setPassword(password);
}
// 建立连接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
// 释放资源
public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}