標籤:ast dex 視頻 static代碼塊 odi 讀書 images ges otf
禮悟:
好好學習多思考,尊師重道存感恩。葉見尋根三二一,江河湖海同一體。
虛懷若穀良心主,願行無悔給最苦。讀書鍛煉強身心,誠勸且行且珍惜。
資料、資料,命根就在資料。雲端運算、AI等技術,都是以資料為基礎。操作資料庫一定要謹慎小心。給最苦 這裡的代碼,看看就好,要有自己的判斷。遇到抉擇,要不恥上下問,三思而後行。
javaSE:8
mysql:5.7.14
mysql-connector-java:5.1.44
MySQL-Front:5.4
os:windows7 x64
ide:MyEclipse 2017
特製的異常類
package com.jizuiku;/** * 這個類很重要,即完成了拋異常的動作、通過編譯,又可以使資料邏輯層的介面保持簡潔。 * * @author 部落格園-給最苦 * @version V17.11.08 */public class DaoException extends RuntimeException {/** * */private static final long serialVersionUID = 1L;public DaoException() {// TODO Auto-generated constructor stub}public DaoException(String message) {super(message);// TODO Auto-generated constructor stub}public DaoException(Throwable cause) {super(cause);// TODO Auto-generated constructor stub}public DaoException(String message, Throwable cause) {super(message, cause);// TODO Auto-generated constructor stub}public DaoException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {super(message, cause, enableSuppression, writableStackTrace);// TODO Auto-generated constructor stub}}
JDBCUtils類
package com.jizuiku;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/** * * * @author 給最苦 * @version V17.11.07 */public final class JDBCUtils {/** * url格式 -> jdbc:子協議:子名稱//主機名稱:連接埠號碼/資料庫的名字?屬性名稱=屬性值&屬性名稱=屬性值 * configString變數中有多個參數,需要深入地去研究它們的具體含義 */private static String configString = "?useUnicode=true&characterEncoding=utf8&useSSL=true";private static String url = "jdbc:mysql://localhost:3306/jdbcforjava" + configString;// 本地的mysql資料庫(無子名稱) 連接埠號碼3306 資料庫jdbcforjavaprivate static String user = "root";private static String password = "";// 工具類,直接使用,不可生對象private JDBCUtils() {}// 註冊驅動,這裡應用的是static代碼塊只執行一次的特點static {try {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockthrow new ExceptionInInitializerError(e);}}/** * 擷取與指定資料庫的連結 * */public static Connection getConnection() throws SQLException {return DriverManager.getConnection(url, user, password);}/** * 釋放三種資源ResultSet PreparedStatement Connection * */public static void free(ResultSet rs, PreparedStatement ps, Connection con) {try {if (rs != null) {rs.close();}} catch (SQLException e) {// TODO Auto-generated catch blockthrow new DaoException(e.getMessage(), e);} finally {try {if (ps != null) {ps.close();}} catch (SQLException e) {// TODO Auto-generated catch blockthrow new DaoException(e.getMessage(), e);} finally {try {if (con != null) {con.close();}} catch (SQLException e) {// TODO Auto-generated catch blockthrow new DaoException(e.getMessage(), e);}}}}}
未執行代碼時,資料庫中的內容
測試類別
package com.jizuiku;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * * * @author 部落格園-給最苦 * @version V17.11.09 */public class Demo {public static void main(String[] args) {int result = test();System.out.println("新插入記錄的id是" + result);}public static int test() {Connection con = null;PreparedStatement ps = null;ResultSet rs = null;try {// 註冊驅動並建立串連con = JDBCUtils.getConnection();// 建立語句String sql = "insert into book(name,quantity,time,price) values(‘道德經‘,‘100‘,‘2017-12-01‘,55.99)";// 把產生的主鍵 返回給java程式ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);// 執行語句ps.executeUpdate();// 拿到主鍵(給最苦 的表是單一主鍵,不是複合主鍵)// 它返回的ResultSetrs = ps.getGeneratedKeys();// 表的主鍵是Int類型的,一個int id = 0;if (rs.next()) {id = rs.getInt(1);}return id;} catch (SQLException e) {throw new DaoException(e);} finally {// 釋放資源JDBCUtils.free(rs, ps, con);}}}
控制台輸出的結果
資料庫中的內容變化
註:操作資料庫要謹慎小心,給最苦 這裡的代碼 看看就好。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。
jdbc-mysql基礎 ResultSet getGeneratedKeys 插入一條欄位並返回其主鍵