8.13总结


package com.zhongruan.util;
import java.sql.*;
public class DBUtil {
public static Connection getConnection(){
Connection connection=null;
//1.加载驱动
try {
Class.forName(“com.mysql.jdbc.Driver”);
//2.创建连接
connection = DriverManager.getConnection
(“jdbc:mysql://127.0.0.1:3306/ningda?useSSL=true&characterEncoding=utf-8&user=root&password=root”);
System.out.println(“创建连接成功”);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(ResultSet rs, PreparedStatement preparedStatement,Connection connection){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

二.
package com.zhongruan.jdbc;
import com.zhongruan.bean.Student;
import com.zhongruan.util.DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class TestJdb {
public static void main(String[] args) throws Exception {
Connection connection = DBUtil.getConnection();
List students=new ArrayList<>();
//3.写sql语句
String sql=“select * from student”;
//4.得到statement对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//5.执行sql得到结果集
ResultSet resultSet = preparedStatement.executeQuery();
//6.处理结果集
while (resultSet.next()){
Student student=new Student();
student.setId(resultSet.getInt(1));
student.setName(resultSet.getString(2));
student.setPassword(resultSet.getString(3));
students.add(student);
}
System.out.println(students);
//7.关闭资源
DBUtil.closeAll(resultSet,preparedStatement,connection);
}
}

三.
package com.zhongruan.jdbc;
import com.zhongruan.util.DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestDelete {
public static void main(String[] args) {
Connection connection=null;
PreparedStatement preparedStatement=null;
DBUtil dbUtil=null;
//1.加载驱动
try {
dbUtil=new DBUtil();
connection = dbUtil.getConnection();
//3.写sql
String sql=“delete from student where id=?”;
//4.获得statement对象
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,2);
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
dbUtil.closeAll(null,preparedStatement,connection);
}
}

四.
package com.zhongruan.util;
import java.sql.connection;
import java.aql.drivermanager;

public class DBUtil{
public Connection getConnection(){
Connection connection=null;
//1.加载驱动
try{
class.forname(“com.mysql.jdbc.driver”);
//2.创建链接
Connection=drivermanager.getConnection
(url: (“jdbc:mysql://127.0.0.1:3306/ningda?useSSL=true&characterEncoding=utf-8&user=root&password=root”)
system.out.println(“创建连接成功”);
}catch(Exception e){
e.printstacktrace();
}
return connection;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值