My sql数据库的增删改查

增:

String insert = "insert into students(Sno,Sname,Ssex,Sage,Scno) value(?,?,?,?,?)";  
        PreparedStatement sta = con.prepareStatement(insert);  
        sta.setString(1, "1013");  
        sta.setString(2, "***");  
        sta.setString(3, "男");  
        sta.setInt(4, 20);  
        sta.setInt(5, 3);  
        int row=sta.executeUpdate();  
        if(row==1)  
        {  
            System.out.println("插入成功");  
        }  

删:

String delete = "delete from students where Sno='1006'";  
        PreparedStatement dele = con.prepareStatement(delete);  
        dele.executeUpdate();  

改:

String update = "update students set Sage=21 where Sno='1005'";  
        PreparedStatement upd = con.prepareStatement(update);  
        upd.executeUpdate();  

查:

//数据库某表的逐条查询  
        String select = "select * from students";  
        PreparedStatement sts = con.prepareStatement(select);  
        ResultSet rs = sts.executeQuery();  
        System.out.println("学号       "+"姓名    "+"性别     "+"年龄     "+"课程号");  
        while(rs.next())  
        {  

            System.out.print(rs.getString("Sno")+" ");  
            System.out.print(rs.getString("Sname")+" ");  
            System.out.print(rs.getString("Ssex")+"    ");  
            System.out.print(rs.getInt("Sage")+"    ");  
            System.out.println(rs.getInt("Scno"));  
        }  
        //数据库中特定项查询,例如选课程2的同学的信息;  
        String selectone = "select * from students where Scno=2";  
        PreparedStatement stso = con.prepareStatement(selectone);  
        ResultSet reo = stso.executeQuery();  
        while(reo.next())  
        {  
            System.out.print(reo.getString("Sno")+" ");  
            System.out.print(reo.getString("Sname")+" ");  
            System.out.print(reo.getString("Ssex")+"    ");  
            System.out.print(reo.getInt("Sage")+"    ");  
            System.out.println(reo.getInt("Scno"));  
        }  

你可能感兴趣的:(总结)