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