控制台用戶輸入 用戶名,密碼 登錄測試


 1 package cn.zmh.Prepared;
 2 
 3 import cn.zmh.Utils.JdbcUtils;
 4 
 5 import java.sql.Connection;
 6 import java.sql.PreparedStatement;
 7 import java.sql.ResultSet;
 8 import java.sql.SQLException;
 9 import java.util.Scanner;
10 /*
11 *  控制台登錄測試(用戶名,密碼)
12  *  使用PreparedStatement   安全性強
13 * **/
14 public class prepareDemo {
15     public static void main(String[] args) throws SQLException {
16         // 從控制台輸入  用戶名和密碼
17         Scanner sc = new Scanner(System.in);
18         System.out.println("請輸入用戶名");
19         String name = sc.nextLine();
20         System.out.println("請輸入密碼");
21         String password = sc.nextLine();
22         // 登錄的方法
23         longin(name,password);
24     }
25 
26     private static void longin(String name, String password) {
27         try {
28             Connection connection = JdbcUtils.getConnection();
29             // 1 編寫sql語句  未知內容使用? 占位符:
30             String sql = "select * from user5 where uname=? and upassword=?";
31             // 2 獲得PreparedStatement 對象
32             PreparedStatement ps = connection.prepareStatement(sql);
33             // 3 設置實際的參數  setxxx(占位符的位置,真實的值)
34             ps.setString(1,name);
35             ps.setString(2,password);
36             // 4 執行sql語句
37             ResultSet rs = ps.executeQuery();
38             if(rs.next()){
39                 System.out.println("登錄成功");
40             }else{
41                 System.out.println("登錄失敗");
42             }
43             // 5 關閉資源
44             JdbcUtils.close(connection,ps, rs);
45         } catch (SQLException e) {
46             e.printStackTrace();
47         }
48     }
49 }

 


免責聲明!

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



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