0

我有一个 java web 项目。我在 Sql Server 2008 中有数据库,其中包含人员客户表。我将值(用户名、密码、角色)插入到人员表中,然后获取生成的 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();我是初学者所以谁能告诉我如何解决这个问题?

4

1 回答 1

2

您使用的 Jdbc 驱动程序不支持此方法。广泛使用的 MS SQL Server JDBC 驱动程序 - jTDS支持检索生成的密钥。是在类似任务中使用此驱动程序的示例。

要使用不同的驱动程序更改这些行:

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");
于 2012-12-17T20:14:04.447 回答