我有一个Java Web项目。我在Sql Server 2008中有一个具有表person和customer的数据库。我正在向表person中插入值(用户名,paswword,role),然后将生成的id值'pid'插入其他表中属性表客户。这是我的代码`/ *
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();
我是初学者,所以谁能告诉我如何解决这个问题? 最佳答案
您正在使用的Jdbc驱动程序不支持此方法。广泛使用的MS SQL Server JDBC驱动程序-jTDS支持检索生成的密钥。 Here是在类似任务中使用此驱动程序的示例。
要使用其他驱动程序,请更改以下行:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:OnlineShopping");
如果是jTDS,它将是这样的:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/mydb","login","password");