`
Aga
  • 浏览: 213088 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

成长(一)

    博客分类:
  • J2SE
阅读更多
package com.cxz.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.sql.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class DBConnector {

	private String jdbc = "";

	private String url = "";

	private String userName = "";

	private String password = "";
	
	private ResourceBundle bundle=null;

	public DBConnector() {
		//Properties prop = new Properties();
		 //prop.load(new FileInputStream("app.properties")); 
			bundle=ResourceBundle.getBundle("info.entryexit_v15-info");
			jdbc=bundle.getString("jdbc");
			url=bundle.getString("url");
			userName=bundle.getString("userName");
			password=bundle.getString("password");
			//jdbc = prop.getProperty("jdbc");
			/*
			url = prop.getProperty("url");
			userName = prop.getProperty("userName");
			password = prop.getProperty("password");
			*/
		
	}

	public DBConnector(String jdbc, String url, String userName, String password) {
		this.jdbc = jdbc;
		this.url = url;
		this.userName = userName;
		this.password = password;
	}

	public Connection getConnection() throws SQLException,
			ClassNotFoundException {
		Class.forName(jdbc);
		return DriverManager.getConnection(url, userName, password);
	}

	private int questionCount(String sql) {
		int questions = 0;
		for (int i = 0; i < sql.length(); i++) {
			if (sql.charAt(i) == '?')
				questions++;
		}
		return questions;
	}

	public ArrayList<HashMap<String, Object>> doSelect(String sql,
			ArrayList params) throws Exception {

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ArrayList<HashMap<String, Object>> returns = new ArrayList<HashMap<String, Object>>();

		try {
			conn = getConnection();
			pstmt = conn.prepareStatement(sql);

			if (params != null) {// if the params is null it means no ?
				for (int i = 0; i < params.size(); i++) {
					if (params.get(i).getClass() == Integer.class) {
						pstmt.setInt(i + 1, (Integer) params.get(i));
					} else if (params.get(i).getClass() == String.class) {
						pstmt.setString(i + 1, (String) params.get(i));
					} else if (params.get(i).getClass() == Date.class) {
						pstmt.setDate(i + 1, (Date) params.get(i));
					} else {
						// for some other types
					}
				}

				if (questionCount(sql) != params.size()) {
					throw new Exception();
				}
			}
			rs = pstmt.executeQuery();

			while (rs.next()) {
				HashMap<String, Object> temp = new HashMap<String, Object>();
				for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
					if (rs.getMetaData().getColumnType(i) == Types.VARCHAR) {
						temp.put(rs.getMetaData().getColumnName(i), rs
								.getString(i));
					} else if (rs.getMetaData().getColumnType(i) == Types.INTEGER) {
						temp.put(rs.getMetaData().getColumnName(i), rs
								.getInt(i));
					} else if (rs.getMetaData().getColumnType(i) == Types.DATE) {
						temp.put(rs.getMetaData().getColumnName(i), rs
								.getDate(i));
					} else if (rs.getMetaData().getColumnType(i) == Types.DOUBLE) {
						temp.put(rs.getMetaData().getColumnName(i), rs
								.getDouble(i));
					} else {// some type else;

					}
				}
				returns.add(temp);
			}
			rs.close();
			pstmt.close();
			conn.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (pstmt != null) {
					pstmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return returns;
	}

	public void closeConn(Connection conn) {
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void closeStmt(Statement stmt) {
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void closePstmt(PreparedStatement pstmt) {
		try {
			if (pstmt != null) {
				pstmt.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void closeRs(ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String args[]) {
		DBConnector test=new DBConnector();
		
		
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics