jdbc连接数据库共有七个步骤:
1、加载驱动
在连接数据库之前,首先要加载驱动连接到Java虚拟机,即实现java.sql.driver接口的一个类,本次是加载MySQL数据库驱动,代码如下:
Class.forName("com.mysql.jdbc.Driver");
2、创建连接
在加载驱动之后,即可创建连接到所选的数据库,向java.sql.DriverManager请求获得connection对象,使用DriverManager的getConnection()的方法传入要连接的数据库,代码如下:
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&" + "characterEncoding=utf-8&user=root&password=root");
其中url路径和登入数据库用户名、密码可根据实际情况自行更改
3、写SQL语句
创建一个SQL语句,String sql=" ";写入相应的查询、插入、删除或修改指令。代码如下:
查询(我所建立的表格名为users):
String sql = "select * from users";
插入:
String sql="Insert into users(username,userpassword,userID) values(?,?,?)";
删除:
String sql="delete from users where username='小明'";
修改:
String sql="update users set username='小吴'where userID='1'";
4、得到statement对象,生成容器
要执行SQL语句获得java.sql.Statement实例,此处执行的是动态SQL语句,所以要用PreparedStatement来实现,相关代码如下:
PreparedStatement statement=connection.prepareStatement(sql);
5、执行SQL语句,得到结果集。
Statement接口提供了三种执行SQL语句的方法,分别为:executeQuery, executeUpdate和execute。
1、statement.executeQuery()常用于查询数据库,并将结果返回给结果集ResultSet。
2、 statement.executeUpdate()常用于更新数据库,如执行插入、删除、修改等指令时便会用到这一方法。
3、execute()用于执行返回多个结果集、多个计数或两者相结合的语句。
6、处理结果集
将ResultSet对象进行返回,如在本次查询操作中相关代码如下:
while(resultSet.next()){
System.out.print(resultSet.getString(1));
System.out.print(resultSet.getString(2));
System.out.print(resultSet.getInt(3));
}
7、关闭连接
操作完成后要将ResultSet、 PreparedStatement、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();
}
}
全部完整代码如下:
这是我所在user数据库内所创建的表users:
1、创建一个名为DBUtil的类,将数据库的驱动和连接开启和关闭等代码整合到其中(否则之后每次执行相关的SQL语句都要写驱动和连接数据库的代码会很麻烦):
package com;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil {
public static Connection getConnection() {
Connection connection = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
System.out.print("Driver successful\n");
//2.创建连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&" + "characterEncoding=utf-8&user=root&password=root");
System.out.print("connection successful!\n");
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 (Exception e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2、check类,实现数据库相关信息的查询:
package com;
import com.mysql.jdbc.ConnectionImpl;
import java.sql.*;
public class check {
private static ConnectionImpl connection;
public static void main(String[] args) {
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
try {
connection= DBUtil.getConnection();
String sql = "select * from users";
statement=connection.prepareStatement(sql);
resultSet=statement.executeQuery();
while(resultSet.next()){
System.out.print(resultSet.getString(1));
System.out.print(resultSet.getString(2));
System.out.print(resultSet.getInt(3));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBUtil.close(resultSet,statement,connection);
}
}
}
3、insert类,往数据库插入信息:
package com;
import com.mysql.jdbc.ConnectionImpl;
import java.sql.*;
public class insert {
private static ConnectionImpl connection;
public static void main(String[] args) {
Connection connection=null;
PreparedStatement statement=null;
try {
connection=DBUtil.getConnection();
String sql="Insert into users(username,userpassword,userID) values(?,?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,"小明");
statement.setString(2,"xiaoming");
statement.setInt(3,3);
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtil.close(null,statement,connection);
}
}
}
4、delete类,删除数据库中的相关信息:
package com;
import java.sql.*;
public class delete {
public static void main(String[] args) {
Connection connection=null;
PreparedStatement statement=null;
try {
connection=DBUtil.getConnection();
String sql="delete from users where username='小明'";
statement=connection.prepareStatement(sql);
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.close(null,statement,connection);
}
}
}
5、Update类,实现数据库信息的更新:
package com;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class Update {
public static void main(String[] args){
Connection connection=null;
PreparedStatement statement=null;
try{
connection=DBUtil.getConnection();
String sql="update users set username='小吴'where userID='1'";
statement=connection.prepareStatement(sql);
statement.executeUpdate();
}catch (Exception e)
{
e.printStackTrace();
}finally {
DBUtil.close(null,statement,connection);
}
}
}