maven項(xiàng)目連接MySQL方式(使用原生的jdbc)
1.引入依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency>
2.項(xiàng)目目錄結(jié)構(gòu)

jdbc.properties內(nèi)容
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/shiro?useUnicode=true&characterEncoding=utf-8&useSSL=false username=root password=123456
util類
package com.cxb.shiro;
import java.io.IOException;
import java.util.Properties;
/**
* 獲取資源文件的util
* @author 81046
*
*/
public class PropertiesUtil {
static Properties properties = new Properties();
public PropertiesUtil() {
}
public static boolean loadFile(String fileName){
try {
properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static String getPropertyValue(String key){
return properties.getProperty(key);
}
}
package com.cxb.shiro;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcUtils {
//通過上面的工具就可以獲取到properties文件中的鍵值從而可以加載驅(qū)動(dòng) 獲取鏈接 從而 可以增刪改查
private static Connection conn = null;
public static Connection getConn(){
PropertiesUtil.loadFile("jdbc.properties");
String driver = PropertiesUtil.getPropertyValue("driver");
String url = PropertiesUtil.getPropertyValue("url");
String username = PropertiesUtil.getPropertyValue("username");
String password = PropertiesUtil.getPropertyValue("password");
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
close();
}
return conn;
}
public static void close(){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 通過用戶名到數(shù)據(jù)庫中獲取憑證密碼
* @param userName
* @return
*/
private static String getPasswordByUserName(String userName) {
//SQL語句
String sql = "select password from users where username = " +"'" + userName+"'";
Connection conn = JdbcUtils.getConn();
Statement stmt=null;
ResultSet ret = null;
String password=null;
try {
stmt = conn.createStatement();
//執(zhí)行語句,得到結(jié)果集
ret = stmt.executeQuery(sql);
while (ret.next()) {
//這里只查詢的密碼
password = ret.getString(1);
}
ret.close();
conn.close();//關(guān)閉連接
} catch (SQLException e1) {
e1.printStackTrace();
}
return password;
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot自動(dòng)裝配原理詳細(xì)解析
這篇文章主要介紹了SpringBoot自動(dòng)裝配原理詳細(xì)解析,一個(gè)對象交給Spring來管理的三種方式 @Bean @Compoment @Import,2024-01-01
@Bean主要在@Configuration中,通過方法進(jìn)行注入相關(guān)的Bean,@Compoent與@Service歸為一類,在類上加注入對應(yīng)的類,需要的朋友可以參考下
IDEA+Maven搭建Spring環(huán)境的詳細(xì)教程
這篇文章主要介紹了IDEA+Maven搭建Spring環(huán)境的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
SpringBoot整合rabbitMq自定義消息轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot整合rabbitMq自定義消息轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)模擬進(jìn)度計(jì)量器,模擬血壓計(jì)實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Mybatis-Plus的條件構(gòu)造器QueryWrapper & UpdateWrapper示例詳解
Mybatis-Plus的條件構(gòu)造器QueryWrapper和UpdateWrapper為開發(fā)者提供了強(qiáng)大、靈活的條件構(gòu)建工具,能夠大大簡化數(shù)據(jù)庫操作的代碼,通過本文的介紹,讀者可以更加深入地理解這兩個(gè)條件構(gòu)造器的使用方法,并在實(shí)際項(xiàng)目中靈活應(yīng)用,感興趣的朋友跟隨小編一起看看吧2024-01-01
淺談java分頁三個(gè)類 PageBean ResponseUtil StringUtil
下面小編就為大家?guī)硪黄獪\談java分頁三個(gè)類 PageBean ResponseUtil StringUtil。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
JAVA使用POI獲取Excel的列數(shù)與行數(shù)
Apache POI 是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,Apache POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。 下面這篇文章給大家介紹了JAVA使用POI獲取Excel列數(shù)和行數(shù)的方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
Springboot中@Async異步,實(shí)現(xiàn)異步結(jié)果合并統(tǒng)一返回方式
這篇文章主要介紹了Springboot中@Async異步,實(shí)現(xiàn)異步結(jié)果合并統(tǒng)一返回方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

