简单的CRUD(二)
2024-08-27 22:08:24
一、重构简单的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;
}
最新文章
- 新手入门JUnit单元测试
- 将博客搬至CSDN
- linux 防火墙配置
- VirtualBox安装Ubuntu教程
- canvas实践小实例二 —— 扇形
- Django 1.6.0 正式发布,大幅改进事务处理
- 在EntityFramework中使用 nock的方法。
- 股票自用指标 boll 菜刀
- 009--VS2013 C++ 显示位图部分透明化
- php正则过滤html标签、空格、换行符的代码,提取图片
- java 正则表达式抽取
- C++四种类型的转换
- Helper Method
- 一张图搞定Java设计模式——工厂模式! 就问你要不要学!
- Ubuntu 16.04 LTS(入门一)国内快速更新软件源
- HTML5 placeholder(空白提示) 属性
- 点聚weboffice隐藏自带工具栏
- Redhat 6.3上安装libssh
- java 值传递 数组传递
- Java注册帐号邮箱激活验证实现
热门文章
- DOM基本操作
- “全栈2019”Java第五十三章:向上转型和向下转型详解
- 读优&;&;输优
- scrapy框架基于CrawlSpider的全站数据爬取
- INSERT IGNORE 与INSERT INTO的区别,以及replace的用法
- [ActionScript 3.0] 实现放大镜效果的简单方法
- Linux 下四条高大命令(计划360检测脚本)
- ubuntu15.04下安装jdk8
- UDF-Java提取身份证内信息
- Linux进程间通信——使用System V 消息队列