网上有把步骤分为七步的,其实就是把准备数据库账号密码地址又分为了一个步骤
六步
注册驱动
告诉数据库,即将来凝结哪个品牌的数据库
获取连接
表示jvm的进程和数据库进程之间的通道打开了。
获取数据库操作对象、
专门执行sql语句的对象
执行sql语句
DQL和DML
处理查询结果集
只有当第四步执行的时select语句的时候,才有第五步查询结果集。
释放资源
java和数据库之间属于进程间的通信,开启后一定要关闭。
注意:只有查询操作才有第五步。因为查询外的操作只是返回"影响的行数",只需要用int类型变量接收就可以了。而查询操作返回的是一个ResultSet结果集,需要额外处理。
查询操作
先提前获取所需对象
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
1.注册驱动
// 1. 注册驱动
Driver driver = new Driver();
DriverManager.registerDriver(driver);
//合写成一句
DriverManager.registerDriver(new Driver());
2.获取数据库连接
//先获取数据库url和账号密码
String url = "jdbc:mysql://127.0.0.1:3306/userdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong&allowPublicKeyRetrieval=true";
String user = "root";
String password = "123456";
//注册驱动
conn = DriverManager.getConnection(url,user,password);
3.获取数据库操作对象
Statement stmt = conn.createStatement();
4.执行sql语句。该语句为查询操作,执行完后需接收结果
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
5.处理结果集
while (rs.next()){
String value = rs.getString("userId")+", "+rs.getString("userName");
System.out.println(value);
}
6.释放数据库连接。查询操作有三个对象,需要分开关闭,并且处理异常。
if (rs != null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
查询完成
增删改
第四步:执行sql增删改语句的返回值是”影响的行数“,int类型变量接收即可。if接收到的变量是否为一就可以知道是否执行成功。
int count = statement.executeUpdate(sql);
System.out.println(count==1?"成功":"失败");
全部六步完整代码
public class Query {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//注册驱动
DriverManager.registerDriver(new Driver());
//获取数据库连接
String url = "jdbc:mysql://127.0.0.1:3306/userdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong&allowPublicKeyRetrieval=true";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
//获取数据库操作对象
stmt = conn.createStatement();
//执行sql语句
String sql = "select * from userinfo";
rs = stmt.executeQuery(sql);
//处理结果集
while (rs.next()){
String value = rs.getString("userId")+", "+rs.getString("userName");
System.out.println(value);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
//关闭连接
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
五步完整代码
public class Insert {
public static void main(String[] args) {
Connection conn = null;
Statement statement = null;
try {
// 1. 注册驱动
DriverManager.registerDriver(new Driver());
// 2. 获取连接
String url = "jdbc:mysql://127.0.0.1:3306/userdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong&allowPublicKeyRetrieval=true";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url,user,password);
// 3. 获取数据库操作对象
statement = conn.createStatement();
// 4. 执行sql语句
String sql = "insert into userinfo(userId, userName, gender, roleId, userPassword) values('user10', 'achong', 1,1,'123123')";
int count = statement.executeUpdate(sql);
System.out.println(count);
System.out.println(count==1?"成功":"失败");
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
// 6. 释放资源
if (statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}