从JDBCUtils看代码简洁之道

从JDBCUtils看代码简介之道

使用PreparedStatement实现增删改查

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
import java.util.Scanner;

public class TestPreparedStatement1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
System.out.println("请输入待修改的客户编号:");
int id = input.nextInt();
System.out.println("请输入新的客户姓名:");
String name = input.next();

//------------------------连接数据库的步骤------------------------

Properties info = new Properties();
info.load(new FileInputStream("src\\jdbc1.properties"));

String user = info.getProperty("user");
String password = info.getProperty("password");
String url = info.getProperty("url");
String driver = info.getProperty("driver");

//1.注册驱动
Class.forName(driver);
//2.获取连接
Connection connection = DriverManager.getConnection(url, user, password);

//3.执行修改
String sql = "UPDATE customers SET name = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setInt(2, id);
int i = statement.executeUpdate();//执行增删改,返回受影响的行数
System.out.println(i > 0 ? "修改成功!" : "修改失败!");

//4.关闭
statement.close();
connection.close();
}
}

工具类JDBCUtils

由于每次都要加载配置文件,读取配置信息,注册驱动,获取连接,想到封装成工具类JDBCUtils。功能:

  • 1、获取连接
  • 2、释放资源
public class JDBCUtils {
/**
* 功能:获取可用的连接对象
* @return 连接
* @throws Exception
*/
public static Connection getConnection() throws Exception{
Properties info = new Properties();
info.load(new FileInputStream("src\\jdbc1.properties"));

String user = info.getProperty("user");
String password = info.getProperty("password");
String url = info.getProperty("url");
String driver = info.getProperty("driver");

//1.注册驱动
Class.forName(driver);
//2.获取连接
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
}
/**
* 功能:释放资源
*
*/
public static void close(ResultSet set, Statement statement,Connection connection) throws Exception {
if(set != null) set.close();
if(statement != null) statement.close();
if(connection != null) connection.close();
}
}

在getConnection()方法中仍有可以提出的部分,使用静态代码块只执行一次加载配置文件,读取配置信息,注册驱动的步骤,使代码更加简洁,效率更高。注意:提出的步骤需要处理异常。将user\url。

public class JDBCUtils {
static String user;
static String password;
static String url ;
static String driver;

static{
try {
Properties info = new Properties();
info.load(new FileInputStream("src\\jdbc1.properties"));

user = info.getProperty("user");
password = info.getProperty("password");
url = info.getProperty("url");
driver = info.getProperty("driver");

//1.注册驱动

Class.forName(driver);
} catch (Exception e) {
throw new RuntimeException(e);//编译时异常转为运行时异常
}
}

/**
* 功能:获取可用的连接对象
* @return 连接
* @throws Exception
*/
public static Connection getConnection() throws Exception{

//2.获取连接
return DriverManager.getConnection(url, user, password);
}
/**
* 功能:释放资源
*
*/
public static void close(ResultSet set, Statement statement,Connection connection) throws Exception {
if(set != null) set.close();
if(statement != null) statement.close();
if(connection != null) connection.close();
}
}

改进之后

有了JDBCUtils工具类,再看改进后的代码

public class TestPrepasredStatementByUtils {
public static void main(String[] args) throws Exception{
Scanner input = new Scanner(System.in);
System.out.println("请输入待修改的客户编号:");
int id = input.nextInt();
System.out.println("请输入新的客户姓名:");
String name = input.next();

//----------------------连接数据库的步骤----------------
//1.获取连接
Connection connection = JDBCUtils.getConnection();
//2.执行修改
String sql = "UPDATE customers SET name = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setInt(2, id);
int i = statement.executeUpdate();//执行增删改,返回受影响的行数
System.out.println(i > 0 ? "修改成功!" : "修改失败!");

//3.关闭
JDBCUtils.close(null,statement,connection);
}
}

直接调用工具类中的方法就简洁多啦!

再体会一下编译时异常变为运行时异常:

如果不抛异常,会报错

对JDBCUtils工具类中的getConnection()方法进行try-catch处理,将编译时异常转为运行时异常

public static Connection getConnection() {

//2.获取连接
try {
return DriverManager.getConnection(url, user, password);
}
catch (Exception e){
throw new RuntimeException(e);
}

}

此时将不会报编译时错误

对另一个close方法的处理也一样

public static void close(ResultSet set, Statement statement,Connection connection){
try {
if (set != null) set.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
}
catch (SQLException e){
throw new RuntimeException(e);
}
}

最终代码为:

public class TestPrepasredStatementByUtils {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("请输入待修改的客户编号:");
int id = input.nextInt();
System.out.println("请输入新的客户姓名:");
String name = input.next();

//----------------------连接数据库的步骤----------------
Connection connection = null;
PreparedStatement statement = null;
try {
//1.获取连接
connection = JDBCUtils.getConnection();
//2.执行修改
String sql = "UPDATE customers SET name = ? WHERE id = ?";
statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setInt(2, id);
int i = statement.executeUpdate();//执行增删改,返回受影响的行数
System.out.println(i > 0 ? "修改成功!" : "修改失败!");
}
catch(Exception e){
e.printStackTrace();
}
finally {
//3.关闭
JDBCUtils.close(null, statement, connection);
}
}
}
------ 本文结束感谢您的阅读 ------