数据库部分代码如下:
- Java code
import java.sql.*;
public class Student_mysql {
private Connection connection;
private Statement statement;
public boolean getStudent_welcome(String sql, String str1, String str2) ////传入sql语句字符串
throws SQLException, ClassNotFoundException {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Success loading Mysql Driver!");
} catch (Exception e) {
System.out.println("Error loading Mysql Driver!");
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3306/myschool";
String username = "root";
String password = "123";
connection = DriverManager.getConnection(url, username, password);
System.out.println("Success connect!");
System.out.println(sql);
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) { //不执行,直接跳过到connection.close();return false;了,为啥?
System.out.println(str1);
try {
if (str1 == resultSet.getString(1)
&& str2 == resultSet.getString(2)){
connection.close();
return true;
}
else
connection.close();
return false;
} catch (Exception e) {
e.printStackTrace();
connection.close();
return false;
}
}
connection.close();
return false;
}
}
具体问题就是这样~~求助!!!
------解决方案--------------------
拿你的这个sql语句到数据库里查询一下,看看sql语句有没有语法错误。
正常情况下,如果你的sql有语法错误,上面的这个方法会抛出异常,打印出来的,如果没有抛异常,就是查询条件下没有找到结果,但sql语句没错。
------解决方案--------------------
看来 你的sql语句问题了 你这样写试试
String sqlString ="select id,password from student_welcome " +
"where id = " + s.getIdString() + " and password= '" + s.getPasswordString()+"'";