数据库访问JavaBean的设计
Posted ing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库访问JavaBean的设计相关的知识,希望对你有一定的参考价值。
1.在数据库test建表
2.在类路径(src)下建立文件db.properties
driver=com.mysql.jdbc.Driver; url=jdbc\\:mysql\\://localhost\\:3306/test?useUnicode\\=true&characterEncoding\\=utf-8; user=root; password=123456;
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的设计的主要内容,如果未能解决你的问题,请参考以下文章