2012-02-07 44 views
-3

jsp文件,其中包含用戶名&在LoginServlet處理的密碼...此servlet在Access數據庫中執行驗證。但即時得到錯誤在Servlet中訪問數據庫

如果用戶名密碼&與Access數據庫則頁面重定向匹配flat_status.jsp

可以ANY1幫我..

package Validate; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.*; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

public class LoginServlet extends HttpServlet{ 

    @Override 
    public void init(ServletConfig config) throws ServletException 
    { 
     super.init(config); 
      try 
     { 
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     } 
     catch (Exception ex) 
     { 
      System.out.println("Initialize connector string"); 
     } 
     } 
    @Override 
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
       Connection conn; 
    Statement st; 
    ResultSet rs; 
    PrintWriter pw = response.getWriter(); 
     String userName = request.getParameter("username"); 
    String password = request.getParameter("password"); 
     HttpSession session = request.getSession(true); 
     response.setContentType("text/html"); 
      try 
      { 
       conn=DriverManager.getConnection("jdbc:odbc:FBS"); 
       st=conn.createStatement(); 
       rs=st.executeQuery("select UserName,Password from user_login where UserName='"+userName+"' and Password='"+password+"'"); 
       while(rs.next()) 
       { 
        session.setAttribute("USerName",userName); 
        RequestDispatcher dispatch=request.getRequestDispatcher("/flat_status.jsp"); 
        dispatch.forward(request, response); 
       } 
      st.close(); 
      rs.close(); 
      conn.close(); 
      } 
      catch (SQLException ex) 
      { 
        pw.println(ex); 
      } 
     } 

     } 

下面是異常的IM在瀏覽器中獲取窗口

java.sql.SQLException:[Microsoft] [ODBC Microsoft Access Driver]標準表達式中的數據類型不匹配。

+0

哪一行是LoginServlet的第58行?我的猜測是它是'out.println'(ex.getMessage());',因爲你寫出來了,它被初始化爲null。 – 2012-02-07 15:07:05

+0

@ ^^ ...這是什麼解決方案?當我刪除PrintWriter和使用System.out.println(ex.getMessage());它顯示空的servlet頁面後點擊提交按鈕 – Shaggy 2012-02-07 16:55:09

+1

它顯示你一個空的頁面,因爲你有一個例外。如果您不想要空白頁面,則向響應寫入程序輸出錯誤消息。 – 2012-02-07 17:21:41

回答

0

在Ms-Access中始終使用PreparedStatement和Password是reserved關鍵字。

boolean userFound=false; 

    conn=DriverManager.getConnection("jdbc:odbc:FBS"); 
    String sql="select [UserName],[Password] from user_login 
          where [UserName]=? and [Password]=?"; 
    PreparedStatement st=conn.prepareStatement(sql); 
    st.setString1(1,userName); 
    st.setString1(2,password); 
    rs=st.executeQuery(); 

    if(rs.next()) 
    { 
    userFound=true; 
    } 
    st.close(); 
    conn.close(); 

if(userFound) 
    { 
    } 
0

有兩件事情在我身上跳出來。

首先是在init,你應該只需要調用

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

無需額外 「的newInstance()」。事實上,如果您使用的是支持JDBC 4.0的JRE,則甚至不需要「Class.forName(...)」調用

第二個是您應該只需要雙斜槓即可 在路徑中正斜槓。嘗試將連接字符串更改爲:

conn=DriverManager.getConnection("jdbc:odbc:FBS={Microsoft Access Driver(*.accdb)};DBQ=C:\\Users\\Dumbre\\Documents\\NetBeansProjects\\Flat Booking System\\Flat_System.accdb;"); 

此外,您可能會嘗試切換到DSN,僅用於測試。創建系統DSN 和您的連接字符串應該是:

jdbc:odbc:TheNameForTheNewDSNThatYouJustCreated 

嗯,很明顯的是最後一部分必須是實際名稱。

+0

肯定會試一試,讓你知道.. !! – Shaggy 2012-02-07 16:44:52

+0

不能正常工作。 – Shaggy 2012-02-07 16:59:20