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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本)
這篇文章主要介紹了SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08JAVA設(shè)置手動提交事務(wù),回滾事務(wù),提交事務(wù)的操作
這篇文章主要介紹了JAVA設(shè)置手動提交事務(wù),回滾事務(wù),提交事務(wù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04解決springcloud 配置gateway 出現(xiàn)錯誤的問題
今天給大家分享springcloud 配置gateway 出現(xiàn)錯誤的問題,其實解決方法很簡單,只需要降低springcloud版本,改成Hoxton.SR5就好了,再次改成Hoxton.SR12,也不報錯了,下面給大家展示下,感興趣的朋友一起看看吧2021-11-11