在主項目類中調用此方法時,填寫參數等,我收到一個錯誤。表中的字段列爲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());
}
您的錯誤描述「但是,情況並非如此」太含糊。更深入地挖掘問題根源的一個好方法是調試。檢查rs.next()是否至少返回一次「true」。檢查由rs.getString(「ID」)和rs.getString(「PASSWORD」)返回的值。檢查系統錯誤日誌中是否沒有錯誤。這將縮小根本問題的範圍,即使您無法理解其原因,請再次返回結果,我們可以提供幫助。 –
'&&'甚至可以工作嗎?我本來期望它是'AND'。您還應該使用參數化查詢,而不是將值直接轉儲到查詢字符串中以避免SQL注入。 – JonK
@RobertoLinares我添加了一個sout來打印rs.getString的值,它只是返回發生相同的連接認證失敗。所以它基本上直接到了catch語句。 –