第二步: 配置数据库
1.首先创建数据联接bean, 并打包到 ConnectionPool, 它将从我们配置的联接池中取得可用联接.
// Java Document
package ConnectionPool;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
import java.io.*;
import java.util.*;
public class getDbConnection {
Connection conn;
Statement stmt;
ResultSet rs=null;
String foo = "Not Connected";
int bar = -1;
public getDbConnection() {
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/JDBC/TestDB");
if (ds != null) {
conn = ds.getConnection();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public Connection getCon()
{
return conn;
}
public void d_close() throws SQLException
{
if (rs != null) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) { ; }
stmt = null;
}
if (conn != null) {
try { conn.close(); } catch (SQLException e) { ; }
conn = null;
}
}
protected void finalize() throws Throwable
{
try { d_close(); } catch (SQLException e) { ; }
}
}