日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

jdbc连接mysql的配置文件_如何实现jdbc连接数据库并通过配置文件调用数据的方法(代码)...

發(fā)布時間:2024/7/23 数据库 75 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jdbc连接mysql的配置文件_如何实现jdbc连接数据库并通过配置文件调用数据的方法(代码)... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本篇文章給大家?guī)淼膬?nèi)容是關(guān)于如何實現(xiàn)jdbc連接數(shù)據(jù)庫并通過配置文件調(diào)用數(shù)據(jù)的方法(代碼) ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

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

相關(guān)推薦:

總結(jié)

以上是生活随笔為你收集整理的jdbc连接mysql的配置文件_如何实现jdbc连接数据库并通过配置文件调用数据的方法(代码)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。