我有一个基本代码,但它不起作用。我不知道为什么。我通过在每行之后打印来检查整个代码,但似乎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
}
答案 0 :(得分:1)
实际上,问题出现在带有大写字母的表格中! Postgres似乎区分大小写。所以,我用所有小写字母创建新表。它起作用了: - )
String searchQuery =
"SELECT * FROM users WHERE username='"
+ username
+ "' AND password='"
+ password
+ "'";
答案 1 :(得分:0)
String searchQuery =
"SELECT * FROM [SchemaNameGoesHere].Users WHERE username='"
+ username
+ "' AND password='"
+ password
+ "'";