導入驅動jar包java
註冊驅動mysql
獲取數據庫鏈接對象web
定義sql語句sql
獲取執行sql的對象 statement數據庫
執行sql服務器
處理結果app
釋放資源ide
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.Statement; public class JdbcDamo1 { public static void main(String[] args) throws Exception { //1.導入驅動jar包 //2. 註冊驅動 Class.forName("com.mysql.jdbc.Driver"); //3. 獲取數據庫鏈接對象 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3?useSSL=false","root","*******" ); //4.定義sql語句 String sql = "update emp set mgr = 1999 where id = 1001 "; //輸入須要執行的sql語句 //5.獲取執行sql的對象 statement Statement stmt = conn.createStatement(); //6.執行sql int count = stmt.executeUpdate(sql); //7.處理結果 System.out.println(count); //8.釋放資源 stmt.close(); conn.close(); } }
在JDBC鏈接Mysql數據庫的過程當中出現了以下的警告信息:
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.svg
Mysql數據庫的SSL鏈接問題,提示警告不建議使用沒有帶服務器身份驗證的SSL鏈接,是在MYSQL5.5.45+, 5.6.26+ and 5.7.6+版本中才有的這個問題。學習
1.在數據庫鏈接的url中添加?useSSL=false;
2.url中添加useSSL=true,而且提供服務器的驗證證書。
咱們在簡單的學習過程當中,只須要使用方法一,在鏈接後添加一個?useSSL=false便可
例如:
jdbc:mysql://localhost:3306/數據庫名?useSSL=false
注意:必定是3306/數據庫名後面加?useSSL=false
static void registerDriver(Driver driver) 註冊給定的驅動程序DriverManager。
代碼使用:Class.forName(「com.mysql.jdbc.Driver」);
查看Driver類源碼發現:在com.mysql.jdbc.Driver類中存在靜態代碼塊以下。
static{ try{ java.sql.DriverManager.registerDriver(new Driver()); }catch(SQLException E) { throw new RuntimeException("can't register driver!"); } }
mysql5 以後的驅動jar包能夠省略註冊驅動的步驟,在jar包中的services 文件夾中有一個文件java.sql.Driver裏面包含了註冊驅動的代碼,一旦編譯器檢測到代碼中沒有註冊驅動的代碼,就會調用jar包中的註冊驅動代碼。
2. 獲取數據庫鏈接 方法:static Connection getConnection(String url,String user,String password) 參數: url:指定鏈接的路徑 * 語法:jdbc:mysql://ip地址(域名):端口號/數據庫名稱 * 示例:jdbc:mysql://localhost:3306/db3 * 注意:若是鏈接的是本機mysql服務器,而且mysql服務器默認端口是3306,則url能夠簡寫爲:jdbc:mysql:///db3 user:用戶名 password:密碼
Statement :執行sql的對象
> 返回值int:影響的行數,能夠經過影響的行數判斷DML語句是否執行成功,返回值大於0則執行成功,反之錯誤。
ResultSet :結果集對象
PrepareStatement :執行sql的對象
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JdbcDemo2 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; //1.註冊驅動 try { Class.forName("com.mysql.jdbc.Driver"); //2.獲取鏈接對象 conn = DriverManager.getConnection("jdbc:mysql:///db3?useSSL=false","root","******"); //3.定義sql String sql = "update dept set dname = '富婆部' where id = 50"; //4.獲取執行sql對象 stmt = conn.createStatement(); //5.執行sql int count = stmt.executeUpdate(sql); //6.處理結果 System.out.println(count); if(count > 0) { System.out.println("添加成功"); }else{ System.out.println("添加失敗"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { //7.釋放資源 if(stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JdbcDemo2 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; //1.註冊驅動 try { Class.forName("com.mysql.jdbc.Driver"); //2.獲取鏈接對象 conn = DriverManager.getConnection("jdbc:mysql:///db3?useSSL=false","root","******"); //3.定義sql String sql = "insert into dept(id,dname,loc) values(50,'鹹魚部','西安')"; //4.獲取執行sql對象 stmt = conn.createStatement(); //5.執行sql int count = stmt.executeUpdate(sql); //6.處理結果 System.out.println(count); if(count > 0) { System.out.println("添加成功"); }else{ System.out.println("添加失敗"); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }finally { //7.釋放資源 if(stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }