JDBC工具類實現(xiàn)登錄功能
本文實例為大家分享了JDBC工具類實現(xiàn)登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
我們使用JDBC實現(xiàn)數(shù)據(jù)庫的增刪改查,代碼基本差不多,有很多重復(fù),所以我們可以把這些重復(fù)的代碼寫成一個工具類,使用的時候直接調(diào)用就可以了。下面以實現(xiàn)登錄功能的案例來介紹。
創(chuàng)建數(shù)據(jù)庫,插入數(shù)據(jù)
use student;
create table user(
id int primary key auto_increment,
username varchar(32),
password varchar(32)
);
insert into user values(null,'zhangsan','123');
insert into user values(null,'lisi','234');
jdbc.properties
url=jdbc:mysql://localhost:3306/student username=root password=root driver=com.mysql.jdbc.Driver
JDBC工具類
package cn.itcast.util;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
/**
* JDBC工具類
**/
public class JDBCUtils {
private static String url;
private static String username;
private static String password;
private static String driver;
/**
* 文件的讀取,著需要讀取一次即可拿到這些值,使用靜態(tài)代碼塊
**/
static {
try {
//1、讀取資源文件,獲取值
Properties properties = new Properties();
//獲取src路徑下的文件的方式 ---> ClassLoader類加載器
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL resource = classLoader.getResource("jdbc.properties");
String path = resource.getPath();
//2、加載文件
properties.load(new FileReader(path));
//3、獲取數(shù)據(jù),賦值
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
driver = properties.getProperty("driver");
//4、注冊驅(qū)動
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 獲取連接
* @return 連接對象
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
/**
* 釋放資源
* @param statement
* @param connection
*/
public static void close(Statement statement,Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 釋放資源
* @param resultSet
* @param statement
* @param connection
*/
public static void close(ResultSet resultSet,Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
實現(xiàn)登錄功能
package cn.itcast.jdbc;
import cn.itcast.util.JDBCUtils;
import java.sql.*;
import java.util.Scanner;
/**
* 1、通過鍵盤錄入用戶名和密碼
* 2、判斷用戶是否登錄成功
*/
public class JDBCLogin {
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、調(diào)用方法
boolean flag = new JDBCLogin().login(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;
}
//連接數(shù)據(jù)庫判斷是否登陸成功
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//1、獲取鏈接
connection = JDBCUtils.getConnection();
//2、定義sql
String sql = "select * from user where username = ? and password = ?";
//3、獲取執(zhí)行sql的對象
//為了防止sql注入,實現(xiàn)事務(wù)安全,效率更高,必須要用PreparedStatement
preparedStatement = connection.prepareStatement(sql);
//給?賦值
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
//4、執(zhí)行查詢,不需要傳遞sql
resultSet = preparedStatement.executeQuery();
//5、判斷:如果是下一行,則返回true
return resultSet.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(resultSet,preparedStatement,connection);
}
return false;
}
}
運行結(jié)果:



以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-4 mybatis與spring結(jié)合使用及原理解析
本文通過圖文并茂的形式給大家介紹了mybatis-4 mybatis與spring結(jié)合使用及原理解析,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-04-04
解決HttpServletResponse和HttpServletRequest取值的2個坑
這篇文章主要介紹了解決HttpServletResponse和HttpServletRequest取值的2個坑問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
idea已經(jīng)提交到遠程分支,但需要本地和遠程都回退到某一版本問題
這篇文章主要介紹了idea已經(jīng)提交到遠程分支,但需要本地和遠程都回退到某一版本問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

