• 数据库访问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;
            
            
            
        }
    }
  • 相关阅读:
    zoj1137 poj1466
    poj3041
    zoj1455
    hdu1160 FatMouse's Speed
    zoj2770
    hdu1469
    hdu3169
    Mapped exception to response: 500 (Internal Server Error)
    Mapped exception to response: 500 (Internal Server Error)
    object is not a function
  • 原文地址:https://www.cnblogs.com/ljs-666/p/7768393.html
Copyright © 2020-2023  润新知