//===========================================================================
// package
//===========================================================================
package jp.gr.java_conf.bugslife.db;

//===========================================================================
// import
//===========================================================================
import java.sql.*;
import java.util.*;
import java.io.*;
import org.apache.commons.logging.*;
/**
 *<PRE>
 * 
 * PoolongManagerクラス
 *
 *</PRE>
 * @version	0.0, 2001/09/10
 * @author	鰐川　正寛
 */
public class PoolingManager
{
//===========================================================================
// attributes
//===========================================================================
	/**
	 *	jdbc:mysql://[hostname][:port]/[dbname]
	 */
//	private static String m_uri = "jdbc:mysql://localhost:3306/BUGS";
	private static String m_uri = "jdbc:mysql://localhost:3306/UT3";
	/**
	 *	login user
	 */
//	private static String m_user = "wakame";
//	private static String m_password = "wakame";
	private static String m_user = "wani";
	private static String m_password = "wani";
	/**
	 *	driver name
	 */
//	private static String m_driverName = "org.gjt.mm.mysql.Driver";
	private static String m_driverName = "gwe.sql.gweMysqlDriver";
	/**
	 * Log
	 */
	private Log log = LogFactory.getLog(this.getClass().getName());
	/**
	 * property file name
	 */
	private static final String propertyFileName = "/bugslife.properties";
//===========================================================================
// methods
//===========================================================================
	static {
		try {
			//propertyの取得
			InputStream in = PoolingManager.class.getResourceAsStream(propertyFileName);
			Properties prop = new Properties();
			prop.load(in);
			m_uri = prop.getProperty("db.uri");
			m_user = prop.getProperty("db.user");
			m_password = prop.getProperty("db.password");
			m_driverName = prop.getProperty("db.driverName");
			
			Class.forName(m_driverName);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection() {
		Connection conn = null;
		try {
			Properties prop = new Properties();
			prop.put("user", m_user);
			prop.put("password", m_password);
			prop.put("characterEncoding", "Shift_JIS");
//			prop.put("add5CAfter5C", "true");
                        //delete start 07/10/5 isida オートコミットを解除するため
//			prop.put("autoCommit", "false");
                        //delete end 07/10/5 isida オートコミットを解除するため
			prop.put("debugSQL", "true");			
			conn = DriverManager.getConnection(m_uri, prop);
                        //insert start 07/10/5 isida オートコミットを解除するため
			conn.setAutoCommit(false); //オートコミットを解除する。
                        //insert end 07/10/5 isida オートコミットを解除するため
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void main(String[] args) {
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rset = null;
		
		String testSql = "select * from userinfo";
		
		try {
			System.out.println("Creating connection.");
			conn = PoolingManager.getConnection();
			System.out.println("Creating statement.");
			stmt = conn.createStatement();
			System.out.println("Executing statement.");
			rset = stmt.executeQuery(testSql);
			System.out.println("Results:");
			int numcols = rset.getMetaData().getColumnCount();
			while(rset.next()) {
			for(int i=1;i<=numcols;i++) {
			    System.out.print("\t" + rset.getString(i));
			}
			System.out.println("");
		    }
		} catch(SQLException e) {
		    e.printStackTrace();
		} finally {
		    try { rset.close(); } catch(Exception e) { }
		    try { stmt.close(); } catch(Exception e) { }
		    try { conn.close(); } catch(Exception e) { }
		}
	}
	
}
