81,092
社区成员
发帖
与我相关
我的任务
分享
public class Adminhe {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Admincheck {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
/**
* @param adminhe
* Adminhe对象
* @return true:此用户存在 false:无此用户
* @throws ClassNotFoundException
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public boolean checkNameAndPassword(Adminhe adminhe)
throws ClassNotFoundException, SQLException,
InstantiationException, IllegalAccessException {
System.out.println("待验证用户名:" + adminhe.getName() + "密码:"
+ adminhe.getPassword());
System.out
.println("======================我是分隔符===========================");
boolean flag = false;
// 注册驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
// 获取数据库连接
conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/admin?user=root&password=root");
// 获得Statement对象
stmt = conn.createStatement();
String sql = "SELECT*FROM adminin";
// 执行sql获得结果集
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("数据库name:" + rs.getString(1));
System.out.println("数据库password:" + rs.getString(2));
// 查找的记录中当此行的第一列String与name相同且第二列String与password相同时,表示此用户在数据库中存在
if (rs.getString(1).equals(adminhe.getName())
&& rs.getString(2).equals(adminhe.getPassword())) {
flag = true;
}
}
rs.close();
stmt.close();
conn.close();
System.out.println(flag == true ? "验证pass" : "无此用户");
return flag;
}
}
public class MyDAO{
/**
* 查询所有
* @return
* @throws SQLException
*/
public List findUser(String uName) throws SQLException
{
db=DbConnect.getInstance();
conn=db.getConnect();
sql="select * from User where uName="+uName;
ps =conn.prepareStatement(sql);
rs=ps.executeQuery();
list =new ArrayList();
while(rs.next())
{
User user=new User();
user.setUname(rs.getString("uname"));
user.setPassword(rs.getString("password"));
list.add(user);
}
rs.close();
ps.close();
conn.close();
return list;
}
}
public class MyJavaBean{
public String checkUser(String uName,Strng pwd){
List<User> uList=MyDAO.findUser(uName);
if(uList.size()<=0){return "用户名输入不正确"};
for(int i=0;i<uList.size();i++){
if(uList.get(i).getPassword.equals(pwd)){
return "该用户名可用";
} else{
return "密码错误";
}
}
}
}