?? dbutils.java
字號(hào):
package com.qiming.jspch20.model.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DButils {
private static DataSource dataSource;
static{
InputStream input=null;
try {
input = DButils.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties props =new Properties();
props.load(input);
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(input!=null){
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private Connection con =null;
public DButils(boolean requriedTx) throws SQLException{
try {
con = dataSource.getConnection();
if(requriedTx){
con.setAutoCommit(false);
}
} catch (SQLException e) {
throw e;
}
}
public void commit(boolean iscommit) throws SQLException{
if(iscommit){
con.commit();
}else{
con.rollback();
}
}
public void free(){
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void excuteSQl(String sql,Object[] values,String[] types)throws SQLException{
if(values==null||types==null){
return;
}
if(values.length!=types.length){
return;
}
PreparedStatement pst =null;
try {
pst = con.prepareStatement(sql);
for(int i=0;i<values.length;i++){
if("String".equals(types[i])){
pst.setString(i+1,(String)values[i]);
continue;
}
if("Integer".equals(types[i])){
pst.setString(i+1,(String)values[i]);
continue;
}
}
pst.execute();
} catch (SQLException e) {
throw e;
}finally{
if(pst!=null){
pst.close();
}
}
}
public void excuteWithResult(String sql) throws SQLException{
Statement st =null;
ResultSet rst =null;
try {
st = con.createStatement();
rst = st.executeQuery(sql);
mapData(rst);
} catch (SQLException e) {
throw e;
}finally{
if(rst!=null){
rst.close();
}
if(st!=null){
st.close();
}
}
}
public void mapData(ResultSet rst)throws SQLException{
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -