通過讀取properties檔案動態生成對資料庫的連線
properties檔案:db.properties
#Oracle mysql db info
db_url_oracle = jdbc:oracle:thin:@127.0.0.1:1521:ora9
db_url_mysql = jdbc:mysql://localhost/dandan?characterEncoding=utf-8
username = root
password = dada
獲取對mysql的連線並操作:
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class MysqlConn {
public static Connection getConn() throws Exception {
//新建物件
Properties ps=new Properties();
//獲取輸入流
FileInputStream fis=new FileInputStream("src\\chapter18\\properties\\db.properties");
//把流載入到記憶體中
ps.load(fis);
//關閉流
fis.close();
//獲取資料庫url
String url=ps.getProperty("db_url_mysql");
//獲取使用者名稱
String user=ps.getProperty("username");
//獲取密碼
String password=ps.getProperty("password");
//獲取連線
Connection conn=DriverManager.getConnection(url,user,password);
return conn;
}
public static void main(String[] args) throws Exception {
//載入驅動
Class.forName("org.gjt.mm.mysql.Driver");
//獲取連線
Connection conn=MysqlConn.getConn();
//建立statement
Statement stmt=conn.createStatement();
//執行操作
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next()) {
System.out.println("id: "+rs.getInt(1)+"\tname: "+rs.getString(2)+
"\tsex: "+rs.getString(3)+"\tgrade: "+rs.getString(4));
}
rs.close();
stmt.close();
conn.close();
}
}
獲取對Oracle的連線並操作
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class OracleConn {
public static Connection getConnection() throws Exception {
//建立Properties物件
Properties ps=new Properties();
//建立讀取流
FileInputStream fis=new FileInputStream("mydb.properties");
ps.load(fis);
fis.close();
String url=ps.getProperty("db_url_oracle");
String userName=ps.getProperty("userName");
String password=ps.getProperty("password");
Connection conn=DriverManager.getConnection(url,userName,password);
return conn;
}
public static void main(String[] args) throws Exception {
//1.載入驅動 2.獲取連線 3.獲取statement 4.執行操作
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=OracleConn.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next()){
System.out.print("stuId: " + rs.getInt(1));
System.out.print("\name: " + rs.getString(2));
System.out.println("\tgrade: " + rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
}
}
相關文章
- Java讀取properties檔案連線資料庫Java資料庫
- 1.6.5.2. 通過密碼檔案驗證連線資料庫密碼資料庫
- 織夢CMS(dedecms)的資料庫連線檔案_織夢連線資料庫檔案資料庫
- mybatis讀取properties檔案內容MyBatis
- C#連線Oracle資料庫,通過EF自動生成與資料庫表相關的實體類C#Oracle資料庫
- java Spring讀取properties檔案的注意點JavaSpring
- C編譯: 動態連線庫 (.so檔案)編譯
- pandas的基礎使用,資料庫連線,檔案讀取,切片的使用等基本操作----01資料庫
- Spring boot 讀取properties檔案的四種方式Spring Boot
- 動態網頁(JSP 檔案)如何連線資料庫(SQL Server)--看這裡網頁JS資料庫SQLServer
- 使用openpyxl庫讀取Excel檔案資料Excel
- Java讀取properties配置檔案工具包Java
- 1.3.3. 通過SQL*Plus 連線資料庫SQL資料庫
- 網站連線資料庫配置檔案網站資料庫
- Java的JDBC通過SSH Tunnel連線MySQL資料庫JavaJDBCMySql資料庫
- 配置檔案讀取——MySQL 多個連線MySql
- iis連線資料庫的檔案是哪個資料庫
- SpringBoot--SpringBoot 讀取Properties檔案(結合JDBC)Spring BootJDBC
- 讀取resources中properties檔案內容範例
- 資料庫的連線過程資料庫
- root 使用者通過 sqlplus 連線資料庫SQL資料庫
- EasyExcel庫來讀取指定Excel檔案中的資料Excel
- 讀取資料夾檔案
- 爬取LeetCode資料,生成README檔案,美化GitHub倉庫LeetCodeGithub
- 工具類,關於手工讀取 properties檔案引數
- 通過觸發器記錄資料庫連線資訊觸發器資料庫
- ArcGIS Pro SDK 002 對資料檔案的讀取和操作
- Intellij IDEA 通過資料庫生成 POJOIntelliJIdea資料庫POJO
- 使用yaml檔案讀取資料YAML
- 讀取本地Excel檔案生成echartsExcelEcharts
- JAVA通過URL連結獲取視訊檔案資訊(無需下載檔案)Java
- Java動態指令碼Groovy讀取配置檔案Java指令碼
- 通過web url獲取檔案資訊Web
- 輕鬆找到並檢視織夢CMS的資料庫配置檔案,從而獲取資料庫連線資訊資料庫
- 如何獲取java執行時動態生成的class檔案?Java
- Java 最佳化:讀取配置檔案 "萬能方式" 跨平臺,動態獲取檔案的絕對路徑Java
- Java 對 properties 檔案操作 (ResourceBundle 類和 Properties 類)Java
- 通過本地直接連線linux伺服器的mysql資料庫Linux伺服器MySql資料庫
- ios端app讀取iphone檔案(通過itunes實現)iOSAPPiPhone