2016-07-07 98 views
0

在主項目類中調用此方法時,填寫參數等,我收到一個錯誤。表中的字段列爲ID,我正在從用戶字段接收用戶輸入,並將其與SQL表中的ID字段進行比較。如果來自用戶的輸入與SQL表中的ID字段和PASSWORD字段相匹配,則應將用戶發送到主GUI屏幕。然而,這種情況並非如此。連接認證失敗JDBC

錯誤:

run: java.sql.SQLSyntaxErrorException: Comparisons between 'INTEGER' and 'CHAR (UCS_BASIC)' are not supported. Types must be comparable. String types must also have matching collation. If collation does not match, a possible solution is to cast operands to force them to the default collation (e.g. SELECT tablename FROM sys.systables WHERE CAST(tablename AS VARCHAR(128)) = 'T1') 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.Statement.executeQuery(Unknown Source) at GUI.Login_Check.CoLogin(Login_Check.java:27) at GUI.Login.btnSubmitActionPerformed(Login.java:208) at GUI.Login.access$200(Login.java:17) at GUI.Login$3.actionPerformed(Login.java:110) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6525) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6290) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4881) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) Caused by: org.apache.derby.client.am.SqlException: Comparisons between 'INTEGER' and 'CHAR (UCS_BASIC)' are not supported. Types must be comparable. String types must also have matching collation. If collation does not match, a possible solution is to cast operands to force them to the default collation (e.g. SELECT tablename FROM sys.systables WHERE CAST(tablename AS VARCHAR(128)) = 'T1') at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source) at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source) at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source) at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source) at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source) at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source) at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source) at org.apache.derby.client.am.Statement.flowExecute(Unknown Source) at org.apache.derby.client.am.Statement.executeQueryX(Unknown Source) ... 41 more

代碼:

public static void CoLogin(String username, String password){ 
    try{ 
     String dbuser = ""; 
     String dbpass = ""; 
     String url = "jdbc:derby://localhost:1527/Java-App"; 
     String user = username; 
     String pass = password; 
     Connection con = DriverManager.getConnection(url, "admin", "admin"); 

     Statement stmt = con.createStatement(); 
     String SQL = "SELECT * FROM MEMBERS WHERE ID='" + user + "' && PASSWORD='" + pass + "'"; 
     ResultSet rs = stmt.executeQuery(SQL); 

     while(rs.next()){ 
      dbuser= rs.getString("ID"); 
      dbpass = rs.getString("PASSWORD"); 
     } 
     if(user.equals(dbuser) && pass.equals(dbpass)){ 
       Main x = new Main(); 
       x.setVisible(true); 
     } 
     else{ 
      Login x = new Login(); 
      x.setVisible(true); 
     } 

     //Connection con = DriverManager.getConnection(url, user,  pass); 

    } 
    catch(SQLException error){ 
     System.out.println(error.getMessage()); 
    } 
+0

您的錯誤描述「但是,情況並非如此」太含糊。更深入地挖掘問題根源的一個好方法是調試。檢查rs.next()是否至少返回一次「true」。檢查由rs.getString(「ID」)和rs.getString(「PASSWORD」)返回的值。檢查系統錯誤日誌中是否沒有錯誤。這將縮小根本問題的範圍,即使您無法理解其原因,請再次返回結果,我們可以提供幫助。 –

+0

'&&'甚至可以工作嗎?我本來期望它是'AND'。您還應該使用參數化查詢,而不是將值直接轉儲到查詢字符串中以避免SQL注入。 – JonK

+0

@RobertoLinares我添加了一個sout來打印rs.getString的值,它只是返回發生相同的連接認證失敗。所以它基本上直接到了catch語句。 –

回答

0

你的問題可能有很多原因。

但是,如果用戶鍵入username =「」,password =「」他將在任何情況下進行身份驗證。還介意SQL injection

1

根據你的日誌,你的錯誤的根本原因是在momment您建立與數據庫的連接,在日線:

Connection con = DriverManager.getConnection(url, "admin", "admin"); 

訊息話題Connection authentication failure occurred. Reason: Userid or password invalid.告訴你,「管理員」錯誤用戶或passowrd用於連接到您的德比數據庫是不正確的。

你可以做的是,使用驗證的數據連接到數據庫,檢查他們在像數據源資源管理器在Eclpise或數據庫資源管理器在Netbeans的數據庫客戶端之前。一旦確定數據庫的驗證數據是正確的,請在代碼中使用它。

+0

此外,有關Derby如何實現連接身份驗證的背景信息,請參閱手冊中的身份驗證章節:https://db.apache.org/derby/docs/10.12/security/ –

+0

謝謝,它是大寫的admin 。但是我遇到了另一個錯誤。 –

+0

好吧,這個錯誤似乎與'MEMBERS'表中的數據類型有關。在你的查詢中,你假設'ID'和'PASSWORD'字段的類型是'VARCHAR',但是錯誤表明其中一個實際上是'INTEGER'類型,所以查詢中的比較無效。再次檢查數據類型,我也建議你先在數據庫客戶端嘗試查詢。 –