servlet给出了createSQLException

时间:2016-06-14 04:48:54

标签: java servlets jdbc

我在netbeans浏览器中执行时有以下代码显示空白屏幕,无法移动到目标页面

import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SignIn extends HttpServlet {



    public void init(ServletConfig config) throws ServletException {
        super.init(config);
       System.out.println("Signin Init");

    }

        public void destroy() {   }

       protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
    System.out.println("Signin process");

        System.out.println("name="+request.getParameter("uname"));
        System.out.println("Password="+request.getParameter("pwd"));

       name=request.getParameter("uname");
       password=request.getParameter("pwd");
       System.out.println("name="+name);
     System.out.println("Password="+password);
       //code here for the validating of the user and if validated send them to the
       //myAccoutn.jsp
       //else send to the accountError.jsp

    try{   
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection con=DriverManager.getConnection("jdbc:odbc:ERP");
         st=con.createStatement();
         System.out.println("---------------------in ConnectDB/n");
         System.out.println("-----------------before connect");
         System.out.println("-----------------after connect..."+con);
         System.out.println("-----------------create statemet");
         rs=st.executeQuery("select * from erplogin where name='"+name+"' and password='"+password+"'");
         System.out.println("-----------------qry exec");
         if(rs.next()){
        System.out.println("-----------------in If");
        int auth = rs.getInt("name");
System.out.println("-----------------getting auth"+auth);
       // session hass been created     
       session=request.getSession();
       session.setAttribute("uname", name);
       session.setAttribute("pwd",password);
       session.setAttribute("auth",new Integer(auth));

        /* TODO output your page here */
        response.sendRedirect("./MyAccount.jsp");

    }
else
      response.sendRedirect("./error/accountError.jsp");

    }catch(Exception e){
        e.printStackTrace();
    }


    }

            protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("Signin doGet");

        processRequest(request, response);
    }

          protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      System.out.println("Signin doPost");

        processRequest(request, response);
    }

          public String getServletInfo() {
        return "User login";
    }
    private String name="";
    private String password="";
    HttpSession session;
    private boolean boolVal;
    private Statement st;
    private ResultSet rs=null;
    private Connection conObj;
    private static int errorVal=0;

}

使用Exception执行以下执行

Signin Init
Signin doPost
Signin process
name=aaa
Password=sss
name=aaa
Password=sss
---------------------in ConnectDB/n
-----------------before connect
-----------------after connect...sun.jdbc.odbc.JdbcOdbcConnection@4260ab
-----------------create statemet
-----------------qry exec
-----------------in If
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(JdbcOdbc.java:3811)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(JdbcOdbcResultSet.java:5638)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:583)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:601)
at SignIn.processRequest(SignIn.java:61)
at SignIn.doPost(SignIn.java:102)

在第

int auth = rs.getInt("name");

   processRequest(request, response);

请帮我解决这些错误

2 个答案:

答案 0 :(得分:0)

当您使用不正确的索引读取结果集java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid descriptor index时,会获得

rs

    int auth = rs.getInt(3);

尝试使用正确的索引,列索引从1开始,依此类推。也许3不是列的索引。

答案 1 :(得分:0)

而不是直接使用索引,你应该使用列名来获取数据。例如:假设在第一个列名'studentId'的索引为0,那么你可以通过使用类似于int id = rs.getInt获得正确的答案( 0);但是稍后你的studentId列被更改为索引号2.然后当你访问int id = rs.getInt(0)然后你会得到错误的结果或错误。所以在rs.get中使用column-name是安全的()。例如:rs.getInt(“studentId”)。这意味着即使您的列名studentId更改为任何索引,您也可以正确访问数据。