public class UserDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
public User login(User loginUser){
try {
//1.编写sql
String sql ="select * from user WHERE username=? AND PASSWORD=?";
String username = loginUser.getUsername();
String password = loginUser.getPASSWORD();
System.out.println("DAO+"+username);
BeanPropertyRowMapper<User> BPR =new BeanPropertyRowMapper<User>(User.class);
//2.调用query方法
User user = template.queryForObject(sql, BPR,username,password);
// User user = new JdbcTemplate().queryForObject(sql,BPR,username,password);
System.out.println("UD"+user);
return user;
} catch (DataAccessException e) {
e.printStackTrace();//记录日志
return null;
}
}
}
报错了
java.sql.SQLException: com.mysql.cj.jdbc.Driver
和
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: com.mysql.cj.jdbc.Driver
at cn.itcast.dao.UserDao.login(UserDao.java:21)
at cn.itcast.Servlet.LoginServlet.doGet(LoginServlet.java:34)
大致上说就是那个template.queryForObject 和数据库没办法交流把 我觉得是