版权属于:
Hello World
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
网上有把步骤分为七步的,其实就是把准备数据库账号密码地址又分为了一个步骤
注册驱动
获取连接
获取数据库操作对象、
执行sql语句
处理查询结果集
释放资源
注意:只有查询操作才有第五步。因为查询外的操作只是返回"影响的行数",只需要用int类型变量接收就可以了。而查询操作返回的是一个ResultSet结果集,需要额外处理。
先提前获取所需对象
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
注册驱动
// 1. 注册驱动
Driver driver = new Driver();
DriverManager.registerDriver(driver);
//合写成一句
DriverManager.registerDriver(new Driver());
获取数据库连接
//先获取数据库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);
获取数据库操作对象
Statement stmt = conn.createStatement();
执行sql语句。该语句为查询操作,执行完后需接收结果
String sql = "select * from userinfo";
ResultSet rs = stmt.executeQuery(sql);
处理结果集
while (rs.next()){
String value = rs.getString("userId")+", "+rs.getString("userName");
System.out.println(value);
}
释放数据库连接。查询操作有三个对象,需要分开关闭,并且处理异常。
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();
}
}
}
}
}
评论 (0)