JDBC 通過讀取配置檔案連線資料庫

2021-10-19 19:50:11 字數 915 閱讀 8416

1、在工程的src目錄下建立配置檔案jdbc.properties;

#配置檔案包含:四個基本資訊

user=root#建立連線的資料庫賬號

password=222222#建立連線的資料庫密

url=jdbc:mysql://localhost:3306/test#test代表所要連線的資料庫

driverclass=com.mysql.jdbc.driver

配置檔案的url由於mysql版本問題在建立連線時程序會出現錯誤有時候需改為下面兩種形式(這是我目前遇到的兩種問題)

url=jdbc:mysql://localhost:3306/test?useunicode=true&usejdbccomplianttimezoneshift=true&uselegacydatetimecode=false&servertimezone=utc

url ="jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf8";

2、書寫測試單元進行連線測試

@test

public void connectiontest() throws ioexception, classnotfoundexception, sqlexception

3、執行結果得到連線

通過jdbc連線資料庫

public class jdbcdemo catch classnotfoundexception e try int data statm.executebatch system.out.println data.length string sql select c.cname,t.tname ...

python通過配置檔案連線mysql

之前在python中連線mysql是直接在python檔案中寫的,這次把資料庫資訊放在單獨乙個配置檔案中 這是linux環境下 下面列出了select update insert 語句 bin env python coding utf 8 import sys import mysqldb imp...

Java連線mySql 簡單JDBC連線資料庫

利用jdbc開發資料庫 經典應該用框架 第一步,載入jdbc資料庫驅動程式 不同的資料庫有不同的資料庫驅動,所以在連線資料庫之前,需載入驅動 格式 string driver com.mysql.jdbc.driver class.forname driver 載入mysql資料庫,用class.f...