JDBC技術-獲取數據庫的連接

1.加載數據庫驅動

(1)在工程目錄下新建Floder->Folder name:lib

(2)在lib目錄導入數據庫驅動.jar包,再選擇Build Path->Add to Build Path,最后才能使用.jar包下的API。

(3)加載驅動源碼(驅動文件下src文件目錄)

之后選中src文件即可。

2.獲取數據庫連接方式一:

@Test

public void testConnection1() throws SQLException {

// 獲取Driver實現類對象

Driver driver = new com.mysql.jdbc.Driver();

// url:http://localhost:8080/gmall/keyboard.jpg

// jdbc:mysql:協議

// localhost:ip地址

// 3306:默認mysql的端口號

// test:test數據庫

String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";

// 將用戶名和密碼封裝在Properties中

Properties info = new Properties();

info.setProperty("user", "root");

info.setProperty("password", "123456");

Connection conn = driver.connect(url,info);

System.out.println(conn);

}

①Eclipse連接數據庫報錯com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'test'.

背景:用戶名密碼正確,database在mysql中存在。

命令行下執行show databases(),test數據庫是存在的。

找資料發現有可能是有其他mysql服務自啟動了,導致程序有點混亂。

鍵盤“windows”+R 輸入services.msc

發現有mysql這個服務是啟動的。關掉即可。

②Eclipse連接數據庫報錯:Unknown initial character set index '255' received from server.

java.sql.SQLException: Unknown initial character set index ‘255’ received from server.

編碼不匹配的原因

方法一:直接在連接的URL后加上

?useUnicode=true&characterEncoding=utf8 即可。

方法二:換一個新版本的mysql-connector-java。

5.1.44的character_set_server和character_set_database系統變量的默認值已從latin1更改為utf8mb4

mysql 8.0版本與mysql數據庫驅動8.0以上版本

第一步:把驅動的類名改為:

static String driver="com.mysql.cj.jdbc.Driver";

第二步:在訪問mysql的url后加入時區設置:

static String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC";(UTC表示標準時區)

Druid數據庫連接池技術的實現

①在WEB-INF目錄下創建一個lib目錄,把mysql數據庫驅動,Druid數據庫連接池驅動放到lib目錄下,并加載到lib。

②編寫JDBCUtils類,實現數據庫連接池技術連接數據庫。

編寫druid.properties配置文件。

public class JDBCUtils {

? ? /*public static void main(String[] args) {

? ? ? ? try {

? ? ? ? ? ? Connection conn = JDBCUtils.getConnection();//每調用一次getConnection()就創建一個數據庫連接池

? ? ? ? ? ? System.out.println(conn);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }*/

? ? /**

? ? * 獲取數據庫連接池中的連接

? ? * @return

? ? */

? ? private static DataSource dataSource;

? ? //使用靜態代碼塊初始化數據庫連接池

? ? static {

? ? ? ? try {

? ? ? ? ? ? Properties pros = new Properties();

? ? ? ? ? ? //InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");

? ? ? ? ? ? //讀取jdbc.properties屬性配置文件

? ? ? ? ? ? InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

? ? ? ? ? ? //從流中加載數據

? ? ? ? ? ? pros.load(is);

? ? ? ? ? ? //創建數據庫連接池

? ? ? ? ? ? dataSource = DruidDataSourceFactory.createDataSource(pros);

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

? ? public static Connection getConnection(){

? ? ? ? Connection conn = null;

? ? ? ? try {

? ? ? ? ? ? conn = dataSource.getConnection();

? ? ? ? } catch (SQLException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? return conn;

? ? }

? /* public static Connection getConnection() throws Exception {

? ? ? ? InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

? ? ? ? //FileInputStream is = new FileInputStream(new File("src/jdbc.properties"));

? ? ? ? Properties pros = new Properties();

? ? ? ? pros.load(is);

? ? ? ? //創建一個Druid數據庫連接池

? ? ? ? DataSource dataSource = DruidDataSourceFactory.createDataSource(pros);

? ? ? ? Connection conn = dataSource.getConnection();

? ? ? ? return conn;

? ? }*/

? ? /**

? ? * 關閉連接,放回數據庫連接池

? ? * @param conn

? ? */

? ? public static void close(Connection conn){

? ? ? ? if (conn != null);

? ? ? ? try {

? ? ? ? ? ? conn.close();

? ? ? ? } catch (SQLException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

}

public class JDBCUtilsTest {

? ? @Test

? ? public void test(){

? ? ? ? Connection conn = JDBCUtils.getConnection();

? ? ? ? System.out.println(conn);

? ? ? ? JDBCUtils.close(conn);

? ? }

}

使用數據庫連接池獲取數據庫連接,連接使用完一定要記得及時關閉數據庫連接。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。