⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbutils.java

📁 jsp技术的一个相关例子
💻 JAVA
字号:
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{
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -