0

基本的なコードはありますが、機能していません。どうしてか分かりません。各行の後に出力してコード全体をチェックしましたが、executequery が苦労しているようです。専門家からの助けが必要です..

{

    package com.pack.database.userinformation;

    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;

    /**
     *
     * @author Shokouh
     */
    class UserDAO {

         static Connection currentCon = null;
          static ResultSet rs = null;  



          public static SurveyData login(SurveyData bean) {

             //preparing some objects for connection 
             Statement stmt = null;    

             String username = bean.getUserName();    
             String password = bean.getPassword();   

             String searchQuery =
                   "SELECT * FROM userinfo WHERE username='"
                            + username
                            + "' AND password='"
                            + password
                            + "'";

          // "System.out.println" prints in the console; Normally used to trace the process
          System.out.println("Your user name is " + username);          
          System.out.println("Your password is " + password);
          System.out.println("Query: "+searchQuery);

          try 
          {
              System.out.println("testssssssssssssssssssssssss");
             //connect to DB 
             currentCon = ConnectionManager.getConnection();
             stmt=currentCon.createStatement();
              System.out.println("1111111111111111111111");
             rs = stmt.executeQuery(searchQuery);       
             System.out.println("kkkkkkkkkkkkkkkkkkk");
             boolean more = rs.next();
              System.out.println("222222222222222222");

             // if user does not exist set the isValid variable to false
             if (!more) 
             {
                System.out.println("Sorry, you are not a registered user! Please sign up first");
                bean.setValid(false);
             } 

             //if user exists set the isValid variable to true
             else if (more) 
             {

                String firstName = rs.getString("username");
                String lastName = rs.getString("lastname");

                System.out.println("Welcome " + firstName);
                bean.setFirstName(firstName);
                bean.setLastName(lastName);
                bean.setValid(true);
             }
          } 

          catch (Exception ex) 
          {
             System.out.println("Log In failed: An Exception has occurred! " + ex);
          } 

          //some exception handling
          finally 
          {
             if (rs != null)    {
                try {
                   rs.close();
                } catch (Exception e) {}
                   rs = null;
                }

             if (stmt != null) {
                try {
                   stmt.close();
                } catch (Exception e) {}
                   stmt = null;
                }

             if (currentCon != null) {
                try {
                   currentCon.close();
                } catch (Exception e) {
                }

                currentCon = null;
             }
          }

    return bean;

          }


    }

}

出力は次のとおりです。

{

        NFO:   test was successfully deployed in 1,146 milliseconds.
        INFO:   Your user name is m.rezai
        INFO:   Your password is mina123
        INFO:   Query: SELECT * FROM userinfo WHERE username='m.rezai' AND password='mina123'
    INFO:   testssssssssssssssssssssssss
    INFO:   1111111111111111111111
    INFO:   kkkkkkkkkkkkkkkkkkk
    INFO:   222222222222222222
    INFO:   Sorry, you are not a registered user! Please sign up first

}
4

2 に答える 2

0

実際、問題は大文字のテーブル名にありました! Postgres は大文字と小文字を区別しているようです。だから、すべて小文字で新しいテーブルを作成します。出来た :-)

String searchQuery =
           "SELECT * FROM users WHERE username='"
                    + username
                    + "' AND password='"
                    + password
                    + "'";
于 2015-07-29T07:19:54.920 に答える