-1

I Want to create Autogenerated Key. Autogenerated Key with JDBC JSP.i am getting error as out.print(rs.next())= false and rs.getInt(1)= ..Help me to solve my problem ..i am freshers in 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>
Debsish Ghosh
  • 35
  • 1
  • 8

1 Answers1

0

try removing out.print(rs.next()) from the code

<%
         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>

if you are still facing the issue go through this post

post

Narendra
  • 127
  • 3
  • 11