package DAO;
import java.sql.*;
import entity.student;
public class studentDAO {
public student login(String username, String password) {
try {
new com.mysql.jdbc.Driver();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select * from Student where username = ? and password = ?";
try {
conn = java.sql.DriverManager.getConnection(
"jdbc:mysql://localhost:3306/oltest", "root", "4873617");
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs != null && rs.next()) {
// 读到数据,生成实体类
student student = new student();
student.setUsername(rs.getString("username"));
student.setPassword(rs.getString("password"));
return student;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args){
student s = new studentDAO().login("zncuxt","4873617");
System.out.println(s.getUsername());
}
}