在mysql jdbc中自动生成密钥

时间:2013-03-31 08:25:46

标签: java mysql

我没有从mysql

获取自动生成的密钥
String query="insert into course(subject,board,standard,course_code)values(?,?,?,?)";
            st1 = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
            st1.setString(1, sub);
            st1.setString(2, board);
            st1.setString(3, std);
            st1.setString(4, course);

            rs1 = st1.getGeneratedKeys();
            if (rs1.next()){
                cid=rs1.getInt(1);
            }
            System.out.println("2>>>>>>>>" + cid+" "+rs1);

输出:

2>>>>>>>>0 com.mysql.jdbc.JDBC4ResultSet@1b04d3d

1 个答案:

答案 0 :(得分:2)

撰写resultset.executeQuery,然后致电getGeneratedKey