Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSP MYSql自动生成密钥_Java_Jsp - Fatal编程技术网

Java JSP MYSql自动生成密钥

Java JSP MYSql自动生成密钥,java,jsp,Java,Jsp,我想创建自动生成的密钥。使用JDBC JSP自动生成密钥。我得到的错误为out.print(rs.next())=false和 rs.getInt(1)=…帮助我解决我的问题..我是jsp的新手 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.sql.*" %>

我想创建自动生成的密钥。使用JDBC JSP自动生成密钥。我得到的错误为out.print(rs.next())=false和 rs.getInt(1)=…帮助我解决我的问题..我是jsp的新手

   <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@page import="java.sql.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <%
             Connection con = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.
                    getConnection("jdbc:mysql://localhost:3306/test?useSSL=false","root","root");  
                String query = "insert into emp2(name,department,salary) values (?,?,?)";
                pstmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
                pstmt.setString(1, "John");
                pstmt.setString(2, "Acc Dept");
                pstmt.setString(3, "10000");
                pstmt.executeUpdate();
                rs = pstmt.getGeneratedKeys();
               out.print(rs.next());
               rs.next();
                    %>
                 <p>Generated Emp Id: <%=rs.getInt(1)%> </p>
                <%
            } 
            catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                try{
                    if(rs!= null) rs.close();
                    if(pstmt!= null) pstmt.close();
                    if(con!= null) con.close();
                } catch(Exception ex){}
            }
    %>
    </body>
    </html>

在此处插入标题
生成的Emp Id:

尝试从代码中删除。打印(rs.next())

<%
         Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.
                getConnection("jdbc:mysql://localhost:3306/test?useSSL=false","root","root");  
            String query = "insert into emp2(name,department,salary) values (?,?,?)";
            pstmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
            pstmt.setString(1, "John");
            pstmt.setString(2, "Acc Dept");
            pstmt.setString(3, "10000");
            pstmt.executeUpdate();
            rs = pstmt.getGeneratedKeys();
          // out.print(rs.next());
           rs.next();
                %>
             <p>Generated Emp Id: <%=rs.getInt(1)%> </p>
            <%
        } 
        catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            try{
                if(rs!= null) rs.close();
                if(pstmt!= null) pstmt.close();
                if(con!= null) con.close();
            } catch(Exception ex){}
        }
%>
</body>
</html>

生成的Emp Id:

如果你仍然面临这个问题,请浏览这篇文章

您两次调用rs.next(),请尝试使用comment out.print(rs.next());然后试试看