javaWeb使用DAO实现增删改查

导入mysql核心驱动包

1、工具类

         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、定义增删改查接口

         

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

    public User FillAll();
    
}
 

3、实现类

         

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、实体类

         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、测试类

         

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、链接数据库配置文件

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

发布了40 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sj_1993/article/details/103247295