成长(一)

[code]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();


}
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值