import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.edu.ayit.dao.UserDao;
import cn.edu.ayit.pojo.User;
public class UserDaoImpl implements UserDao {
public User checkUserLoginDao(String uname, String pwd) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//声明返回变量
User u = null;
try {
//获取连接
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaee?useUnicode=true&characterEncoding=UTF-8", "root", "root");
//创建sql语句
String sql = "select * from t_userinfo where uname=? and pwd=?";
//创建sql命令对象
ps = conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1, uname);
ps.setString(2, pwd);
//执行sql
rs = ps.executeQuery();
//遍历结果
while(rs.next()){
u = new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
u.setAge(rs.getInt("age"));
u.setSex(rs.getString("sex"));
u.setBirth(rs.getString("birth"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//释放资源
if(null != rs){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(null != ps){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(null != conn){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return u;
}
}
使用的连接驱动版本是mysql-connector-java-5.0.8-bin.jar,数据库mysql版本是8.0以前的项目可以使用这个驱动正常连接且使用,但是这次连接一直报java.sql.SQLException: Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.异常.就算设置了字符集也不行,然而使用了针对mysql8.0的连接驱动同样异常,求大神解围.