javaWeb implemented using DAO CRUD

Introducing the core driver package mysql

1, tools

         public class BaseDao {
    
    
    static Properties proper;
    static String driver;
    static String url;
    static String username;
    static String password;
    Connection conn;
    
    static{
        init();
    }
    
    public static void init(){
        
        proper = new Properties();
        
        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
        
        try {
            proper.load(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        driver = proper.getProperty("driver");
        url = proper.getProperty("url");
        username = proper.getProperty("username");
        password = proper.getProperty("password");
        
    }
    
    public Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(driver);
            System.out.println("加载数据库成功!");
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("链接数据库成功!");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        return conn;
    }
    
    public void closeConnection(Connection conn , Statement state , ResultSet rs){
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(state != null){
            try {
                state.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    public int exceuteUpdate(String sql, Object patame[]){
        
        PreparedStatement ps = null;
        
        int num = 0;
        
        conn = getConnection();
        
        try {
            ps = conn.prepareStatement(sql);
            if(patame != null){
                for(int i = 0; i < patame.length ; i ++){
                    ps.setObject(i+1, patame[i]);
                }
            }
            num = ps.executeUpdate();
            
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            closeConnection(conn, ps, null);
        }
        
        System.out.println("num:"+num);
        return num;
    }    
    
}

 

2, the definition of CRUD interfaces

         

public interface UserInterface {
    
    public int InsterMethod(User user);
    
    public int UpdateMethod(User user);
    
    public int DeleteMethod(User user);

    public User FillAll();
    
}
 

 

 

3, the implementation class

         

public class UserInterfaceDaoImpl extends BaseDao implements UserInterface {

    @Override
    public int InsterMethod(User user) {
        
        String sql = "insert into User values(?,?)";
        Object obj[] = {user.username,user.password};
        int num = exceuteUpdate(sql, obj);
        return num;
    }

    @Override
    public int UpdateMethod(User user) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int DeleteMethod(User user) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public User FillAll() {
        // TODO Auto-generated method stub
        return null;
    }

}
 

4, the entity class

         public class User {
    
    public String username;
    public String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

 

5, the test class

         

public class javaDemo01 {

    public static void main(String[] args) {

        UserInterfaceDaoImpl uifdi = new UserInterfaceDaoImpl();
        User user = new User();
        user.setUsername("zhaoyun");
        user.setPassword("123456");
        uifdi.InsterMethod(user);
        
        
    }

}
 

 

6, link database configuration file

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

 

 

 

Published 40 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/sj_1993/article/details/103247295