|
|
@@ -0,0 +1,167 @@
|
|
|
+package com.sf.dao.impl;
|
|
|
+
|
|
|
+import com.sf.dao.IUserDAO;
|
|
|
+import com.sf.domain.User;
|
|
|
+
|
|
|
+import java.sql.*;
|
|
|
+
|
|
|
+public class UserDAOImpl implements IUserDAO {
|
|
|
+ @Override
|
|
|
+ public User queryUserByUsernameAndPassword(String username, String password) {
|
|
|
+ // 1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement= null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 2 获取连接对象
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/login_demo", "root", "admin");
|
|
|
+ // 3 创建语句对象
|
|
|
+ String sql = "select * from user where username = ? and password = ?";
|
|
|
+ preparedStatement = connection.prepareStatement(sql);
|
|
|
+ preparedStatement.setString(1, username);
|
|
|
+ preparedStatement.setString(2, password);
|
|
|
+ // 4 执行sql 语句
|
|
|
+ // resultSet 是一个结果集
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ // 我们需要把结果集当中数据封装到User 当中
|
|
|
+ if(resultSet.next()){
|
|
|
+ User user = new User();
|
|
|
+ user.setId(resultSet.getLong("id"));
|
|
|
+ user.setUsername(resultSet.getString("username"));
|
|
|
+ user.setPassword(resultSet.getString("password"));
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ // 5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ // 把上面三个关闭资源
|
|
|
+ if(resultSet!=null){
|
|
|
+ try {
|
|
|
+ resultSet.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(preparedStatement!=null){
|
|
|
+ try {
|
|
|
+ preparedStatement.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(connection!=null){
|
|
|
+ try {
|
|
|
+ connection.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public User queryUserByUsername(String username) {
|
|
|
+ // 1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement= null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 2 获取连接对象
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/login_demo", "root", "admin");
|
|
|
+ // 3 创建语句对象
|
|
|
+ String sql = "select * from user where username = ?";
|
|
|
+ preparedStatement = connection.prepareStatement(sql);
|
|
|
+ preparedStatement.setString(1, username);
|
|
|
+ // 4 执行sql 语句
|
|
|
+ // resultSet 是一个结果集
|
|
|
+ resultSet = preparedStatement.executeQuery();
|
|
|
+ // 我们需要把结果集当中数据封装到User 当中
|
|
|
+ if(resultSet.next()){
|
|
|
+ User user = new User();
|
|
|
+ user.setId(resultSet.getLong("id"));
|
|
|
+ user.setUsername(resultSet.getString("username"));
|
|
|
+ user.setPassword(resultSet.getString("password"));
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ // 5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ // 把上面三个关闭资源
|
|
|
+ if(resultSet!=null){
|
|
|
+ try {
|
|
|
+ resultSet.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(preparedStatement!=null){
|
|
|
+ try {
|
|
|
+ preparedStatement.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(connection!=null){
|
|
|
+ try {
|
|
|
+ connection.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addUser(User user) {
|
|
|
+ // 1 加载驱动
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement preparedStatement= null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 2 获取连接对象
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/login_demo", "root", "admin");
|
|
|
+ // 3 创建语句对象
|
|
|
+ String sql = "insert into user (username,password) values(?,?)";
|
|
|
+ preparedStatement = connection.prepareStatement(sql);
|
|
|
+ preparedStatement.setString(1, user.getUsername());
|
|
|
+ preparedStatement.setString(2, user.getPassword());
|
|
|
+ // 4 执行sql 语句
|
|
|
+ // resultSet 是一个结果集
|
|
|
+ preparedStatement.executeUpdate();
|
|
|
+ // 5 释放资源
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }finally {
|
|
|
+ // 把上面三个关闭资源
|
|
|
+ if(resultSet!=null){
|
|
|
+ try {
|
|
|
+ resultSet.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(preparedStatement!=null){
|
|
|
+ try {
|
|
|
+ preparedStatement.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(connection!=null){
|
|
|
+ try {
|
|
|
+ connection.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|