2014-11-07 2 views
0

код не позволяет мне войти в систему из базы данных:Ошибки: java.sql.SQLException: [Microsoft] [ODBC Microsoft Access Driver] Несоответствие типов данных в выражении критериев

 package Business; 

import java.io.Serializable; 
import java.sql.*; 


public class Student implements Serializable { 

    private int iD; 
    private String password; 
    private String firstName; 
    private String lastName; 
    private String street; 
    private String city; 
    private String state; 
    private double zip; 
    private String eMail; 
    private double gpa; 
    private String message; 

    /** 
    * No arg constructor that sets the default value of all 
    * customer properties to an empty string value. 
    */ 
    public Student() { 
     this.iD = 0; 
     this.password = ""; 
     this.firstName = ""; 
     this.lastName = ""; 
     this.street = ""; 
     this.city = ""; 
     this.state = ""; 
     this.zip = 0; 
     this.eMail = ""; 
     this.gpa = 0; 
    } 


    public void setID(int id) { 
     this.iD = iD; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 


    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 


    public void SetLastName(String lastName) { 
     this.lastName = lastName; 
    } 


    public void setStreet(String street) { 
     this.street = street; 
    } 

    public void setCity(String city) { 
     this.city = city; 
    } 

     public void setState(String state) { 
     this.state = state; 
    } 

     public void setZip(double zip) { 
     this.zip = zip; 
    } 

      public void setEMail (String email) { 
     this.eMail = email; 
    } 
      public void setGpa(double gpa) { 
     this.gpa = gpa; 
    } 

    public int getID() { 
     return iD; 
    } 


    public String getPassword() { 
     return password; 
    } 


    public String getFirstName() { 
     return firstName; 
    } 


    public String getLastName() { 
     return lastName; 
    } 


    public String getStreet() { 
     return street; 
    } 

    public String getCity() { 
     return city; 
    } 
    public String getState() { 
     return state; 
    } 

    public double getZip() { 
     return zip; 
    } 
     public String getEMail() { 
     return eMail; 
    } 

    public double getGpa() { 
     return gpa; 
    } 
    public String getMessage() { 
     return this.message; 
    } 


    public boolean login(String password) throws SQLException { 
     boolean yes_no = false; 

     /* Verify password against database password for the userId */ 
     if (password.equals(this.password)) { 
      this.message = "Thank you, you are logged in."; 
      yes_no = true; 
     } else { 
      this.message = "Sorry Your Login Information seems to be incorrect."; 
      yes_no = false; 
     } 

     /* Returns true or false based on the condition statement above */ 
     return yes_no; 
    } 

    /** 
    * Establishes connection with the database containing the customer information 
    * @return Connection 
    */ 
    public Connection studentConnect() { 
     try { 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     } catch (ClassNotFoundException e) { 
      System.out.println("Errors: " + e); 
     } 

     Connection connect = null; 
     try { 
      connect = DriverManager.getConnection("jdbc:odbc:RegisDB"); 
     } catch (SQLException e) { 
      System.out.println("You have Errors: " + e); 
     } 

     return connect; 
    } 

    /** 
    * Takes the student Id and retrieves the information from the customers 
    * data table and stores it in the properties of the customer object. 
    * @param iD 
    * @throws SQLException 
    */ 
    public void findDB(int iD) { 

     Connection con = studentConnect(); 
     Statement statement = null; 
     ResultSet result = null; 

     String sql = "Select * From Students Where iD = " + iD + ";"; 

     try { 
      statement = con.createStatement(); 
      result = statement.executeQuery(sql); 
      while (result.next()) { 
       this.iD = result.getInt("iD"); 
       this.password = result.getString("password"); 
       this.firstName = result.getString("firstName"); 
       this.lastName = result.getString("lastName"); 
       this.street = result.getString("street"); 
       this.city = result.getString("city"); 
       this.state = result.getString("state"); 
       this.zip = result.getDouble("zip"); 
       this.eMail = result.getString("eMail"); 
       this.gpa = result.getDouble("gpa"); 
      } 
      con.close(); 

     } catch (SQLException e) { 
      System.out.println("Errors: " + e); 
     } 
    } 

    /** 
    * Takes in the parameters of the customer database and inserts the parameters taken into 
    * the database for the addition of customers to the database. 
     * @param password 
    * @param firstName 
    * @param lastName 
    * @param street 
    * @param city 
    * @param state 
     * @param zip 
    * @param email 
    * @param gpa 
    * @throws SQLException 
    */ 
    public void insertDB(String password, String firstName, String lastName, String street, String city, String state, double zip, String email, double gpa) throws SQLException { 
     Connection con = studentConnect(); 
     Statement statement; 
     ResultSet result; 
     int resultVal; 
     String sql = "INSERT INTO Students (password, firstName, lastName, street, city, state, zip, eMail, gpa) VALUES ('" + password + "','" + firstName + "','" + lastName + "','" + street + "','" + city +"','" + state +"','" + zip + "','" + eMail +"','" + gpa + "');"; 

     try { 
      statement = con.createStatement(); 
      resultVal = statement.executeUpdate(sql); 

      System.out.println(resultVal); 
     } catch (SQLException e) { 
      System.out.println("Error: " + e); 
      System.out.println(e.getStackTrace()); 
     } finally { 
      con.close(); 
     } 
    } 

    /** 
    * Takes in student id and locates the customer in the database, performs 
    * an sql update and deletes the customer from the database. 
    * @param iD 
    * @throws SQLException 
    */ 
    public void deleteDB(int iD) throws SQLException { 
     Connection con = studentConnect(); 
     Statement statement; 
     int resultVal; 

     String sql = "DELETE FROM Students WHERE iD = " + iD + "';"; 

     try { 
      statement = con.createStatement(); 
      resultVal = statement.executeUpdate(sql); 
      System.out.println(resultVal); 
     } catch (SQLException e) { 
      System.out.println("Errors: " + e); 
     } finally { 
      con.close(); 
     } 
    } 
} 

Я получаю Страница ошибки однажды пытаюсь войти в систему, а затем Ошибки: java.sql.SQLException: [Microsoft] [ODBC Microsoft Access Driver] Несоответствие типов данных в выражении критериев.

погрешность.

Вот мой сервлет:

@WebServlet(urlPatterns = {"/StudentLoginServlet"}) 
public class StudentLoginServlet extends HttpServlet { 

    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
    * methods. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     try (PrintWriter out = response.getWriter()) { 
      /* TODO output your page here. You may use following sample code. */ 

      String iD = request.getParameter("iD"); 
      String pw = request.getParameter("pw"); 
      System.out.println(iD); 
      System.out.println(pw); 


      Student s1=new Student(); 
      s1.findDB(iD); 


      String fn=s1.getFirstName(); 
      System.out.println(fn); 

      String ln=s1.getLastName(); 
      System.out.println(ln); 

      String pwdb=s1.getPassword(); 
      System.out.println(pwdb); 
      RequestDispatcher rd; 

      HttpSession ses1=request.getSession(); 
      ses1.setAttribute("s1", s1); 

      if (pw.equals(pwdb)){ 
       rd= request.getRequestDispatcher("/ViewStudentSchedule.jsp"); 
       rd.forward(request,response); 

      } 
      else{ 
       rd= request.getRequestDispatcher("/LoginError.jsp"); 
       rd.forward(request,response); 

      } 

     } 
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
    /** 
    * Handles the HTTP <code>GET</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Handles the HTTP <code>POST</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Returns a short description of the servlet. 
    * 
    * @return a String containing servlet description 
    */ 

    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    }// </editor-fold> 

} 

Sidenote:

В таблице называется «Студенты» в базе данных и я доступ к следующим (правильный корпус /, чтобы в базе данных) :

ID Пароль FirstName LastName Street City S Тейт Zip EMail GPA

+0

Это не то, что вы просили, но я думаю, вы должны прочитать об атаках SQL-инъекций, прежде чем делать слишком много кодирования. –

ответ

0

Эта ошибка говорит о том, что iD столбца в базе данных является то, что числовым; но вы пытаетесь сравнить его с значением String. Вам нужно изменить свою базу данных так, чтобы столбец iD был чем-то вроде varchar или text.

0

Удалить одиночные кавычки из запроса, как, например

Строка SQL = "SELECT * FROM Студенты Где Ид =" + Ид + ";";

+0

исправил это, но есть еще ошибки, спасибо ~! повторно просматривая код – helpmeimnoob

+0

Могу ли я иметь структуру вашего стола. И вы используете двойной тип для Id, может быть, он находится в int datatype – Kandy

+0

http://puu.sh/cGvGN/6d29084aec.png все это подделка, для школы lol – helpmeimnoob

Смежные вопросы