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

//===========================================================================
// import
//===========================================================================
import java.sql.*;
import java.util.*;
import java.io.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.Action;

import javax.naming.*;
import javax.sql.*;
/**
 *<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";
//	/**
//	 * property file name
//	 */
//	private static final String propertyFileName = "/bugslife.properties";
	/**
	 * Log
	 */
    private static Log log = null;
	
//===========================================================================
// methods
//===========================================================================
	static {
		log = LogFactory.getLog(PoolingManager.class.getName());
	}
//	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() {
//		try {
//			Properties prop = new Properties();
//			prop.put("user", m_user);
//			prop.put("password", m_password);
//			prop.put("characterEncoding", "Shift_JIS");
//			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;

		Connection conn = null;
		try{
			Context ctx=new InitialContext();
			DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bugslifeDB");
			conn = ds.getConnection();
			//本来はプーリングする時点でAutoCommitをきるべきだが、
			//設定方法がわからないのでここで解除する。
			conn.setAutoCommit(false); 
		
		} catch (NamingException e ) {
		    log.fatal(e);
			e.printStackTrace();
		} catch (SQLException e ) {
		    log.fatal(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) { }
		}
	}
	
}
