目录
前言:
JDBC指的是Java DataBase Connectivity。本文在idea环境中实现。
JDBC操作有7个步骤:
1、加载驱动
2、创建连接
3、写sql
4、得到statement对象
5、执行sql
6、处理对结果集
7、关闭资源
1、数据库:
我们需要对一个表中的数据进行操作。在数据库中创建一个userinfo表,有id、username、password 3个列,其数据如下图所示。
2、jar包
我们需要添加jar包来通过编写代码连接数据库。这里选择mysql-connector-java-5.1.45-bin.jar。在项目中新建一个lib文件夹,把jar包复制进去。
右键点击jar包,点击Add to libary,将其添加到项目中
3、JDBC基本操作
1、连接数据库
我们新建一个名为DBUtil的工具类,该类用于实现与数据库的连接,和结束与数据库的连接。
(1)在与数据库连接的过程中,首先要加载mysql驱动,然后创建连接,最后返回这个连接供其他方法使用。
(2)结束与数据库的连接需要传递ResultSet,PreparedStatement 和Connection 类型的变量。如果这些变量都不为空,则调用其close()方法,关闭它。
public class DBUtil {
public static Connection getConnection()
{
try {
//1、加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2、创建连接
Connection connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc_test?useSSL=true&characterEncoding=utf-8" +
"&user=root&password=12345678");
return connection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void close(ResultSet resultSet, PreparedStatement statement,Connection connection)
{
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();
}
}
}
}
2、增删查改操作步骤
(1)根据操作写sql语句
(2)调用connection.prepareStatement()方法来获取对应地PreraredStatement对象
(3)获取结果集需调用statement的statement.executeQuery()来执行sql语句来执行查询操作;若执行增删改操作,则要调用statement.executeUpdate()方法
(4)对结果集进行处理
(5)关闭资源
2.1、select操作(查询操作)
此处是查询user表中所有的数据
public void select()
{
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
try {
connection= DBUtil.getConnection();
System.out.println("创建连接成功");
//写sql
String sql="select * from userinfo";
//得到statement对象
statement=connection.prepareStatement(sql);
//执行sql,得到结果集
resultSet=statement.executeQuery();
//处理结果集
while(resultSet.next())
{
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.close(resultSet,statement,connection);
}
}
运行结果:
2.3、insert操作(添加操作)
此处添加的数据为(王五,22313)
public void insert()
{
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
try {
connection= DBUtil.getConnection();
System.out.println("创建连接成功");
//3、写sql
String sql="insert into userinfo(username,password) values (?,?)";
//4、得到statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,"王五");
statement.setString(2,"22313");
//5、执行sql
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
//7、关闭资源
DBUtil.close(null,statement,connection);
}
}
运行结果:
2.4、update操作(修改操作)
此处是根据输入的用户名,来修改其密码
public void update()
{
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
String name,password;
Scanner scan=new Scanner(System.in);
System.out.println("请输入要修改的用户的用户名:");
name=scan.next();
System.out.println("请输入要修改的密码:");
password=scan.next();
try {
connection = DBUtil.getConnection();
//3、写sql
String sql="update userinfo set password=? where username=?";
statement=connection.prepareStatement(sql);
statement.setString(1,password);
statement.setString(2,name);
statement.executeUpdate();
System.out.println("修改成功");
}catch(Exception e){
e.printStackTrace();
}finally {
DBUtil.close(null,statement,connection);
}
}
运行结果:
2.5、delete操作(删除操作)
此处是根据输入的用户名,来删除其数据
public void delete()
{
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
String name;
Scanner scan=new Scanner(System.in);
System.out.println("请输入要删除的用户的用户名:");
name=scan.next();
try {
connection = DBUtil.getConnection();
String sql="delete from userinfo where username=?";
statement=connection.prepareStatement(sql);
statement.setString(1,name);
statement.executeUpdate();
System.out.println("删除成功");
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.close(null,statement,connection);
}
}
运行结果: