首页>代码>java开源报表产品JReport的demo实例>/ereportDemo/src/com/yitong/struts/dao/SQLConnection.java
package com.yitong.struts.dao;

import java.io.IOException;
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.util.Properties;

public class SQLConnection {
	
	private Connection conn;
	private Statement stmt;
	private ResultSet rs;
	private PreparedStatement pstmt;
	
	private static SQLConnection sqlConnection;
	
	private SQLConnection(){
		initConn();
	}
	
	public static void main(String[] args) {
		SQLConnection dbaccess=SQLConnection.getInstance();
		Connection conn=null;
		Statement stat=null;
		ResultSet rs=null;
		try {
			conn = dbaccess.getConnection();
			conn.setAutoCommit(false);
			stat=sqlConnection.getConnection().createStatement();
			rs=stat.executeQuery("select top 1 id from T_PM_MaterialPurchasePlanDetails");
			if(rs.next()){
				System.out.println("--------------link is ok-:>"+rs.getString(1));
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {if(null!=rs){rs.close();}} catch (SQLException e) {e.printStackTrace();}
			try {if(null!=stat){stat.close();}} catch (SQLException e) {e.printStackTrace();}
			try {if(null!=conn){conn.close();}} catch (SQLException e) {e.printStackTrace();}
		}
	}
	
	
	public void initConn() {
		try {
			Properties property = new Properties();
			property.load(SQLConnection.class.getResourceAsStream("sql.properties"));
			String driverclass = property.getProperty("db.driver");
			String url = property.getProperty("db.url");
			String user = property.getProperty("db.user");
			String pass = property.getProperty("db.pass");
			//System.out.println("link datebase info:"+url+"\t"+user+"\t"+pass);
			Class.forName(driverclass);
			conn=DriverManager.getConnection(url, user, pass);
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
		}catch (IOException e1) {
			e1.printStackTrace();
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static SQLConnection getInstance() {
		if (null==sqlConnection) {
			sqlConnection = new SQLConnection();
			sqlConnection.initConn();
		}else{
			try{
				if(null==sqlConnection.getConnection() || sqlConnection.getConnection().isClosed()){
					sqlConnection = new SQLConnection();
					sqlConnection.initConn();
				}
//				else{
//					Statement stat=sqlConnection.getConnection().createStatement();
//					ResultSet rs=stat.executeQuery("select top 1 id from T_PM_MaterialPurchasePlanDetails");
//					if(rs.next()){
//						System.out.println("--------------link is ok");
//					}
//					rs.close();
//					stat.close();
//				}
			}catch(Exception e){
				sqlConnection = new SQLConnection();
				sqlConnection.initConn();
			}
		}
		return sqlConnection;
	}
	
	public Connection getConnection() throws SQLException {
		return conn;
	}
	
	public Statement createStatement(Connection conn) throws SQLException {
		Statement stmt = conn.createStatement();
		return stmt;
	}
	
	public PreparedStatement getPrepStmt(String sql) throws SQLException {
		pstmt = conn.prepareStatement(sql);
		return pstmt;
	}
	
	public void executeUpdate(String sql) throws SQLException {
		stmt.executeUpdate(sql);
	}
	
	public ResultSet executeQuery(String sql) throws SQLException {
		rs = stmt.executeQuery(sql);
		return rs;
	}
	
	public void closeCon() {
		try {
			if (null!=conn) {
				conn.close();
				conn = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void closeStmt() {
		try {
			if (null!=stmt) {
				stmt.close();
				stmt = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
最近下载更多
1358849392  LV21 2022年11月10日
zlmbr2012  LV3 2020年9月17日
wkc  LV21 2020年7月26日
jinzixian  LV1 2020年7月17日
沉默梦幻  LV1 2020年6月17日
qq415446566  LV1 2020年6月15日
zhouzh  LV9 2020年4月26日
liujiajun  LV7 2020年3月26日
chaoy_nx  LV8 2020年3月5日
java小书童  LV18 2019年12月19日
最近浏览更多
is_gary  LV15 2023年10月31日
pnews88  LV8 2023年6月30日
gyuhaoMM 2023年4月10日
暂无贡献等级
1358849392  LV21 2022年11月10日
abc562311934  LV4 2022年10月8日
wusiyin  LV14 2022年9月15日
杠上炮  LV6 2022年6月28日
crosa_Don  LV18 2022年3月31日
来恬爸爸晋亚阳  LV3 2021年10月29日
tmdgdx  LV9 2021年9月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友