jdbc工具类
由于JDBC的注册驱动,连接数据库,关闭资源的步骤是相同的,所以我们可以写一个JDBC工具类。
/*
工具类:
私有化构造方法
提供静态方法
*/
public class jdbcUtil {
private final static String DRIVER = "com.mysql.cj.jdbc.Driver";
private final static String URL = "jdbc:mysql://127.0.0.1:3306/mysql_day2";
private final static String NAME = "root";
private final static String PASSWORD = "123456";
//注册驱动
static//静态代码块,类加载的时候执行,且只执行一次
{
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
//与数据库连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, NAME, PASSWORD);
}
//释放资源
public static void Close(Statement s, Connection c, ResultSet r) {
if (r != null) {
try {
r.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (s != null) {
try {
s.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (c != null) {
try {
c.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public static void Close(Statement s, Connection c)
{
Close(s,c,null);
}
}
测试:
public class testUtil {
@Test
public void testUpdate() {
Connection con = null;
PreparedStatement pstat = null;
try {
//获取连接
con = jdbcUtil.getConnection();
con.setAutoCommit(false);//开启事务
//编写sql语句
String sql = "update user set password=? where name=?";
//获取数据库对象
pstat = con.prepareStatement(sql);
pstat.setInt(1, 23456);
pstat.setString(2, "hhh");
int i = pstat.executeUpdate();
if (i > 0) {
System.out.println("数据修改成功");
con.commit();//提交
}
} catch (Exception e) {//有异常终止就回滚
try {
con.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
} finally {//不管有没有异常,都要关闭资源
jdbcUtil.Close(pstat, con);
}
}
}
开发中常用的三层架构模型
- web层:接收客户端发送的数据->把接收的数据封装成对象->调用services层的方法(并传递数据对象)->根据services层方法执行结果,给客户端回馈结果
- service层:处理业务逻辑(会调用dao的方法)
- dao层:和数据库交互(底层利用jdbc技术)
在开发中三层架构通常的命名
web层 com.hhh.web
service层 com.hhh.service
dao层 com.hhh.dao
分层的目的
解耦:降低代码之间的依赖关系
可维护性:哪一层出问题,就去维护哪一层
可扩展性:哪一层需要数据,就在某一层添加数据
下面我们使用三层架构模式实现用户登录
先创建一个对象,保存用户的名字和密码
public class User {
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
web层:
public class web {
@Test
public void login() {
String name = "hhh";//获取客户端发来的数据
String password = "23456";
//封装成对象
User user = new User();
user.setName(name);
user.setPassword(password);
//调用service层的方法,把对象发送给service
service s=new service();
boolean b = s.userLogin(user);
if(b)
{
System.out.println("有当前用户");
}
else {
System.out.println("没有此用户");
}
}
}
service层
public class service {
public boolean userLogin(User loginUser)
{
//校验:传递的数据是否合法
if(loginUser==null)
{
throw new RuntimeException("传递的参数为null");
}
//调用dao层的方法
dao d=new dao();
User login = d.login(loginUser);
if(login!=null)
{
return true;
}
return false;
}
}
Dao层
public class dao {
public User login(User loginUser)
{
User u=null;
Connection con=null;
PreparedStatement pstat=null;
ResultSet rs=null;
try{
con=jdbcUtil.getConnection(); //获取连接
String sql="select name,password from user where name=? and password=?";
pstat = con.prepareStatement(sql);
pstat.setString(1,loginUser.getName());
pstat.setString(2,loginUser.getPassword());
rs = pstat.executeQuery();
if(rs!=null&&rs.next())
{
String name=rs.getString("name");
String password=rs.getString("password");
u=new User(name,password);
}
}catch (Exception e)
{
e.printStackTrace();
}finally {
jdbcUtil.Close(pstat,con,rs);
}
return u;//如果没有数据返回的是null
}
}
数据库连接池
数据库连接池是一个容器,负责分配,管理数据库连接
它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个
好处:
资源重用
提升系统响应速度