DBUtil

抽取过程

1.写出完整版的JDBC(CRUD)
2.抽取四要素、加载驱动、创建数据库连接、关闭连接、改动操作(曾、删、改)、设置参数、查询操作
3.把上面所以得方法放在DBUtile类中
4.简化JDBC(CRUD)
5.创建Properties配置文件,在DBUtile读取配置文件信息,赋值给四要素

DBUtil.java

public class DBUtil {
 <font color=red size=4>  //连库四要素</font>
private static String driverName;
private static String url;
private static String username;
private static String password;
static{
try {

  <font color=red size=4>  //创建Properties对象</font>
   Properties prop=new Properties();
  
   <font color=red size=4> //创建输入流对象,指向配置文件</font>
   InputStream is= new FileInputStream("config/db.properties");
	<font color=red size=4>//加载</font>
	prop.load(is);
	<font color=red size=4>//获取文件中的数据</font>
	driverName=prop.getProperty("driverName");
	url =prop.getProperty("url");
	username=prop.getProperty("username");
	password=prop.getProperty("password");
	} catch (IOException e) {
	e.printStackTrace();
	 }
}
<font color=red size=4>//加载驱动</font>
private static void driverLoader(){
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
<font color=red size=4>//创建连接对象</font>
public static Connection getConnection(){
try {
driverLoader();
<font color=red size=4>//创建连接对象</font>
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
<font color=red size=4>//关闭连接对象</font>
public static void closeAll(Connection conn){
if(conn==null) return;
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
<font color=red size=4>//执行改动操作</font>
public static void executeUpdate(Connection conn,String sql,Object... args){
if(conn==null)return;

<font color=red size=4>// 创建预编译对象,同时编写sql语句</font>
PreparedStatement pst = null;
try {
pst = conn.prepareStatement(sql);

<font color=red size=4>//设置参数</font>
setPropretie(pst,args);

<font color=red size=4>// 执行sql语句</font>
pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}

<font color=red size=4>//设置参数</font>
private static void setPropretie(PreparedStatement pst,Object... args){
if(pst==null) return;
if(args==null) return;
try {
for(int i=0;i<args.length;i++){
pst.setObject(i+1,args[i]);
}
} catch (SQLException e) {
e.printStackTrace();
}
}

db.properties

driverName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/库名?characterEncoding=utf-8
username=root
password=root

JDBC.java

//预编译操作数据库 添加数据
@Test
public void jdbcAdd() throws SQLException {
// 创建连接对象
Connection conn = DBUtil.getConnection();
String sql =" insert into user(name,age,address,birthday) values(?,?,?,?)";
DBUtil.executeUpdate(conn,sql,"周瑜",25,"吴国",new Date(new
java.util.Date().getTime()));
//释放资源
DBUtil.closeAll(conn);
}
//修改
@Test
public void jdbcUpdate() throws SQLException {
//创建连接对象
Connection conn =DBUtil.getConnection();
String sql ="update user set name=?,age=?,address=?,birthday= ? where id = ? ";
DBUtil.executeUpdate(conn,sql,"孙尚香",22,"吴国",new Date(new
java.util.Date().getTime()),18);
// 释放资源
DBUtil.closeAll(conn);
}
//删除
@Test
public void jdbcDelete() throws SQLException {
Connection conn = DBUtil.getConnection();
String sql =" delete from user where id = ? ";
DBUtil.executeUpdate(conn,sql,3);
DBUtil.closeAll(conn);
}
//查询所有
@Test
public void jdbcQueryAll() throws SQLException {
Connection conn = DBUtil.getConnection();
String sql =" select id,name,age,address,birthday from user ";
//执行查询操作
ResultSet rs = DBUtil.executeQuery(conn, sql);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
Date birthday = rs.getDate(5);
System.out.println(id+" "+name+" "+age+" "+address+" "+birthday);
}
DBUtil.closeAll(conn);
}
//查询一条
@Test
public void jdbcQueryById() throws SQLException {
Connection conn = DBUtil.getConnection();
String sql =" select * from user where id = ? ";
ResultSet rs =DBUtil.executeQuery(conn,sql,7);
while(rs.next()){
int id = rs.getInt(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
Date birthday = rs.getDate(5);
System.out.println(id+" "+name+" "+age+" "+address+" "+birthday);
}
DBUtil.closeAll(conn);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值