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();
}
分享到:
相关推荐
一路成长一路歌.doc
一路成长一路歌精选.doc
给成长一把梯子.doc
一门具有良好电子知识 的理论 技能 、一种做 要到最 好的工作兴趣、一份勤奋和努力行动种细 好的工作兴趣、一份勤奋和努力行动种细 好的工作兴趣、一份勤奋和努力行动种细 心和认真的工作态度 。
2021_Master-ed 一个安全的地方成长一个勇敢的地方成长genkiness 为了帮助塞勒姆或公立学校的糖尿病阅读障碍二年级学生,我们指定黄色作为我们的起点。 我大喊哦! 对于一个开始接受新生活方式的二年级学生来说,这...
部编版三年级上册道德与法治第一单元第一课学习伴我成长 课件.pptx
一个计算机高手的成长
部编版四年级道德与法治第一单元《与班级共成长》教学设计.pdf
2020年七年级道德与法治上册 第一单元 成长的节拍 第一课 中学时代知识梳理 新人教版.doc
象征美好未来,一颗小树苗的成长,一个树苗长成了一颗大树ppt动画模板。
企业成长与激励机制第一讲:企业成长与百年老店.ppt
孩子的成长只有一次,你想为你的宝宝作一套完整的成长记录吗?记录孩子从小到大成长的历程;留住纯真,记录成长过程,让孩子长大以后能和你一起从这里找到童年时美好的回忆。为此,今天就为大家推荐一款记录儿童成长...
宝宝成长档案是一款记录宝宝成长的软件,可以添加编辑宝宝的变化,可以给宝宝上传图片记录精彩时刻。还可以备份宝宝的影音视频,给宝宝最贴心的关怀。让宝宝健健康康的成长。喜欢的朋友快来下载使用吧! 软件功能:...
教你写成长礼给孩子一封信
一路走过,一路成长.doc
《一个Delphi高手的成长之路》电子书。
人格结构理论分析《一个人的朝圣》中哈罗德的成长历程.pdf
20210903-浙商证券-隆利科技-300752-MiniLED多元化应用启动新一轮成长曲线!.pdf
一个java程序员的成长历程
一位电脑高手的成长史.docx