上次提到過可以通過參數(shù)的方式將要連接的數(shù)據(jù)庫url径簿、用戶名及密碼等通過參數(shù)的方式傳給連接函數(shù)自娩,下面給出具體測(cè)試代碼
package Datacon;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
public class Dataconn {
public String driver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://127.0.0.1:3306/test";
private String user ="root";
private String passwd = "root@2017";
//聲明默認(rèn)連接函數(shù)
public Connection defDataConn() throws ClassNotFoundException, SQLException{
Connection connDB = null; //初始化返回null
//加載驅(qū)動(dòng)類
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.println("未找到指定類");
}
try{
connDB = (Connection) DriverManager.getConnection(url, user, passwd);
}catch(SQLException
e){
e.printStackTrace();
System.out.println("數(shù)據(jù)庫連接失敗");
}
return connDB;
}
public Connection setConnDB(){
Connection connDB = null;
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.println("未找到指定類");
}
try{
connDB = (Connection) DriverManager.getConnection(url, user, passwd);
}catch(SQLException
e){
e.printStackTrace();
System.out.println("數(shù)據(jù)庫連接失敗");
}
System.out.println("===================================================");
System.out.println("=====================自定義函數(shù)========================");
System.out.println("===================================================");
return connDB;
}
public Dataconn(String driver, String url, String user, String passwd) {
super();
this.driver = driver;
this.url = url;
this.user = user;
this.passwd = passwd;
}
}
除以上方法連接數(shù)據(jù)庫jdbc還提供通過讀取配置文件的方式連接數(shù)據(jù)庫,讀取配置文件通過Properties類繼承自Hashtable類并且實(shí)現(xiàn)了Map接口较店,使用鍵值對(duì)的形式來保存屬性集柿顶。需要注意的是配置文件的存放位置為class path目錄。
properties配置文件
新建配置文件唯蝶,通過右擊src文件夾選擇new-other,選擇file類型
選擇file類型
輸入文件名及擴(kuò)展名點(diǎn)擊保存即可遗嗽。
點(diǎn)擊add添加對(duì)應(yīng)配置文件信息粘我。
添加完成點(diǎn)擊保存即可
輸入內(nèi)容
通過Properties類實(shí)例化,通過x.load()方式導(dǎo)入文件內(nèi)容。如下:
prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));
讀取配置文件詳細(xì)代碼如下:
package Datacon;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Connection;
public class Dataconn {
public String driver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://127.0.0.1:3306/test";
private String user ="root";
private String passwd = "root@2017";
public Dataconn(){
}
//聲明默認(rèn)連接函數(shù)
public Connection defDataConn() throws ClassNotFoundException, SQLException{
Connection connDB = null; //初始化返回null
//加載驅(qū)動(dòng)類
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.println("未找到指定類");
}
try{
connDB = (Connection) DriverManager.getConnection(url, user, passwd);
}catch(SQLException
e){
e.printStackTrace();
System.out.println("數(shù)據(jù)庫連接失敗");
}
return connDB;
}
public Connection setConnDB(){
Connection connDB = null;
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.println("未找到指定類");
}
try{
connDB = (Connection) DriverManager.getConnection(url, user, passwd);
}catch(SQLException
e){
e.printStackTrace();
System.out.println("數(shù)據(jù)庫連接失敗");
}
System.out.println("===================================================");
System.out.println("=====================自定義函數(shù)========================");
System.out.println("===================================================");
return connDB;
}
public Dataconn(String driver, String url, String user, String passwd) {
super();
this.driver = driver;
this.url = url;
this.user = user;
this.passwd = passwd;
}
public Connection openFileconn() throws IOException, ClassNotFoundException, SQLException{
Connection dataConn = null;
Properties prop = new Properties();
try{
prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties")); //讀入配置文件
this.driver=prop.getProperty("driver"); //獲取配置文件中相應(yīng)鍵值
this.url = prop.getProperty("url");
this.user = prop.getProperty("user");
this.passwd = prop.getProperty("password");
}catch(IOException e){
e.printStackTrace();
}
System.out.println("config file :"+this.driver+this.url+this.user+this.passwd);
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.println("為找到該java驅(qū)動(dòng)");
}
try{
dataConn = (Connection) DriverManager.getConnection(url, user, passwd);
}catch(SQLException e){
e.printStackTrace();
System.out.println("連接失敗");
}
return dataConn;
}
}
方法測(cè)試代碼如下:
package Datacon;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;
import org.junit.Test;
import com.mysql.jdbc.Connection;
public class testRun {
@Test
public void runDef() throws ClassNotFoundException, SQLException{
Dataconn dataconn = new Dataconn();
System.out.print(dataconn);
try{
Connection connDB= dataconn.defDataConn(); //調(diào)用數(shù)據(jù)庫連接函數(shù)
Statement stat = connDB.createStatement(); //初始化statement對(duì)象連接數(shù)據(jù)庫
String SQL = "select * from users";
ResultSet res = stat.executeQuery(SQL); //查詢指定數(shù)據(jù)
//遍歷數(shù)據(jù)集合
while(res.next()){
String name = res.getNString(2);
System.out.println("查詢內(nèi)容為:"+name);
}
}catch(SQLException e){
e.printStackTrace();
System.out.println("連接失敗");
}
}
@Test
public void runSet(){
Dataconn dataconn = new Dataconn("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1:3306/test", "root", "root@2017");
System.out.print(dataconn);
try{
Connection connDB= dataconn.setConnDB(); //調(diào)用數(shù)據(jù)庫連接函數(shù)
Statement stat = connDB.createStatement(); //初始化statement對(duì)象連接數(shù)據(jù)庫
String SQL = "select * from users";
ResultSet res = stat.executeQuery(SQL); //查詢指定數(shù)據(jù)
//遍歷數(shù)據(jù)集合
while(res.next()){
String name = res.getNString(2);
System.out.println("查詢內(nèi)容為:"+name);
}
connDB.close();
}catch(SQLException e){
e.printStackTrace();
System.out.println("連接失敗");
}
}
@Test
public void openFileconn() throws IOException, ClassNotFoundException, SQLException{
Dataconn dataconn = new Dataconn();
System.out.print(dataconn);
try{
Connection connDB= dataconn.openFileconn(); //調(diào)用數(shù)據(jù)庫連接函數(shù)
Statement stat = connDB.createStatement(); //初始化statement對(duì)象連接數(shù)據(jù)庫
String SQL = "select * from users";
ResultSet res = stat.executeQuery(SQL); //查詢指定數(shù)據(jù)
//遍歷數(shù)據(jù)集合
while(res.next()){
String name = res.getNString(2);
System.out.println("查詢內(nèi)容為:"+name);
}
}catch(IOException e){
e.printStackTrace();
System.out.println("打開文件失敗");
}
}
}