如何實作jdbc連接資料庫並透過設定檔呼叫資料的方法(代碼)
Sep 14, 2018 pm 04:16 PM
jdbc
這篇文章帶給大家的內容是關於如何實現jdbc連接資料庫並透過設定檔呼叫資料的方法(程式碼) ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
Dbutil類別
package com.db; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class Dbutil { private String driver = "com.mysql.cj.jdbc.Driver"; private String host = "localhost"; private String post = "3306"; private String db = "student"; private String user = "root"; private String psd = "123456"; private String url = ""; private Connection con = null; public Dbutil() { Properties pro = new Properties(); InputStream in = Dbutil.class.getResourceAsStream("db.properties"); try { pro.load(in); this.driver =pro.getProperty("driver"); this.host= pro.getProperty("host"); this.post=pro.getProperty("post"); this.db=pro.getProperty("db"); this.user=pro.getProperty("user"); this.psd=pro.getProperty("psd"); this.url = "jdbc:mysql://" + host + ":" + post + "/" + db + "?serverTimezone=PRC&useSSL=false"; Class.forName(driver); this.con=DriverManager.getConnection(url,user,psd); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public Dbutil(String host, String post, String db, String user, String psd){ this.host = host; this.post = post; this.db = db; this.user = user; this.psd = psd; this.url = "jdbc:mysql://" + host + ":" + post + "/" + db + "?serverTimezone=PRC&useSSL=false"; try { Class.forName(driver); this.con=DriverManager.getConnection(url,user,psd); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPost() { return post; } public void setPost(String post) { this.post = post; } public String getDb() { return db; } public void setDb(String db) { this.db = db; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPsd() { return psd; } public void setPsd(String psd) { this.psd = psd; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Connection getCon() { return con; } public void setCon(Connection con) { this.con = con; } }
登入後複製
設定檔db.properties
driver=com.mysql.cj.jdbc.Driver host=localhost post=3306db=student user=root psd=123456
登入後複製
以上是如何實作jdbc連接資料庫並透過設定檔呼叫資料的方法(代碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱門文章
擊敗分裂小說需要多長時間?
3 週前
By DDD
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java8(291)之後禁用了TLS1.1使JDBC無法用SSL連接SqlServer2008怎麼解決
