jdbc底层实现

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!");
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值