总结:本文通过对JDBC工具类的封装和使用工具类实现模糊查询,带你入门JDBC,其中使用的知识点在上篇博客里有!
场景:
在学习JDBC的语言中,每次都执行通用的几步:即注册驱动,获取连接,创建操作,处理结果,释放资源 过于复杂,因此不妨将上述步骤封装成工具类,只对外提供方法!
描述:
这是不使用工具类的封装写出来的代码,比较冗余复杂
package com.zdx.JDBC;
import java.sql.*;
public class JAVA1129_5 {
public static void main(String[] args) {
//设置空对象,注册驱动,获取连接,创建操作,处理结果集,释放资源
String url = "jdbc:mysql://127.0.0.1:3306/hello";
String username = "root";
String password = "rota";
String SQL = "insert into stu values(1,'zdx','nbnc'),(2,'cyc','qwq');";
// String SQL1 = "update stu set sname ='xzq',major='bask' where sno = '1';";
String SQL1="select * from stu";
Connection connection = null;
Statement statement = null;
ResultSet resultset = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
int cnt = statement.executeUpdate(SQL);
if (cnt != 0) {
System.out.println("执行成功");
}
ResultSet result = statement.executeQuery(SQL1);
while (result.next()) {
//随着光标移动对操作对象进行操作
System.out.println("nbnb");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//释放资源,必须在最后加上finally语句块执行
finally {
if (resultset != null) {
try {
resultset.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
解决方案:
首先类内的构造方法加私有修饰符,模仿Sun公司工具类,如Arrays类 和 Collection 。
其次注册驱动,利用静态代码块内只注册一次进行注册驱动
然后获取数据库连接,返回数据库连接对象的方法内有异常,不能catch,需要向外扔。
最后封装一个关闭的方法。
注意由于封装工具类,且对外只提供方法因此都封装成类方法(即static修饰)
package com.zdx.JDBC; import java.sql.*; //2021.11.2920点03分 对数据库的工具类进行封装 public class DBUtil{ private DBUtil(){} static{ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } }//利用静态代码块在类加载时只加载一次的特性注册驱动。 //获取连接的方法 public static Connection getConnection (String url,String user,String password)throws SQLException{ return DriverManager.getConnection(url,user,password);//这里注意驱动管理类内调用的获取连接方法返回对象就是connection对象。 } //关闭资源 //按照顺序,结果集,数据库操作对象,连接对象! public static void close(Connection connection,Statement ps,ResultSet resultSet){ if(resultSet!=null){ try { resultSet.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(ps!=null){ try { ps.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
对工具类进行调用实现模糊查询:
package com.zdx.JDBC; import java.sql.*; public class Main { public static void main(String[] args) { Connection connection = null; PreparedStatement ps = null; ResultSet resultSet = null; String url = "jdbc:mysql://127.0.0.1:3306/hello"; String user = "root"; String password = "rota"; //获取连接 try { connection = DBUtil.getConnection(url, user, password); } catch (SQLException throwables) { throwables.printStackTrace(); } //获取预编译的数据库操作对象 String SQL = "select sname from stu where sname like ?"; try { ps = connection.prepareStatement(SQL); ps.setString(1, "_y%"); resultSet = ps.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString("sname")); } } catch (SQLException throwables) { throwables.printStackTrace(); } //释放资源 finally { DBUtil.close(connection, ps, resultSet); } } }