连接验证失败JDBC

时间:2016-07-07 15:54:14

标签: java jdbc derby

在主项目类中调用此方法,填写参数等时,我收到错误。表中的字段列为ID,我从用户字段接收用户输入,将其与SQL表中的ID字段进行比较。如果用户的输入与SQL表中的ID字段和PASSWORD字段匹配,则应将用户发送到主GUI屏幕。但事实并非如此。

错误:

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());
    }

2 个答案:

答案 0 :(得分:1)

根据您的日志,您的错误的根本原因在于您建立与数据库的连接,如下所示:

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

错误消息Connection authentication failure occurred. Reason: Userid or password invalid.告诉您" admin"用于连接到derby数据库的用户或passowrd不正确。

在使用该身份验证数据连接到数据库之前,您可以做的是在数据库客户端中检查它们,例如Eclpise中的 Data Source Explorer Data Base Explorer 在Netbeans。一旦确定数据库的身份验证数据正确无误,请在代码中使用它。

答案 1 :(得分:0)

你的问题可能有很多原因。

但是,如果用户键入username =“”,则密码=“”他将在任何情况下进行身份验证。还要注意SQL injection