try {
//链接字符串
String driver ="com.mysql.jdbc.Driver";
//加载驱动
Class.forName(driver);
//建立数据库连接
Connection con = (Connection) DriverManager.getConnection(url,user, dbpassword);
if(con==null){
System.out.println("can't open JDBConnection");
}
//sql语句
String sql = "select * from user.user where (username=? or email=? )and password=?";
PreparedStatement pstmt=(PreparedStatement) con.prepareStatement(sql);
pstmt.setString(1,this.Name); //第1个?字符的替换
pstmt.setString(2,this.Name); //第2个?字符的替换
pstmt.setString(3,this.Password);//第3个?字符的替换
//pstmt.setString(4,this.Password);//第4个?字符的替换
System.out.println(sql);
System.out.println(getName());
System.out.println(getPassword());
ResultSet rs=pstmt.executeQuery();//执行语句后,产生单个结果集的语句
if(rs.next())
{
System.out.println(rs.getString("username"));
System.out.println("PASS");
result=true;
}
else
{
System.out.println("FALL");
result=false;
}
pstmt.close();//3个关闭PreparedStatement,ResultSet,Connection
rs.close();
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
总结:测试的时候看看是不是测试的用户名写错了= =