私はJava Webプロジェクトを持っています.Sql Server 2008にテーブルpersonとcustomerを持つデータベースがあります.テーブルpersonに値(ユーザー名、パスワード、ロール)を挿入し、生成されたID値「pid」を取得して、これを他の属性をテーブルcustomerに追加します。これは私のコードです`/*
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;
}
}
しかし、エラーjava.lang.UnsupportedOperationExceptionが発生しています。エラーはこの行が原因だと思います。ResultSet rs = ps.getGeneratedKeys();
私は初心者なので、この問題の解決方法を教えてもらえますか??