java的jdbc

來源:互聯網
上載者:User

標籤:cep   explicit   格式   database   parent   應用程式開發   stream   串連   製作   

1.什麼是JDBC

JDBC是一個獨立於特定資料庫管理系統、通用的SQL資料庫存取和操作的公用介面,面向介面編程,為訪問不同的資料庫提供了一種統一的途徑,為開發人員屏蔽了一些細節問題,各個資料庫廠商根據JDBC的規範製作的 JDBC 實作類別的類庫。主要面向兩個層次,面嚮應用的api:供應應用程式開發人員使用和面向資料庫的api:供開發商開發資料庫驅動程式用

2.Driver介面:所有的jdbc驅動程式需要實現的介面

#config.propertiesuser=rootpassword=密碼url=jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false&verifyServerCertificate=falsedriverClass=com.mysql.cj.jdbc.Driver
<!--父pom.xml--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.jdbc.test</groupId><artifactId>jdbctest</artifactId><version>0.0.1-SNAPSHOT</version><!-- 必須為pom --><packaging>pom</packaging><name>jdbctest</name><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build><!-- 依賴的自訂實現 --><dependencyManagement><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></dependency></dependencies></dependencyManagement></project> 
<!--子pom.xml--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.jdbc.test2</groupId><artifactId>jdbctest2</artifactId><name>jdbctest2</name><description>jdbctest2</description><parent><groupId>cn.jdbc.test</groupId><artifactId>jdbctest</artifactId><version>0.0.1-SNAPSHOT</version><!-- 相對該pom.xml的父pom.xml的位置 --><relativePath>../jdbctestparent/pom.xml</relativePath></parent><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies></project>
public class DriverTest1 {public static void main(String[] args) throws Exception {Connection connection = DriverTest1.getConnection("config.properties");System.out.println(connection);}// 適用於mysql的jdbc串連//@Test // test相容性不好不能使用public static Connection getMysqlConnection() throws SQLException {// mysql5用的驅動url是com.mysql.jdbc.Driver,mysql6以後用的是com.mysql.cj.jdbc.Driver。// 版本不匹配便會報驅動類已淘汰的錯誤。 JDBC串連Mysql6// com.mysql.cj.jdbc.Driver需要指定時區serverTimezone,// 否則會報錯如下/* * 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. 時區不統一 */Driver driver = new Driver();/* * mysql需要提供的一些資訊的源碼 public DriverPropertyInfo[] getPropertyInfo(String * url, Properties info) throws SQLException { String host = "";//主機ip * localhost (url中寫了) String port = "";//連接埠3306(url中寫了) String database * = "";資料庫 table (url中寫了) String user = "";資料庫帳號 (properties需要提供) * String password = "";資料庫密碼 (properties需要提供) url * 格式:jdbc:mysql://localhost:3306/table 說明:jdbc:固定格式 mysql:協議 * //localhost:3306主機名稱和連接埠/test資料庫名,後續還可串連如下 * mysql.url=jdbc:mysql://127.0.0.1:3306/table?serverTimezone=UTC& * useUnicode=true&characterEncoding=utf8&characterSetResults=utf8& * useSSL=false&verifyServerCertificate=false&autoReconnct=true& * autoReconnectForPools=true&allowMultiQueries=true * serverTimezone=UTC:時區utc(世界標準時間),我過時間在其上加8個小時即可 有兩種方法:方法1:在mysql中使用 * set global time_zone=‘+8:00‘; 方法2:&serverTimezone=GMT%2B8 來設定為中國時區 * useUnicode=true&characterEncoding=utf8&characterSetResults=utf8 * 處理與資料庫字元集不統一導致的亂碼問題, 這3個一般一起書寫,含義如下 * 本質:useUnicode=true&characterEncoding=utf- * 8的作用是指定character_set_client和character_set_connection的值, * 在jdbc連結中使用characterSetResults=UTF-8,即可設定character_set_results的值 * 系統變數character_set_client:用來指定解析用戶端傳遞資料的編碼 * 系統變數character_set_connection:用來指定資料庫內部處理時使用的編碼 * 系統變數character_set_results:用來指定資料返回給用戶端的編碼方式 useSSL=false * 不需要使用SSL串連,你需要通過設定useSSL=false來顯式禁用SSL串連。 SSL:Secure Sockets * Layer(安全套接層) verifyServerCertificate=false 服務憑證 * 設定useSSL=false與verifyServerCertificate=false可以解決如下異常警告 Mon Aug 06 * 23:00:16 CST 2018 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. *  * autoReconnct=true 逾時自動重連 autoReconnectForPools=true 針對資料庫重連策略 * allowMultiQueries=true 允許批次更新 *  * (後續mysql中說) */Properties properties = new Properties();properties.put("user", "root");properties.put("password", "cgz12345678");Connection connect = driver.connect("jdbc:mysql://localhost:3306/table?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8",properties);return connect;}public static Connection getConnection(String path) throws Exception {Properties properties = new Properties();properties.load(new FileInputStream(path));String driverClass=properties.getProperty("driverClass");// 為什麼使用sql呢,因為這裡使用的是jdbc規範,也就是說都要實現jdbc的Driver介面// 所以這裡採用的是多態的方式java.sql.Driver driver = (java.sql.Driver) Class.forName(driverClass).newInstance();String url = properties.getProperty("url");Connection connect = driver.connect(url, properties);return connect;}}

 未完待續

 

java的jdbc

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.