简体   繁体   中英

How to getGeneratedKeys() of a table in java

I have a java web project.I have database in Sql Server 2008 with tables person and customer .I am inserting values(username ,paswword,role) in to table person and then take the generated id value 'pid' and insert this with other attributes into table customer .This is my code`/*

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

 String name=request.getParameter("name");  
 String email=request.getParameter("email");
 String house=request.getParameter("house");
 String street=request.getParameter("street");
 String city=request.getParameter("city");
 String state=request.getParameter("state");
 String phone = "6086492155";
 int pincode=Integer.parseInt(request.getParameter("zip"));
 String username=request.getParameter("username");
 String password=request.getParameter("password");

try{
   Connection con= getConnection(response);
   PreparedStatement ps =con.prepareStatement("insert into person (username,password,role) values(?,?,'normaluser')",Statement.RETURN_GENERATED_KEYS);
   ps.setString(1, username);
   ps.setString(2, password);
   int i=ps.executeUpdate();
   ResultSet rs = ps.getGeneratedKeys();
    if (rs.next()) {
    int pId = rs.getInt(1);

    }
   PreparedStatement ps2= con.prepareStatement("insert into customer (name,email,house,street,city,state,pincode,phone,pid) values(?,?,?,?,?,?,?,?,?,pId)");
    ps.setString(1, name);
   ps.setString(2, email); 
   ps.setString(3, house);
   ps.setString(4, street);
   ps.setString(5, city);
   ps.setString(6, state);
   ps.setInt(7, pincode);
   ps.setString(8, phone);
    int j=ps2.executeUpdate();
   out.println(i);

   con.close();
if(i>0)
{
    out.println("inserted");
}
else
{
 out.println("not inserted");   
}
}
catch(Exception e){
   out.println("not inserted"+e); 
}
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

public Connection getConnection (HttpServletResponse response){
     Connection con=null;
        try{
                   PrintWriter out = response.getWriter();

                try{

                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                 con=DriverManager.getConnection("jdbc:odbc:OnlineShopping");;                   
                }
                catch(Exception e){
                   out.println(e.getMessage()); 
                }

        }catch(Exception e){
        }
        return con;
        }    
}

But I am geting error java.lang.UnsupportedOperationException .I think error is due to this line ResultSet rs = ps.getGeneratedKeys(); I am a beginner so can anyone tell me how to solve this issue??

Jdbc driver you are using doesn't support this method. Widely used MS SQL Server JDBC driver - jTDS supports retrieval of generated keys. Here is example of using this driver in similar task.

To use different driver change these lines:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:OnlineShopping");

In case of jTDS it will be something like this:

Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/mydb","login","password");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM