Cheug's Blog

当前位置:网站首页 / JAVA / 正文

JDBC的CURD操作

2019-05-08 / JAVA / 951 次围观 / 0 次吐槽 /

CURD 增删改查

        C:Create

        U:Update

        R:Read

        D:Delete

 

插入数据

public void test1(){
   
    Connection conn = null;//创建连接
    PreparedStatement stmt = null;//预处理
    try {
        conn = DBUtils.getConnection();
        stmt = conn.prepareStatement("INSERT INTO user VALUES(?,?,?,?,?)");
        //设置参数
        stmt.setInt(1, 6);
        stmt.setString(2, "tony");
            stmt.setString(3, "333");
                stmt.setString(4, "tony@163.com");
                stmt.setDate(5, new java.sql.Date(System.currentTimeMillis()));
                int row = stmt.executeUpdate();
                System.out.println("影响的行数:" + row);
        } catch (Exception e) {
                e.printStackTrace();
        }finally {
                DBUtils.closeAll(conn, stmt, null);
        }
  }

更改数据

public void test2(){
   
      Connection conn = null;//创建连接
            PreparedStatement stmt = null;//预处理
            try {
                    conn = DBUtils.getConnection();
                    stmt = conn.prepareStatement("UPDATE user SET password = ? , email = ? WHERE id = ?");
                    //设置参数
                    stmt.setString(1, "123456");
                    stmt.setString(2, "lily@qq.com");
                    stmt.setInt(3, 5);
                    int row = stmt.executeUpdate();
                    System.out.println("影响的行数:" + row);
            } catch (Exception e) {
                    e.printStackTrace();
            }finally {
                    DBUtils.closeAll(conn, stmt, null);
            }
}

删除数据

public void test3(){

            Connection conn = null;//创建连接
            PreparedStatement stmt = null;//预处理
            try {
                    conn = DBUtils.getConnection();
                    stmt = conn.prepareStatement("delete from user  WHERE id = ?");
                    //设置参数
                    stmt.setInt(1, 5);
                    
                    int row = stmt.executeUpdate();
                    System.out.println("影响的行数:" + row);
            } catch (Exception e) {
                    e.printStackTrace();
            }finally {
                    DBUtils.closeAll(conn, stmt, null);
            }
}

查询数据

public void test4(){
            Connection conn = null;//创建连接
            Statement stmt = null;//预处理
            ResultSet resultSet = null;//结果集
            try {
                    conn = DBUtils.getConnection();
                    stmt = conn.createStatement();
                    resultSet = stmt.executeQuery("select * from user;");
                    List<User> list = new ArrayList<User>();
                    //5、处理结果
                    while(resultSet.next()){
                            User user = new User();
                            user.setId(resultSet.getInt(1));
                            user.setName(resultSet.getString(2));
                            user.setPassword(resultSet.getString(3));
                            user.setEmail(resultSet.getString(4));
                            //java.sql.Date继承java.util.Date
                            user.setDate(resultSet.getDate(5));
                            list.add(user);
                    }
                    //打印集合
                    System.out.println(list);
            
            } catch (Exception e) {
                    e.printStackTrace();
            }finally {
                    DBUtils.closeAll(conn, stmt, null);
            }
}


Powered By Cheug's Blog

Copyright Cheug Rights Reserved.