JDBC:
java database connectivity SUN公司提供的一套操作數據庫的標準規範JDBC規範
(掌握四個核心對象)開發一個JDBC程序的準備工作
JDK中
jar文件
開發一個JDBC程序
1 | // 1.創建數據庫表,並向表中添加測試數據 |
java.sql.DriverManager
類:創建連接a. 註冊驅動
Class.forName("com.mysql.jdbc.Driver")
;b. 與數據庫建立連接
方法一:
1 | getConnection("jdbc://mysql://localhost:3306/my_jdbc", "root", "root"); |
jdbc:mysql://localhost:3306/my_jdbc
或者 jdbc:mysql:////my_jdbc
(默認本季連接)jdbc:oracle:thin:@localhost:1521:sid
注意:MySQL在高版本需要指明是否進行SSL連接;so 如上方法會報錯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.
jdbc.url=jdbc:mysql://localhost:3306/my_jdbc?characterEncoding=utf8&useSSL=true
方法二:
1 | Properties info = new Properties(); // 要參考數據文檔 |
方法三:
1 | getConnection("jdbc.url=jdbc:mysql://localhost:3306/my_jdbc?username=root&password=root&characterEncoding=utf8&useSSL=true"); |