jdbc底層實現

來源:互聯網
上載者:User

1、匯入mysql包  地址:http://download.csdn.net/download/u010310183/8529489

package test;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Types;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;public class test_1 {static String Conf_Class, Conf_url, Conf_username, Conf_password, logfile,listsize, threadnum, Conf_WriteFilePath, pathWithCIDS, pathForUsed;static int maxnums = 50;static boolean runtag = true;public static void main(String [] args) throws ClassNotFoundException, SQLException{Connection con = null;Properties properties = new Properties();InitConf();//載入配置資訊Class.forName(Conf_Class);con = DriverManager.getConnection(Conf_url, Conf_username, Conf_password);Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery("SELECT * FROM task");int colNum = rs.getMetaData().getColumnCount();List<Map<String, Object>> row = new ArrayList<Map<String, Object>>(); while (rs.next()) { Map<String, Object> col = new HashMap<String, Object>(); for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { //System.out.println(  rs.getMetaData().getColumnType(i)  ); switch( rs.getMetaData().getColumnType(i) ){  case Types.VARCHAR: col.put(rs.getMetaData().getColumnName(i), rs.getString(i)); break;  case Types.INTEGER: col.put(rs.getMetaData().getColumnName(i), rs.getInt(i)); break;  case Types.BLOB: InputStream in = rs.getBinaryStream(i); col.put(rs.getMetaData().getColumnName(i), in ); break; default: col.put(rs.getMetaData().getColumnName(i), rs.getString(i)); break; } } System.out.println(rs.getString(2));row.add(col);}for(int i = 0 ; i < 10;i++){java.util.Map mapRole = (java.util.Map)row.get(i);//System.out.println(mapRole.get("id"));} System.out.println(rs.getMetaData().getColumnCount());//表結構中屬性的個數System.out.println(rs.getMetaData().getColumnName(2).toLowerCase());//表結構中  第二個位置 屬性的名稱rs.next();//指標後移一位System.out.println(rs.getString(4));//擷取第四個位置的屬性 所對應的值//List<Map<String, Object>> list = rsToList(rs);//System.out.println(list.size());//for(int i = 0 ; i < 10;i++){//java.util.Map mapRole = (java.util.Map)list.get(i);//System.out.println(mapRole.get("id"));//}//if( !list.isEmpty() ){//System.out.println(list.get(0));//}releaseConnection(rs, stmt, con);//關閉串連}private static void releaseConnection(ResultSet rs, Statement stmt, Connection connect) throws SQLException{    try {        if (rs != null){            rs.close();        }        if (stmt != null){        stmt.close();        }        if (connect != null && !connect.isClosed()){        connect.close();        }    } catch (SQLException se){        System.out.println("Close the connection encounter error!\n" + se.getMessage());        throw new SQLException("關閉串連異常。");    }}    public Connection getConnection() throws ClassNotFoundException, SQLException{Connection con = null;Properties properties = new Properties();try {properties.load(this.getClass().getResourceAsStream("/db.properties"));String driver = properties.getProperty("sDBDriver");String url = properties.getProperty("sConnStr");String user = properties.getProperty("username");String password = properties.getProperty("password");try {Class.forName(driver);con = DriverManager.getConnection(url, user, password);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}} catch (IOException e) {e.printStackTrace();}return con;}    /** * 載入設定檔 config.properties */public static void InitConf() {System.out.println("載入配置資訊-Start: "+ new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));try {Properties properties = new Properties();properties.load(test_1.class.getResourceAsStream("/config.properties"));Conf_Class = properties.getProperty("class");Conf_url = properties.getProperty("outurl");Conf_username = properties.getProperty("outuser");Conf_password = properties.getProperty("outpass");logfile = properties.getProperty("logpath");logfile = properties.getProperty("log");listsize = properties.getProperty("listsize");threadnum = properties.getProperty("threadnum");maxnums = Integer.parseInt(properties.getProperty("maxnums"));pathWithCIDS = properties.getProperty("pathWithCIDS");pathForUsed = properties.getProperty("pathForUsed");System.out.println(Conf_Class);System.out.println(Conf_url);System.out.println(Conf_username);File fileLog = new File(logfile);if (!fileLog.exists()) {fileLog.mkdirs();}logfile = logfile + "/"+ new SimpleDateFormat("yyyyMMdd").format(new Date())+ ".log";  System.out.println("載入配置資訊-End: "+ new SimpleDateFormat("HH:mm:ss.SSS").format(new Date()));} catch (Exception e) {runtag = false;System.out.println("error in InitConf!");}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.