简单的CRUD(二)

2023-05-03,

一、重构简单CRUD

1.JDBC工具类

1.因为在crud中都包含一些相同的代码所以可以提取出来,抽取代码重构为工具类。

2.将工具类设置为static静态类,方便调用,不需要new对象。

 public class JDBCUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/station";
private static String username = "root";
private static String password = "admin";
public static Connection getConnection(){
/**
* 1.对加载和创建连接的重构
* 2.把参数提取出来
* 3.返回connection
*/
Connection connection=null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
} /**
* 对关闭资源的异常代码的重构
* @param connection
* @param preparedStatement
*/
public static void close(PreparedStatement preparedStatement,Connection connection){
try {
if (preparedStatement!=null){
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try{
if (connection!=null){
connection.close();
}
}catch (SQLException e){
e.printStackTrace();
}
}
} /**
* 重构close
* @param preparedStatement
* @param connection
*/
public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection){
try {
if (resultSet!=null){
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(preparedStatement, connection);
}
}
}

二、使用预编译sql语句

1.预编译sql语句的好处

  1.效率高,预编译对象把一些格式固定的SQL编译后,存放在内存池中即数据库缓冲池,当我们再次执行相同的SQL语句时就不需要预编译的过程了,只需DBMS运行SQL语句。所以当你需要执行Statement对象多次的时候,PreparedStatement对象将会大大降低运行时间,特别是的大型的数据库中,它可以有效的也加快了访问数据库的速度。(反复使用一个sql语句时)

  2.提高了代码的可读性和维护性,将参数与SQL语句分离出来,这样就可以方便对程序的更改和延续,同样,也可以减少不必要的错误。

  3.开源防止SQL注入。

2.用户实体类

 public class LoginUser {
// 创建注册用户属性
private Integer id;
private String username;
private String password; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

3、用户dao接口

 public interface ILoginUserDao {
/**
* 保存用户
* @param loginUser
*/
void save(LoginUser loginUser); /**
* 通过用户名id查询用户信息
* @param id
* @return
*/
LoginUser getLoginUserById(Integer id);
}

4、用户dao实现

 public class LoginUserDao implements ILoginUserDao {
@Override
public void save(LoginUser loginUser) {
/**
* 0.导入驱动包
* 1.加载 2.连接 提取到了JDBCUtil工具类
* 3.创建预编译语句
* 4.执行sql语句
* 5.释放资源 提取到了JDBCUtil工具类
*/
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//调用工具类中的getConnection,返回连接
connection = JDBCUtil.getConnection();
String sql = "INSERT INTO loginuser (username, password) VALUES (?, ?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, loginUser.getUsername());
preparedStatement.setString(2, loginUser.getPassword());
//执行sql语句
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
JDBCUtil.close(preparedStatement, connection);
}
}   @Override
public LoginUser getLoginUserById(Integer id) {
LoginUser loginUser = new LoginUser();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//调用工具类中的getConnection,返回连接
connection = JDBCUtil.getConnection();
String sql = "SELECT id, username, password FROM loginuser where id = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
//执行sql语句
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
int id1 = resultSet.getInt("id");
String userName = resultSet.getString("username");
String password = resultSet.getString("password");
//封装对象
loginUser.setId(id1);
loginUser.setUsername(userName);
loginUser.setPassword(password);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
JDBCUtil.close(resultSet, preparedStatement, connection);
}
return loginUser;
}

简单的CRUD(二)的相关教程结束。

《简单的CRUD(二).doc》

下载本文的Word格式文档,以方便收藏与打印。