实验九jdbc数据库连接

jdbc数据库连接

1、分别使用Statement和PreparedStatement两种数据库声明,实现向userLogin表中插入新的数据记录(5,[email protected],123456,0);

//使用Statement
Statement stat=conn.createStatement();
String sql="insert into userLogin values(5,'[email protected]','123456',0))";            
stat.execute(sql);
//使用PreparedStatement
String sql="insert into userLogin values(?,?,?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,5);
ps.setString(2,"[email protected]");//
ps.setString(3,"123456");
ps.setInt(4,0);
ps.execute();

2、分别使用PreparedStatement两种数据库声明,实现删除userLogin表状态为冻结的用户信息;

//使用PreparedStatement
PreparedStatement ps=conn.prepareStatement("delete from Goods where state=?");
ps.setString(1,-1);
ps.executeQuery();
//使用Statement
Statement stat = con.createStatement();  
String sql = "delete from userLogin where state=-1";  
stat.executeUpdate(sql); 

3、分别使用PreparedStatement两种数据库声明,实现修改userLogin表中邮箱为zhangsan的用户的密码为abc123和状态为1;

//使用PreparedStatement
PreparedStatement ps=conn.prepareStatement("update userLogin set password=?,state=? where email=? );
ps.setString(1,"abc123");
ps.setString(2,1);
ps.setString(3,"zhangsan@qq.com");
ps.executeQuery();
//使用Statement
Statement stat = con.createStatement();  
String sql = "update userLogin set password='abc123',state=-1 where email='[email protected]'";  
stat.executeUpdate(sql); 

4、将userLogin表的所有数据读取出来,并按顺序打印到控制台上,将打印结果粘贴到实验报告中。

PreparedStatement ps=conn.prepareStatement("select * from userLogin");
rs=ps.executeQuery();
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
    int id = rs.getInt("id");
    String email = rs.getString("email");
    String password = rs.getString("password");
    int state = rs.getInt("state");
    System.out.print(id+" ");
    System.out.print(name+" ");
    System.out.print(email+" ");
    System.out.println(birth);
}

你可能感兴趣的:(Java)