標籤:技術 dstat drive username 分享圖片 鍛煉 block sel end
禮悟:
好好學習多思考,尊師重道存感恩。葉見尋根三二一,江河湖海同一體。
虛懷若穀良心主,願行無悔給最苦。讀書鍛煉強身心,誠勸且行且珍惜。
資料、資料,命根就在資料。雲端運算、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.ResultSetMetaData;import java.sql.SQLException;/** * * * @author 部落格園-給最苦 * @version V17.11.09 */public class Demo {public static void main(String[] args) {// 給最苦 對name起了別名String sql = "select id,name as username,quantity,time,price from book";test(sql);}public static void test(String sql) {Connection con = null;PreparedStatement ps = null;ResultSet rs = null;ResultSetMetaData rsmd = null;try {// 註冊驅動並建立串連con = JDBCUtils.getConnection();// 建立語句 並 對sql語句進行一些預先處理ps = con.prepareStatement(sql);// 執行rs = ps.executeQuery();// 看看結果中的資訊rsmd = rs.getMetaData();// 得到列的個數int colCount = rsmd.getColumnCount();// getColumnName 列的名字 getColumnLabel 別名// 注意: i從1開始的for (int i = 1; i <= colCount; i++) {System.out.print(rsmd.getColumnClassName(i) + " ");System.out.print(rsmd.getColumnName(i) + " ");System.out.println(rsmd.getColumnLabel(i));}} catch (SQLException e) {throw new DaoException(e);} finally {// 釋放資源JDBCUtils.free(rs, ps, con);}}}
資料庫中各個欄位的類型
測試代碼在控制台的輸出
註:操作資料庫要謹慎小心,給最苦 這裡的代碼 看看就好。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。
jdbc-mysql基礎 ResultSetMetaData getColumnName getColumnLabel 得到列的名字和別名