本文目錄一覽:
JAVA 使用JDBC連接MYSQL數據庫時,連接不同數據庫的方法
一般的話,一個數據庫連接用完之後是要關閉的。
如果是一個項目的話一般使用數據庫連接池,如果有多個數據庫的話最好是建立多個連接池,這樣的話,在系統啟動時,一次加載一定數量的連接對象,用完之後放回去。
如果你僅僅需要兩個數據庫連接對象的話,不如分別創建兩個靜態全局變量來保存兩個數據庫的連接對象。
jdbc連接數據庫步驟
jdbc連接數據庫步驟如下:
操作設備:戴爾筆記本電腦
操作系統:win10
操作程序:mysql jdbc
1、點擊左下角的開始菜單,如下圖所示:
2、輸入cmd,打開命令行,如下圖所示:
3、輸入Net start Mysql,啟動mysql服務,如下圖所示:
4、先輸入create database,如下圖所示:
5、然後輸入你想要的數據庫名字,如下圖所示:
使用jdbc連接mysql為什麼報錯?
當我用JDBC連接MySql數據庫時,編譯報了如下錯誤:
錯誤1:
Loading class `com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
這要求我們註冊驅動時,把Class.forName(“com.mysql.jdbc.Driver”);改成 Class.forName(“com.mysql.cj.jdbc.Driver”);
當我信息滿滿的修改之後重新編譯時,再次出現了錯誤:
錯誤2:
Fri Feb 22 08:55:38 CST 2019 WARN: Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
這要求我們在設置url參數時,將useSSL=false,修改後 jdbc:mysql://localhost:3306/ds3?useSSL=false
當我修改後,本以為這下應該沒問題了,沒想到,再一次出現了問題
錯誤3:
Exception in thread “main” java.sql.SQLException: The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
這要求我們修改時區,修改成jdbc:mysql://localhost:3306/ds3?useSSL=falseserverTimezone=UTC
終於,不在報錯誤了。
錯誤4:當我們配置xml文件時,要把轉為其本身的轉義字符
配置properties文件的urlurl=jdbc:mysql:///ds3?useSSL=falseserverTimezone=UTC配置xml文件的urlproperty name=”url”jdbc:mysql://localhost:3306/ds3?useSSL=falseserverTimezone=UTC/property
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/232033.html