- public class DBManager {
- public static final String DEFAULT_PWD = "123456";
- public static final String DEFAULT_UID = "root";
- public static final String DEFAULT_URL = "jdbc:mysql://localhost:3306/schoolforum?useSSL=false&serverTimezone=UTC";
- public static final String DEFAULT_DRIVER = "com.mysql.cj.jdbc.Driver";
-
- public static Connection getConn() {
- Connection con = null;
-
- try {
- Class.forName(DEFAULT_DRIVER); //加载驱动
- con = DriverManager.getConnection(DEFAULT_URL, DEFAULT_UID, DEFAULT_PWD); //获取连接
- } catch (ClassNotFoundException e) {
- System.out.println("数据库驱动加载失败");
- e.printStackTrace();
- } catch (SQLException e) {
- System.out.println("数据库连接失败");
- e.printStackTrace();
- }
-
- return con;
- }
-
- }
- //登录
- @SneakyThrows
- public User selectOneByUsernameAndPassword(String username,String password){
- String sql = "select uid, uName, uPwd, uImg, uSign, uTextNum from user where uName = ? and uPwd = ?";
- try (Connection c = DBManager.getConn()){
- System.out.println("connection success");
- try (PreparedStatement ps = c.prepareStatement(sql)){
- ps.setString(1,username);
- ps.setString(2,password);
-
- System.out.println("DEBUG: " + ps);
- try (ResultSet rs = ps.executeQuery()){
- if(!rs.next()){
- return null;
- }
-
- User user = new User();
- user.setUid(rs.getInt("uid"));
- user.setUserName(username);
- user.setUserPwd(password);
- user.setUserImg(rs.getString("uImg"));
- user.setUserSign(rs.getString("uSign"));
- user.setUserTextNum(rs.getInt("uTextNum"));
- return user;
- }
- }
- }
- }