时间:2021-07-01 10:21:17 帮助过:14人阅读
3.建立一个连接和释放资源的工具类JdbcUtil.java
package dbc; import java.sql.*; import java.util.Properties; public final class JdbcUtil { private static String driver; private static String user; private static String password; private static String url; private static Properties pr=new Properties(); private JdbcUtil(){} // static{ try { pr.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties")); driver= pr.getProperty("driver"); url = pr.getProperty("url"); user = pr.getProperty("username"); password = pr.getProperty("password"); Class.forName(driver); } catch (Exception e) { throw new ExceptionInInitializerError(e); } } // public static Connection getConnection()throws SQLException { return DriverManager.getConnection(url, user, password); } // public static void free(ResultSet rs,Statement st,Connection conn) { try { if(rs!=null) rs.close(); }catch (SQLException e) { e.printStackTrace(); }finally{ try { if(st!=null) st.close(); }catch (SQLException e) { e.printStackTrace(); } finally{ if(conn!=null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
4.建立类User.java实现记录信息对象化,体现面向对象程序设计思想。
package vo; public class User { private String userid,username,sex; public String getUserid(){return userid;} public void setUserid(String userid){this.userid=userid;} public String getUsername(){return username;} public void setUsername(String username){this.username=username;} public String getSex(){return sex;} public void setSex(String sex){this.sex=sex;} }
5.在上面步骤的基础上建立类UserDao.java封装基本的数据库。
package dao; import java.sql.*; import java.util.ArrayList; import java.util.List; import vo.User; import dbc.JdbcUtil; public class UserDao { public void add(User user)throws Exception{ Connection conn=null; PreparedStatement ps=null; try{ conn=JdbcUtil.getConnection(); String sql="insert into user values(?,?,?)"; ps=conn.prepareStatement(sql); ps.setString(1,user.getUserid()); ps.setString(2,user.getUsername()); ps.setString(3,user.getSex()); ps.executeUpdate(); }finally{JdbcUtil.free(null,ps,conn);} } public void update(User user)throws Exception{ Connection conn=null; PreparedStatement ps=null; try{ conn=JdbcUtil.getConnection(); String sql="update user set username=?,sex=? where userid=?"; ps=conn.prepareStatement(sql); ps.setString(1,user.getUserid()); ps.setString(2,user.getUsername()); ps.setString(3,user.getSex()); ps.executeUpdate(); }finally{JdbcUtil.free(null,ps,conn);} } public void delete(String userId)throws Exception{ Connection conn=null; PreparedStatement ps=null; try{ conn=JdbcUtil.getConnection(); String sql="delete from user where userid=?"; ps=conn.prepareStatement(sql); ps.setString(1,userId); ps.executeUpdate(); }finally{JdbcUtil.free(null,ps,conn);} } public User findUserById(String userId)throws Exception{ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; User user=null; try{ conn=JdbcUtil.getConnection(); String sql="select* from user where userid=?"; ps=conn.prepareStatement(sql); ps.setString(1,userId); rs=ps.executeQuery(); if(rs.next()){ user=new User(); user.setUserid(rs.getString(1)); user.setUsername(rs.getString(2)); user.setSex(rs.getString(3)); } }finally{JdbcUtil.free(null,ps,conn);} return user; } public List<User> QueryAll()throws Exception{ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; List<User>userList=new ArrayList<User>(); try{ conn=JdbcUtil.getConnection(); String sql="select* from user"; ps=conn.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ User user=new User(); user.setUserid(rs.getString(1)); user.setUsername(rs.getString(2)); user.setSex(rs.getString(3)); userList.add(user); } }finally{ JdbcUtil.free(rs, ps, conn); } return userList; } }
数据库访问JavaBean的设计
标签:log har delete date() list 面向对象 r.java password 技术