总览
前言
在开始前记得导入jdbc包,使用Tomcat时,如发生找不到JDBC驱动的错误,请在Tomcat的配置lib文件夹下添加jdbc包。
如何使用Java连接MySQLl数据库可以查看
点击查看
正文
User对象与Dao
为简略操作,User对象仅创建三个属性
public class User {
private int id;
private String username;
private String password;
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(){
}
登录方法的数据库操作
public boolean selectByName(User user){
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
boolean s=false;
try{
connection = DBUtil.getConnection();
//3. 写sql语句
String sql = "select * from users where user_name = ?";
//4. 获得statement对象
statement = connection.prepareStatement(sql);
statement.setString(1,user.getUsername());
//5. 执行SQL语句,得到结果集
resultSet = statement.executeQuery();
//6. 处理结果集
while(resultSet.next()){
user.getPassword().equals(resultSet.getString(3));
s=true;
}
}catch(ClassNotFoundException e