一、需求
- 通過鍵盤錄入用戶名和密碼
- 判斷用戶是否登錄成功
select * from user where username = "" and password = "";
如果這個sql有查詢結果,則成功,反之,則失敗
二、代碼實現
數據庫存儲:
jdbc.properties
配置:
url = jdbc://mysql:///student
user = root
password = root
driver = com.mysql.jdbc.Driver
JDBCUtils.java
:
/**
* JDBC工具類
*/
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 文件的讀取,只需要讀取一次即可拿到這些值。使用靜態代碼塊
*/
static{
//讀取資源文件,獲取值。
try {
//1. 創建Properties集合類。
Properties pro = new Properties();
//獲取src路徑下的文件的方式--->ClassLoader 類加載器
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
//2. 加載文件
pro.load(new FileReader(path));
//3. 獲取數據,賦值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
//4. 註冊驅動
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 獲取連接
* @return 連接對象
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 釋放資源
* @param stmt
* @param conn
*/
public static void close(Statement stmt,Connection conn){
if( stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if( conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 釋放資源
* @param stmt
* @param conn
*/
public static void close(ResultSet rs,Statement stmt, Connection conn){
if( rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if( stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if( conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
JdbcDemo.java
:
public class JDBCDemo {
public static void main(String[] args) {
//1.鍵盤錄入,接受用戶名和密碼
Scanner sc = new Scanner(System.in);
System.out.println("請輸入用戶名:");
String username = sc.nextLine();
System.out.println("請輸入密碼:");
String password = sc.nextLine();
//2.調用方法
boolean flag = new JDBCDemo9().login2(username, password);
//3.判斷結果,輸出不同語句
if(flag){
//登錄成功
System.out.println("登錄成功!");
}else{
System.out.println("用戶名或密碼錯誤!");
}
}
/**
* 登錄方法
*/
public boolean login(String username ,String password){
if(username == null || password == null){
return false;
}
//連接數據庫判斷是否登錄成功
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
//1.獲取連接
try {
conn = JDBCUtils.getConnection();
//2.定義sql
String sql = "select * from user where username = '"+username+"' and password = '"+password+"' ";
System.out.println(sql);
//3.獲取執行sql的對象
stmt = conn.createStatement();
//4.執行查詢
rs = stmt.executeQuery(sql);
//5.判斷
/* if(rs.next()){//如果有下一行,則返回true
return true;
}else{
return false;
}*/
return rs.next();//如果有下一行,則返回true
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,stmt,conn);
}
return false;
}
}
這裏需要注意添加 username 和 password 之後 sql 的寫法:
String sql = "select * from user where username = '' and password = ''";
然後將傳入的參數 user 和 password 寫入:
String sql = "select * from user where username = '"+username+"' and password = '"+password"'";