2012-04-24 26 views
5

Khi tôi chạy servlet sau:ResultSet.getString (1) ném java.sql.SQLException: hoạt động không hợp lệ tại vị trí con trỏ hiện tại

// package projectcodes; 
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { 
    String UserID = request.getParameter("UserID"); 
    String UserPassword = request.getParameter("UserPassword"); 
    String userName = null; 
    String Email = null; 
    Encrypter encrypter = new Encrypter(); 
    String hashedPassword = null; 
    try { 
     hashedPassword = encrypter.hashPassword(UserPassword); 
     Context context = new InitialContext(); 
     DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/photog"); 
     Connection connection = ds.getConnection(); 
     String sqlStatement = "SELECT email,firstname FROM registrationinformation WHERE password='" + hashedPassword + "'"; 
     PreparedStatement statement = connection.prepareStatement(sqlStatement); 
     ResultSet set = statement.executeQuery(); 
     userName = set.getString(1); // <<---------- Line number 28 
     response.sendRedirect("portfolio_one.jsp"); 
     // userName = set.getString("FirstName"); 
     Email = set.getString(3); 
     if(set.wasNull() || Email.compareTo(UserID) != 0) { 
      // turn to the error page 
      response.sendRedirect("LoginFailure.jsp"); 
     } else { 
      // start the session and take to his homepage 
      HttpSession session = request.getSession(); 
      session.setAttribute("UserName", userName); 
      session.setMaxInactiveInterval(900); // If the request doesn't come withing 900 seconds the server will invalidate the session 
      RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp"); 
      rd.forward(request, response); // forward to the user home-page 
     } 
    }catch(Exception exc) { 
     System.out.println(exc); 
    } 

tôi nhận được ngoại lệ sau:

INFO: java.sql.SQLException: Invalid operation at current cursor position. 
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source) 
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source) 
at org.apache.derby.client.am.ResultSet.getString(Unknown Source) 
at com.sun.gjc.spi.base.ResultSetWrapper.getString(ResultSetWrapper.java:155) 

-----> at projectcodes.ValidateDataForSignIn.doPost(ValidateDataForSignIn.java:28 

at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) 
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
at java.lang.Thread.run(Thread.java:722) 
    Caused by: org.apache.derby.client.am.SqlException: Invalid operation at current cursor position. 
at org.apache.derby.client.am.ResultSet.checkForValidCursorPosition(Unknown Source) 
at org.apache.derby.client.am.ResultSet.checkGetterPreconditions(Unknown Source) 
... 30 more 

Các nhật ký ở trên từ máy chủ cho thấy dòng số 28 là nguyên nhân của ngoại lệ. Nhưng tôi không thể có được lý do ngoại lệ. Tất cả các cột trong bảng có một kiểu dữ liệu của varchar.

Tôi đã đánh dấu số dòng 28 (nguyên nhân ngoại lệ theo nhật ký máy chủ) trong mã servlet.

+0

nhấn mạnh sự ngoại lệ trong server log –

Trả lời

28

Trước hết, bạn nên sử dụng câu hỏi next.

ResultSet set = statement.executeQuery(); 
if (set.next()) { 
    userName = set.getString(1); 
    //your logic... 
} 

CẬP NHẬT

Như Java 6 Tài liệu nói

Một ResultSet con trỏ ban đầu được vị trí trước khi dòng đầu tiên; cuộc gọi đầu tiên đến phương thức tiếp theo sẽ làm cho hàng đầu tiên của hàng hiện tại; cuộc gọi thứ hai làm cho hàng thứ hai là hàng hiện tại, v.v.

này có nghĩa là khi bạn thực hiện câu

ResultSet set = statement.executeQuery(); 

Các ResultSet set sẽ được tạo ra và chỉ vào một hàng trước khi kết quả đầu tiên của dữ liệu. Bạn có thể nhìn nó theo cách này:

CHỌN email, firstname TỪ registrationinformation

email    | firstname 
    ____________________________________ 
0          <= set points to here 
1 [email protected] | Email1 Person 
2 [email protected]  | Foo Bar 

Vì vậy, sau khi khai mạc ResulSet của bạn, bạn thực hiện phương pháp next để di chuyển nó vào hàng đầu tiên.

if(set.next()) 

Bây giờ set trông như thế này.

email    | firstname 
    ____________________________________ 
0 
1 [email protected] | Email1 Person <= set points to here 
2 [email protected]  | Foo Bar 

Nếu bạn cần phải đọc tất cả các dữ liệu trong ResultSet, bạn nên sử dụng một thời gian thay vì nếu:

while(set.next()) { 
    //read data from the actual row 
    //automatically will try to forward 1 row 
} 

Nếu set.next() trở lại sai, nó có nghĩa là không có hàng để đọc , vì vậy vòng lặp while của bạn sẽ kết thúc.

Thông tin thêm here.

+0

'set.next() 'di chuyển con trỏ hàng 1 về phía trước. Tôi muốn xem dữ liệu có trong bộ sau khi tôi đã thực hiện truy vấn để trích xuất dữ liệu từ một ô cụ thể. Làm thế nào để làm điều đó ? –

+0

Mặc dù nó hoạt động, bạn có thể giải thích 'set.next()' làm gì? Tôi không hiểu từ Doc. Trước 'set.next()' con trỏ được định vị ở đâu? –

+0

Sau khi thực hiện truy vấn yêu cầu lấy tên và email của người thuộc hàng thứ hai, vị trí đặt giờ sẽ là vị trí nào? Nó vẫn sẽ là hàng _0th_? –

1

Bạn phải đặt con trỏ đến đúng vị trí:

while(set.hasNext()){ 
    set.next(); 
    String a = set.getString(1); 
    String b = set.getString(2); 
} 
+0

'set.next()' di chuyển con trỏ 1 hàng về phía trước. Tôi muốn xem các dữ liệu đó là có trong bộ sau khi tôi đã thực hiện các truy vấn để trích xuất dữ liệu từ một tế bào cụ thể. Làm thế nào để tôi làm điều đó? –

0

ResultSet set = statement.executeQuery();

Lặp lại tập hợp và sau đó nhận chuỗi.

 while(set.next()) { 
me = set.getString(1); // <<---------- Line number 28 

} 
+0

'set.next()' di chuyển con trỏ 1 hàng về phía trước. Tôi muốn xem dữ liệu có trong bộ sau khi tôi đã thực hiện truy vấn để trích xuất dữ liệu từ một ô cụ thể. Làm thế nào để làm điều đó ? –

0

sau khi khởi tạo ResultSet kiểm tra xem con trỏ có hàng trong đó hay không, ví dụ:

if(rs.next()){ //your all other works should go here }

Các vấn đề liên quan