1 使用PreparedStatement语句求(账号)跟(密码)的设计
@Test
public void tast1() throws Exception {
Connection conn = JdbcUtil.INSTANCE.getConn();
String sql = "SELECT * FROM t_user where username = ? and password = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "admin"); //输入账号admin
ps.setString(2, "1"); //输入密码1
ResultSet rs = ps.executeQuery();
if(rs.next()){
System.out.println("ok啦....");
}else{
System.out.println("nook啦....");
}
JdbcUtil.INSTANCE.close(conn, ps, rs);
}
2 使用Statement语句求(账号)跟(密码)的设计
@Test
public void tast1_1() throws Exception {
Connection conn = JdbcUtil.INSTANCE.getConn();
Statement st = conn.createStatement();
//直接输入账号admin 跟 密码1
String sql = "SELECT * FROM t_user where username = 'admin' and password = '1'";
ResultSet rs = st.executeQuery(sql);
if(rs.next()){
System.out.println("ok啦....");
}else{
System.out.println("nook啦....");
}
JdbcUtil.INSTANCE.close(conn, st, rs);
}
3 使用PreparedStatement中 count(*) 列的设计
@Test
public void tast2() throws Exception {
Connection conn = JdbcUtil.INSTANCE.getConn();
String sql = "SELECT count(*) FROM t_user where username = ? and password = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "admin");
ps.setString(2, "1");
ResultSet rs = ps.executeQuery();
if(rs.next()){
Long i = rs.getLong(1);
if(i == 1){
System.out.println("ok啦....");//此时查找出来的count(*)列的参数为1密码正确
}else{
System.out.println("nook啦....");//此时查询出来的count(*)参数为0,密码不正确
}
}
JdbcUtil.INSTANCE.close(conn, ps, rs);
}
4 使用PreparedStatement 告诉(账号)求(密码)列的设计
@Test
public void tast3() throws Exception {
Connection conn = JdbcUtil.INSTANCE.getConn();
String sql = "SELECT password FROM t_user where username = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "admin"); //此处输入账号
ResultSet rs = ps.executeQuery();
if(rs.next()){
String pwd = rs.getString(1);
if(pwd.equals("1")){ //此处输入密码
System.out.println("ok啦....");//密码正确
}else{
System.out.println("错误啦....");//密码错误
}
}else{
System.out.println("没有该账号");
}
JdbcUtil.INSTANCE.close(conn, ps, rs);
}