JDBC練習實例(實現注冊登錄等增刪改查)


問題描述:流程如下,先登錄,登錄成功后顯示操作界面,根據界面的提示,執行注冊,修改,刪除,查詢的功能

             1、實現用戶的注冊功能

             2、實現用戶的登陸功能

             3、實現用戶的修改,但要考慮用戶是否真實存在

             4、實現用戶的刪除功能。

             5、實現用戶的查詢功能,查詢全部及根據userName名去查詢

 

注釋+代碼:

  1 package jdbcxiti_1;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.SQLException;
  6 import java.sql.Statement;
  7 import java.util.Scanner;
  8 import java.sql.PreparedStatement;
  9 import java.sql.ResultSet;
 10 
 11 public class text_1 {
 12 
 13     public static void main(String[] args) {
 14         // TODO Auto-generated method stub
 15          Scanner sc=new Scanner(System.in);
 16          String name,pass;
 17          person ob=new person();
 18          //注冊
 19          System.out.println("輸入要注冊的用戶名");
 20          name=sc.nextLine();
 21          System.out.println("輸入要注冊的密碼");
 22          pass=sc.nextLine();
 23          ob.register(name, pass);
 24          //登錄
 25             System.out.println("輸入登錄用戶名");
 26                name=sc.nextLine();
 27             System.out.println("輸入登錄密碼");
 28             pass=sc.nextLine();
 29             ob.login(name, pass);
 30          //修改
 31       System.out.println("輸入修改用戶名");
 32        String a=sc.nextLine();
 33        System.out.println("輸入修改密碼");
 34        String b=sc.nextLine();
 35       ob.update(a, b);
 36         //顯示
 37       ob.show();
 38 }
 39 }
 40 class person{
 41     String name;
 42     String password;
 43     boolean islogin=false;
 44     
 45     void register(String name,String password)//用戶注冊
 46     {                  //1、連接
 47          try (Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","數據庫用戶名","數據庫密碼");){
 48                        //sql語句
 49              String sql="insert into t_user(username,pwd) values (?,?)";
 50                 //2、聲明(PrepareStatement是Statement類的子類,解決了sql注入問題
 51              PreparedStatement ps=con.prepareStatement(sql);
 52              ps.setString(1, name);
 53              ps.setString(2, password);
 54                //3、執行
 55              ps.execute();
 56               //4、關閉
 57              ps.close();
 58             
 59         } catch (SQLException e) {
 60             // TODO Auto-generated catch block
 61             e.printStackTrace();
 62         }
 63    }
 64     void login(String name,String password)//用戶登錄
 65     {
 66          try (Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","數據庫用戶名","數據庫密碼");){
 67              String sql="select * from t_user where username=? and pwd=?";
 68              PreparedStatement ps=con.prepareStatement(sql);
 69              ps.setString(1, name);
 70              ps.setString(2, password);
 71              ps.execute();
 72              ResultSet re=ps.getResultSet();
 73              if(re.next())
 74              {
 75                  this.name=name;
 76                  this.password=password;
 77                  islogin=true;
 78                  System.out.println("登陸成功,歡迎"+name);
 79              }else
 80              {
 81                  System.out.println("用戶名或密碼錯誤!");
 82              }
 83              re.close();
 84              ps.close();
 85             
 86         } catch (SQLException e) {
 87             // TODO Auto-generated catch block
 88             e.printStackTrace();
 89         }
 90     }
 91     
 92     void update(String a,String b)//更改用戶名或密碼
 93     {   if(islogin)
 94      {   
 95         try (Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","數據庫用戶名","數據庫密碼");){
 96             String sql="update t_user set username=?,pwd=? where username=? and pwd=?";
 97             PreparedStatement ps=con.prepareStatement(sql);
 98             ps.setString(1, a);
 99             ps.setString(2, b);
100            ps.setString(3, name);
101            ps.setString(4, password);
102             ps.execute();
103             System.out.println("修改成功");
104             ps.close();
105             this.name=a;
106             this.password=b;
107         } catch (SQLException e) {
108             // TODO Auto-generated catch block
109             e.printStackTrace();
110         }
111      }else
112     {
113         
114     }
115     
116     }
117     void show() //用戶名及密碼列表顯示
118     {
119         try (Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","數據庫用戶名","數據庫密碼");){
120                Statement start=con.createStatement();
121                ResultSet re=start.executeQuery("select * from t_user");
122                String a,b;
123                while(re.next()){
124                    a=re.getString(2);
125                    b=re.getString(3);
126                    System.out.println("name:"+a+" password:"+b);
127                }
128                re.close();
129                start.close();
130         } catch (SQLException e) {
131             // TODO Auto-generated catch block
132             e.printStackTrace();
133         }
134     }
135     void delete()  //刪除用戶
136     {
137         if(islogin)
138          {   
139             try (Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","數據庫用戶名","數據庫密碼");){
140                 String sql="delete from t_user where username=? and pwd=?";
141                 PreparedStatement ps=con.prepareStatement(sql);
142                 ps.setString(1, name);
143                ps.setString(2, password);
144                 ps.execute();
145                 System.out.println("刪除成功");
146                 ps.close();
147                 islogin=false;
148             } catch (SQLException e) {
149                 // TODO Auto-generated catch block
150                 e.printStackTrace();
151             }
152          }else
153         {
154             
155         }
156         
157     }
158 }

 

 

 

 


免責聲明!

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



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