实现用户注册与登入功能的案例

实现用户注册与登入功能的案例

LoginAndRegister类

  1 import java.sql.Connection;
  2 import java.sql.DriverManager;
  3 import java.sql.PreparedStatement;
  4 import java.sql.ResultSet;
  5 import java.sql.SQLException;
  6 import java.sql.Statement;
  7 import java.util.Calendar;
  8 import java.util.Date;
  9 
 10 import utils.JDBCUtil;
 11 
 12 public class LoginAndRegister {
 13     
 14     
 15     public static void main(String[] args) {
 16         Date date = new java.util.Date();
 17         Calendar calendar = Calendar.getInstance();
 18         calendar.set(1994, 8, 3);
 19         
 20         User user = new User();
 21         user.setId(2);
 22         user.setUsername("Jay");
 23         user.setPassword("123456");
 24         user.setNickname("Jay");
 25         user.setGender("女");
 26         user.setRegistdate(date);
 27         user.setBirthday(calendar.getTime());
 28         
 29         boolean regist = regist(user);
 30         if (regist) {
 31             System.out.println("注册成功!");
 32         } else {
 33             System.out.println("注册失败!");
 34         }
 35         
 36         boolean login = login("周娟娟", "123");
 37         if (login) {
 38             System.out.println("成功登入!");
 39         } else {
 40             System.out.println("登入失败!");
 41         }
 42     }
 43     
 44     /**功能: 登录
 45      * @param username
 46      * @param password
 47      * @return
 48      */
 49     public static boolean login(String username,String password) {
 50         Connection connection = null;
 51         PreparedStatement statement = null;
 52         ResultSet resultSet = null;
 53         try {
 54             connection = JDBCUtil.getConnection();
 55             
 56             // 查询用户名和密码是否正确
 57             String sql = "select * from userInfo where username = ? and password = ? ";
 58             statement = connection.prepareStatement(sql); // 这里会进行预编译,防止恶意的代码注入
 59             statement.setString(1, username); // 设置占位符?的值
 60             statement.setString(2, password); // 设置占位符?的值
 61             resultSet = statement.executeQuery(); // 执行sql语句
 62             if(resultSet.next()) {
 63                 return true;
 64             }
 65             return false;
 66         } catch (SQLException e) {
 67             e.printStackTrace();
 68         } finally {
 69             JDBCUtil.release(resultSet, statement, connection);
 70         }
 71         return false;
 72     }
 73     
 74     /**功能: 注册
 75      * @param user
 76      * @return
 77      */
 78     public static boolean regist(User user) {
 79         Connection connection = null;
 80         PreparedStatement statement = null;
 81         ResultSet resultSet = null;
 82         ResultSet isRegist = null;
 83         PreparedStatement statement2 = null;
 84         try {
 85             connection = JDBCUtil.getConnection();
 86             
 87             String sql = "insert into userInfo values(?, ?, ?, ?, ?, ?, ?)";
 88             statement = connection.prepareStatement(sql);
 89             statement.setInt(1, user.getId());
 90             statement.setString(2, user.getUsername());
 91             statement.setString(3, user.getPassword());
 92             statement.setString(4, user.getNickname());
 93             statement.setString(5, user.getGender());
 94             // 向数据库插入日期要转换成数据库的日期格式
 95             statement.setDate(6, new java.sql.Date(user.getRegistdate().getTime()));
 96             statement.setDate(7, new java.sql.Date(user.getBirthday().getTime()));
 97             
 98             // 查询数据库中是否有该用户名
 99             String sql2 = "select username from userInfo where username= ?";
100             statement2 = connection.prepareStatement(sql2);
101             statement2.setString(1, user.getUsername());
102             // 判断该用户名是否已被注册
103             isRegist = statement2.executeQuery();
104             if (isRegist.next()) {
105                 return false;
106             } else {
107                 int update = statement.executeUpdate();
108                 if (update > 0) {
109                     return true;
110                 } else {
111                     return false;
112                 }
113             }
114         } catch (SQLException e) {
115             e.printStackTrace();
116         } finally {
117             JDBCUtil.release(resultSet, statement, connection);
118             JDBCUtil.release(isRegist);
119             JDBCUtil.release(statement2);
120         }
121         return false;
122     }
123     
124 }
View Code

 

 

实现数据库连接和关闭资源的工具类

  1 import java.io.IOException;
  2 import java.io.InputStream;
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.sql.Statement;
  9 import java.util.Properties;
 10 
 11 public class JDBCUtil {
 12     
 13     private static final String DRIVER = "driver";
 14     private static final String URL = "url";
 15     private static final String USERNAME = "username";
 16     private static final String PASSWORD = "password";
 17     private static Properties properties = null;
 18     
 19     static {
 20         try {
 21             // 加载资源文件
 22             properties = new Properties();
 23             ClassLoader classLoader = JDBCUtil.class.getClassLoader();
 24             InputStream is = classLoader.getResourceAsStream("db.properties");
 25             properties.load(is);
 26             
 27             // 加载驱动
 28             String driver = properties.getProperty(DRIVER);
 29             Class.forName(driver);
 30         } catch (ClassNotFoundException e) {
 31             e.printStackTrace();
 32         } catch (IOException e) {
 33             e.printStackTrace();
 34         }
 35     }
 36     
 37     /**功能: 连接数据库
 38      * @return
 39      */
 40     public static Connection getConnection() {
 41         String url = properties.getProperty(URL);
 42         String username = properties.getProperty(USERNAME);
 43         String password = properties.getProperty(PASSWORD);
 44         Connection connection = null;
 45         try {
 46             connection = DriverManager.getConnection(url, username, password);
 47             return connection;
 48         } catch (SQLException e) {
 49             e.printStackTrace();
 50         }
 51         return null;
 52     }
 53     
 54     /**功能: 释放 ResultSet, Statement, Connection资源
 55      * @param resultSet
 56      * @param statement
 57      * @param connection
 58      */
 59     public static void release(ResultSet resultSet, Statement statement, Connection connection) {
 60         try {
 61             if (resultSet != null) {
 62                 resultSet.close();
 63             }
 64             if (statement != null) {
 65                 statement.close();
 66             }
 67             if (connection != null) {
 68                 connection.close();
 69             }
 70         } catch (SQLException e) {
 71             e.printStackTrace();
 72         }
 73     }
 74     
 75     /**功能: 释放ResultSet资源
 76      * @param resultSet
 77      */
 78     public static void release(ResultSet resultSet) {
 79         if (resultSet != null) {
 80             try {
 81                 resultSet.close();
 82             } catch (SQLException e) {
 83                 e.printStackTrace();
 84             }
 85         }
 86     }
 87     
 88     /**功能: 释放Statement资源
 89      * @param statement
 90      */
 91     public static void release(Statement statement) {
 92         if (statement != null) {
 93             try {
 94                 statement.close();
 95             } catch (SQLException e) {
 96                 e.printStackTrace();
 97             }
 98         }
 99     }
100     
101     /**功能: 释放Connection资源
102      * @param connection
103      */
104     public static void release(Connection connection) {
105         if (connection != null) {
106             try {
107                 connection.close();
108             } catch (SQLException e) {
109                 e.printStackTrace();
110             }
111         }
112     }
113     
114     /**功能: 释放PreparedStatement资源
115      * @param PreparedStatement
116      */
117     public static void release(PreparedStatement preparedStatement) {
118         if (preparedStatement != null) {
119             try {
120                 preparedStatement.close();
121             } catch (SQLException e) {
122                 e.printStackTrace();
123             }
124         }
125     }
126 
127 }
View Code

 

posted @ 2017-08-02 19:57  JaneBaby  阅读(440)  评论(0编辑  收藏  举报