java jdbc工具类抽取_JavaWeb入门(三):JDBC工具类的抽取

一、通过上篇文章,我们已经可以使用JDBC对数据库中的表进行增删改查啦(JDBC的基本使用:https://www.cnblogs.com/Infancy/p/12499806.html),我们对上篇文章的代码从头到尾看一下会发现,增删改查每个方法中有些一样的代码,如:加载驱动、获取连接等,为了使代码更简洁高效,我们将公用代码提取出来,封装到一个工具类中。

二、封装此工具类分为三步:

将驱动类全路径、数据库连接地址、数据库用户名、数据库密码写到一个配置文件中,properties或xml都行,这里我们以properties为例。写到配置文件里面的好处是后期我们修改数据库连接相关信息的时候直接修改这个配置文本文件即可。

新建一个JDBCUtils类,在静态代码块中将数据库连接信息加载

编写静态的加载驱动、获取连接、释放资源方法

三、代码示例(自己写的,亲测可以跑起来)

1.db.properties配置文件

6bd206850edfee553d2199aaf0c9ce50fbb.jpg

8f85a5f89a987ce1323772e8d0c6444e81a.jpg

driverClassName=com.mysql.jdbc.Driver

dbUrl=jdbc:mysql://localhost:3306/web_test3

username=root

password=root

View Code

3335f1657efee2df318e2f3a4def221b.png

2.JDBCUtils代码

d3c38fef8943f8d199cadd56602f1664616.jpg

068b5f304e91505fe07edae3e5feb5b24a0.jpg

packagecom.zhurouwangzi.utils;

​importjava.io.FileInputStream;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;importjava.util.Properties;

​public classJDBCUtils {public staticString driverClassName;public staticString dbUrl;public staticString username;public staticString password;

​//静态代码块用于加载配置文件中的数据库信息

static{

Properties properties= newProperties();try{

properties.load(new FileInputStream("src/db.properties"));

}catch(Exception ex){

ex.printStackTrace();

}

driverClassName= properties.getProperty("driverClassName");

dbUrl= properties.getProperty("dbUrl");

username= properties.getProperty("username");

password= properties.getProperty("password");

}//加载驱动方法

public static voidloadDriver(){try{

Class.forName(driverClassName);

}catch(Exception ex){

ex.printStackTrace();

}

}

​//获取连接

public staticConnection getConnection(){

Connection conn= null;try{

conn=DriverManager.getConnection(dbUrl,username ,password );

}catch(Exception ex){

ex.printStackTrace();

}returnconn;

}

​public static voidrelease(Connection conn, Statement statement, ResultSet rs){if(conn!=null){try{

conn.close();

}catch(Exception e){

e.printStackTrace();

}

conn= null;

}if(statement!=null){try{

statement.close();

}catch(Exception e){

e.printStackTrace();

}

statement= null;

}if(rs!=null){try{

rs.close();

}catch(Exception e){

e.printStackTrace();

}

rs= null;

}

}

}

View Code

3.使用JDBC工具类,重新编写之前的方法(这里以添加方法为例)

5dc07ffd0fd089d16e5363f51dcdd8c772e.jpg

1b3dc210a49c4475d7fec06733eb2194ce3.jpg

@Testpublic voidaddData(){

Connection conn= null;

Statement statement= null;try{//加载驱动

JDBCUtils.loadDriver();//获取连接

conn =JDBCUtils.getConnection();//获取执行sql的对象

statement =conn.createStatement();//编写sql

String strSql = "insert into user_baseinfo values(null,'2','学生','正常','aa')";//执行sql并获得返回结果

int num =statement.executeUpdate(strSql);if(num>0){

System.out.println("添加成功");

}else{

System.out.println("添加失败");

}

}catch(Exception ex){

ex.printStackTrace();

}finally{//释放资源

JDBCUtils.release(conn, statement,null);

}

}

View Code

四、参数化执行sql

f47c52a07b80491387bdfad6c17e3c6e24b.jpg

7d6925e4f1c1f80d403ef511c4dfba3474c.jpg

@Testpublic voiddeleteData(){

Connection conn= null;

PreparedStatement prestatement= null;try{//加载驱动

JDBCUtils.loadDriver();//获取连接

conn =JDBCUtils.getConnection();//编写sql

String strSql = "delete from user_baseinfo where id = ?";//获取连接对象

prestatement =conn.prepareStatement(strSql);//设置参数

prestatement.setString(1, "11");//执行sql

int num =prestatement.executeUpdate();if(num>0){

System.out.println("删除成功");

}else{

System.out.println("删除失败");

}

}catch(Exception ex){

ex.printStackTrace();

}finally{

JDBCUtils.release(conn, prestatement,null);

}

}

View Code

五、JDBC的批处理

在默认情况下,Mysql的批处理是没有开启的,需要在数据库的url后边加一个参数才能开启(?rewriteBatchedStatements=true)

如:url=jdbc:mysql://localhost:3306/web_test3?rewriteBatchedStatements=true

aa7172d54d54c58b6601259dee8b2905.png

1.实例代码

e366bbfe27ded8ddddb94e3272a10650a46.jpg

73e7bb69ee4bae584fd517f845b1f379b1c.jpg

@Testpublic voidaddData(){

Connection conn= null;

Statement statement= null;try{//加载驱动

JDBCUtils.loadDriver();//获取连接

conn =JDBCUtils.getConnection();//获取执行sql的对象

statement =conn.createStatement();//编写sql

String strSql = "insert into user_baseinfo values(null,'2','学生','正常','aa')";

String strSql2= "insert into user_baseinfo values(null,'3','工人','正常','bb')";

String strSql3= "insert into user_baseinfo values(null,'4','农民','正常','cc')";//将要执行的sql添加到批处理里面

statement.addBatch(strSql);

statement.addBatch(strSql2);

statement.addBatch(strSql3);//执行批处理

statement.executeBatch();

}catch(Exception ex){

ex.printStackTrace();

}finally{//释放资源

JDBCUtils.release(conn, statement,null);

}

}

View Code

2.批处理参数版

8242a3f8c9544e15084d6a2c1a66c2a45d2.jpg

798bca570947156736f5510ee41d4496bbd.jpg

@Testpublic voidaddDataPre(){

Connection conn= null;

PreparedStatement preStatement= null;try{//加载驱动

JDBCUtils.loadDriver();//获取连接

conn =JDBCUtils.getConnection();//编写sql

String strSql = "insert into user_baseinfo values(null,?,?,?,?)";//预编译sql

preStatement =conn.prepareStatement(strSql);for(int i =1;i<=10;i++){

preStatement.setString(1, "tel"+i);

preStatement.setString(2, "type"+i);

preStatement.setString(3, "statu"+i);

preStatement.setString(4, "name"+i);//添加到批处理

preStatement.addBatch();//执行批处理(每5个执行一次)

if(i%5==0){

preStatement.executeBatch();

preStatement.clearBatch();

}

}

}catch(Exception ex){

ex.printStackTrace();

}finally{

JDBCUtils.release(conn,preStatement,null);

}

}

View Code

转载请注明出处:cnblogs.com/Infancy/p/12502329.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值