package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import entity.User;
import util.DBUtil;
public class LoginDao {
public User ulogin(String uname,String pwd) {
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
connection = DBUtil.getConnection();
String sql = "select * from shop_admin where username = ? and password = ?";
pstmt = connection.prepareStatement(sql);
pstmt.setString(1, uname);
pstmt.setString(2, pwd);
rs = pstmt.executeQuery();
User user = new User();
if(rs.next()) {
int id = rs.getInt(1);
String username = rs.getString(2);
String password = rs.getString(3);
String email = rs.getString(4);
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setEmail(email);
return user;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
finally {
DBUtil.closeAll(rs, pstmt, connection);
}
return null;
}
public static void main(String[] args) {
User user1 = new User();
LoginDao ldao = new LoginDao();
user1 = ldao.ulogin("hhb", "123");
System.out.println(user1);
}
}
user类:
package entity;
public class User {
private Integer id;
private String username;
private String password;
private String email;
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
}