JDBC MVC框架實現用戶登錄


MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫

 

 

 

1、實體entity

 1 package com.uplooking.entity;
 2 
 3 import java.io.Serializable;
 4 
 5 public class User implements Serializable {
 6     private int id;
 7     private String name;
 8     private String pwd;
 9 
10 
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public String getPwd() {
24         return pwd;
25     }
26     public void setPwd(String pwd) {
27         this.pwd = pwd;
28     }
29 
30 
31 }

2、

 1 package com.uplooking.dao;
 2 
 3 import com.uplooking.entity.User;
 4 
 5 import java.sql.Connection;
 6 import java.sql.DriverManager;
 7 import java.sql.PreparedStatement;
 8 import java.sql.ResultSet;
 9 import java.sql.SQLException;
10 
11 public class UserDao {
12     public  User findByName(String name) {
13         PreparedStatement statement = null;
14         Connection con = null;
15         try {
16             //1、加載驅動
17             Class.forName("com.mysql.jdbc.Driver");
18             //2、創建連接
19             String sqlUrl = "jdbc:mysql://127.0.0.1:3307/test?useunicode=true&characterEncoding=utf8";
20             //Driver表示Java驅動的街口
21             con = DriverManager.getConnection(sqlUrl,"root","");
22 
23             //3、預編譯,使用占位符
24             String sql = "select * from user where name= ? ";
25 
26             //4、創建連接狀態
27             statement = con.prepareStatement(sql);
28 
29             //5、填充占位符      第1列
30             statement.setString(1, name);
31 
32             ResultSet set = statement.executeQuery();
33 
34             User user = null;
35             if (set.next()) {
36                 user = new User();
37                 user.setId(set.getInt("id"));
38                 user.setName(set.getString("name"));
39                 user.setPwd(set.getString("pwd"));
40                 return user; //結果集有用戶,則返回用戶
41             }
42 
43         }catch(ClassNotFoundException e){
44             e.printStackTrace();
45         }catch(SQLException e){
46             e.printStackTrace();
47         }finally {
48             try{
49                 statement.close();
50                 con.close();
51             } catch(SQLException e){
52                 e.printStackTrace();
53             }
54         }
55         return null;
56     }
57 
58 }

3、服務

(1)服務接口

1 package com.uplooking.service;
2 
3 import com.uplooking.entity.User;
4 
5 public interface UserService {
6     User login(String name,String pwd);
7 }

(2)服務實現

 1 package com.uplooking.service.imp;
 2 
 3 import com.uplooking.dao.UserDao;
 4 import com.uplooking.entity.User;
 5 import com.uplooking.service.UserService;
 6 
 7 public class UserServiceimp implements UserService {
 8    private UserDao userDao=new UserDao();
 9     @Override
10     public User login(String name, String pwd) {
11         User user=userDao.findByName(name);
12         if(user!=null&&pwd.equals(user.getPwd())){
13             return user;
14         }
15         return null;
16     }
17 }

4、用戶響應

 1 package com.uplooking.action;
 2 
 3 import com.uplooking.entity.User;
 4 import com.uplooking.service.UserService;
 5 import com.uplooking.service.imp.UserServiceimp;
 6 
 7 public class UserAction {
 8     public static void main(String[] args) {
 9         UserAction userAction=new UserAction();
10         userAction.login("smith","123");
11     }
12     private UserService userService=new UserServiceimp();
13     public void login(String name,String pwd){
14         User user=userService.login(name,pwd);
15         if(user!=null){
16             System.out.println("用戶:"+user.getName()+"   成功登陸。");
17         }else{
18             System.out.println("用戶名或密碼錯誤,登錄失敗。");
19         }
20     }
21 
22 }

 執行結果:

 

二:進行方法抽象

 

1、將配置文件放入properties文件中。

1 --properties文件名為:dbconfig.properties
2 
3 DBSRIVER=com.mysql.jdbc.Driver
4 DBURL=jdbc:mysql://127.0.0.1:3307/test?useunicode=true&characterEncoding=utf8
5 DBUSER=root
6 DBPWD=                    //密碼為空

 

2、用構造函數直接加載配置信息

 1 public BaseDao() {
 2         try{
 3             properties =new Properties();
 4             properties.load(this.getClass().getClassLoader().getResourceAsStream("com/uplooking/dbconfig.properties"));
 5             DBRIVER=properties.getProperty("DBRIVER");
 6             DBURL=properties.getProperty("DBURL");
 7             DBUSER=properties.getProperty("DBUSER");
 8             DBPWD=properties.getProperty("DBPWD");
 9 
10 
11         }catch (IOException e){
12             e.printStackTrace();
13         }
14     }
 
        

3、將以下信息抽象到BaseDao中成為可以直接調用的getCon()方法

1 //1、加載驅動
2 Class.forName("com.mysql.jdbc.Driver");
3 //2、創建連接
4  String sqlUrl = "jdbc:mysql://127.0.0.1:3307/test?useunicode=true&characterEncoding=utf8";
5 //Driver表示Java驅動的街口
6  con = DriverManager.getConnection(sqlUrl,"root","");

獲取連接的getcon()方法

 1     /**
 2      * @Method: getCon() 獲取連接,當沒有連接時或者連接已經關閉,調用方法重新獲取連接
 3      * @return: 返回一個連接
 4      */
 5     public Connection getCon(){
 6         try{
 7             Class.forName(DBRIVER);
 8             if(con==null){
 9                 con=DriverManager.getConnection(DBURL,DBUSER,DBPWD);
10             }else{
11                 if(con.isClosed()){
12                     con=DriverManager.getConnection(DBURL,DBUSER,DBPWD);
13                 }
14             }
15         }catch(ClassNotFoundException e){
16             e.printStackTrace();
17         }catch(SQLException e){
18             e.printStackTrace();
19         }
20         return con;
21     }

 4、關閉連接

(1)將關閉連接抽象到BaseDao中

1 con.close();
2 preparedstatement.close();

(2)closeCon()

 1 protected void closeCon(){
 2         try {
 3             if(preparedStatement!=null&&!preparedStatement.isClosed()){
 4                 preparedStatement.close();
 5             }
 6 
 7             if(con!=null&&!con.isClosed()){
 8                 con.close();
 9             }
10 
11         } catch (SQLException e) {
12                 e.printStackTrace();
13 
14         }
15 
16     }

5、通過id對數據進行刪除

寫入UserDaoB包中

 1 //按照id刪除
 2     public int deleteById(int id){
 3         int result=0;
 4         try{
 5             //1、建立連接
 6             con=super.getCon();
 7             //sql語句
 8             String sql="delete from user where id=?";
 9             //2、用預編譯執行sql語句
10             statement=con.prepareStatement(sql);
11             //3、填寫占位符
12             statement.setInt(1,id);
13             //executeUpdate被用來執行delete,update和insert等操作
14             result=statement.executeUpdate();
15             super.closeCon();
16 
17         }catch (SQLException e){
18             e.printStackTrace();
19         }
20         return result;
21     }

調用:

 1 package com.uplooking.action;
 2 
 3 import com.uplooking.dao.UserDao;
 4 
 5 public class JDBCTest {
 6     public static void main(String[] args) {
 7         UserDao userDao=new UserDao();
 8         int result=userDao.deleteById(4);
 9         System.out.println(result>0?"刪除成功":"刪除失敗");
10     }
11 }

6、使用公共方法對MySQL中的數據使用executeUpadte()進行delete、update和insert等等DDL操作。

a、(1)在BaseDao中建立merger()公共方法按照id刪除。

 1  /**           參照按照id刪除核心代碼
 2      *             //1、建立連接
 3      *             con=super.getCon();
 4      *             //sql語句
 5      *             String sql="delete from user where id=?";
 6      *             //2、用預編譯執行sql語句
 7      *             statement=con.prepareStatement(sql);
 8      *             //3、填寫占位符
 9      *             statement.setInt(1,id);
10      *             //executeUpdate被用來執行delete,update和insert等操作
11      *             result=statement.executeUpdate();
12      */
13 
14     /**
15      * @method:該方法是一個公共的方法,可以進行插入、刪除和更新等操作
16      * @return:返回操作結果
17      * @parament:參數是需要執行的sql語句,和不可預估的操作類型和參數
18      */
19     protected int merger(String sql,Object...objects){
20         int result=0;
21         try{
22             //1、連接
23             con=this.getCon();
24             //2、執行預編譯sql語句
25             preparedStatement=con.prepareStatement(sql);
26             //3、填充占位符
27             for (int i = 0; i <objects.length ; i++) {
28                 preparedStatement.setObject(i+1,objects[i]);
29             }
30             //差點忘記,要執行DDL語句
31             result=preparedStatement.executeUpdate();
32 
33         }catch (SQLException e){
34             e.printStackTrace();
35         }finally {
36             this.closeCon();
37         }
38         return result;
39     }

(2)在UserDao中重寫按照id刪除數據庫中的數據。

1 public int deleteById(int id){
2         String sql="delete from user where id=?";
3         return super.merger(sql,id);
4     }

(3)調用方式與上述調用不變,刪除成功。

b、插入操作

1 public int insert(User user){
2         String sql="insert into user(id,name,pwd) values(?,?,?)";
3                 return super.merger(sql,user.getId(),user.getName(),user.getPwd());
4     }

(2)調用

1  //插入操作
2         User user=new User();
3         user.setName("huhu");
4         user.setPwd("123");
5         user.setId(4);
6         System.out.println(userDao.insert(user)>0?"插入成功":"插入失敗");

 

c、

(1)更新操作

1 public int update(User user){
2         String sql="update user set name=?,pwd=? where id=?";
3         return super.merger(sql,user.getName(),user.getPwd(),user.getId());
4     }

(2)調用

1 //更新操作
2         User user=new User();
3         user.setName("hua");
4         user.setPwd("6");
5         user.setId(5);
6         System.out.println(userDao.update(user)>0?"更新成功":"更新失敗");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM