DAO类简单介绍
DAO类:操作数据库的实体类
1.首先你得连接数据库,这就用到了JDBC技术,你得可以编写一个封装的工具类,用来操作数据库
2.连接数据库的步骤:
1.注册驱动 -----------> 告诉java程序,连接的是哪个品牌的数据库
2.获取连接对象 -----------> 表示连接Java程序和数据库的通道打开了
3.创建操作SQL语句的对象 ----------> 表示把货车造出来
4.执行SQL语句 ----------> 把货装进车里
5.处理查询结果集 -----------> (只有当第四步执行的是select语句时,才有第五步,否则直接第六步释放资源)
6.释放资源 ------------> 本次出货不管成不成功,都关闭通道
3.工具类代码实现
1.注意:
1.工具类的构造方法都是私有的,不需要new对象,直接使用类名调用即可
2.把注册驱动放到静态代码块中,这样类加载的时候,数据库的驱动也顺便加载了,而且驱动只会注册一次。
1 package com.bjpowernode.util; 2 3 import java.sql.*; 4 5 public class DBUtil { 6 private DBUtil(){}; 7 static{ 8 try { 9 Class.forName("com.mysql.jdbc.Driver"); 10 } catch (ClassNotFoundException e) { 11 e.printStackTrace(); 12 } 13 } 14 15 // 获取连接对象 16 public static Connection getConnection() throws SQLException { 17 return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bjpowernode","root","root"); 18 } 19 // 释放资源 20 public static void close(Connection conn, Statement stat, ResultSet rs){ 21 if(rs != null){ 22 try { 23 rs.close(); 24 } catch (SQLException throwables) { 25 throwables.printStackTrace(); 26 } 27 } 28 if(stat != null){ 29 try { 30 stat.close(); 31 } catch (SQLException throwables) { 32 throwables.printStackTrace(); 33 } 34 } 35 if(conn != null){ 36 try { 37 conn.close(); 38 } catch (SQLException throwables) { 39 throwables.printStackTrace(); 40 } 41 } 42 } 43 }
4.工具类测试
1.数据查询语言(DQL):凡是select语句都是DQL
2.查询方法:executeQuery
1 public List findALl(){ 2 String sql = "select * from users"; // 1.货物 3 List list = new ArrayList();// 返回的对象可能有多个,所以来个箱子一起装走(ArrayList集合) 4 try { 5 conn = DBUtil.getConnection(); // 2.车道 6 ps = conn.prepareStatement(sql); // 3.运送货物的车 7 rs = ps.executeQuery(); // 4.装车 8 while (rs.next()) { 9 Integer userId = rs.getInt("userId"); 10 String userName = rs.getString("userName"); 11 String password = rs.getString("password"); 12 String sex = rs.getString("sex"); 13 String email = rs.getString("email"); 14 Users users = new Users(userId,userName,password,sex,email); 15 list.add(users); 16 } 17 }catch (Exception e){ 18 e.printStackTrace(); 19 }finally { 20 DBUtil.close(conn,ps,rs); //关闭通道 21 } 22 return list; 23 }
2.数据操作语言(DML):insert:增加 delete:删除 update:修改
1.insert方法:int executeUpdate(String sql) 返回值:int
1 public class UserDao { 2 Connection conn = null; 3 PreparedStatement ps = null; 4 ResultSet rs = null; 5 // 用户注册的方法 6 public int add(Users user){ 7 String sql = "insert into users(userName,password,sex,email) values(?,?,?,?)"; 8 int result = 0; 9 try { 10 conn = DBUtil.getConnection(); 11 ps = conn.prepareStatement(sql); 12 ps.setString(1, user.getUserName()); 13 ps.setString(2, user.getPassword()); 14 ps.setString(3, user.getSex()); 15 ps.setString(4, user.getEmail()); 16 result = ps.executeUpdate(); // 添加数据的方法 17 }catch (Exception e){ 18 e.printStackTrace(); 19 }finally { 20 DBUtil.close(conn,ps,rs); 21 } 22 return result; 23 } 24 }
2.delete方法:int executeUpdate(String sql) 返回值:int
1 // 根据用户id删除用户 2 public int delete(String userId){ 3 String sql = "delete from users where userId = ?"; // 货物 4 int result = 0; 5 try{ 6 conn = DBUtil.getConnection(); // 车道 7 ps = conn.prepareStatement(sql); // 造车 8 ps.setInt(1,Integer.valueOf(userId)); 9 result = ps.executeUpdate(); // 发车 10 11 }catch (Exception e){ 12 e.printStackTrace(); 13 }finally { 14 DBUtil.close(conn,ps,rs); 15 } 16 return result; 17 }
3.修改方法:int executeUpdate(String sql) 返回值:int
String sql = "update dept set dname = '销售部',loc = '天津' where deptno = 20"; int count = stmt.executeUpdate(sql); System.out.println(count == 1 ? "修改成功":"修改失败");

浙公网安备 33010602011771号