一.web容器的配置
不同的web容器的配置都略有不同,具体见相关的官方文档:
Tomcat 6: [url]http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html[/url]
example: [url]http://icansoft.blog.51cto.com/268543/49993[/url]
Jetty 6: [url]http://docs.codehaus.org/display/JETTY/JNDI[/url]
[url]http://docs.codehaus.org/display/JETTY/DataSource+Examples[/url]
Resin 3: [url]http://www.caucho.com/resin-3.1/examples/db-jdbc/index.xtp[/url]
[url]http://www.caucho.com/resin-3.1/doc/database-tags.xtp[/url]
Tomcat 6: [url]http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html[/url]
example: [url]http://icansoft.blog.51cto.com/268543/49993[/url]
Jetty 6: [url]http://docs.codehaus.org/display/JETTY/JNDI[/url]
[url]http://docs.codehaus.org/display/JETTY/DataSource+Examples[/url]
Resin 3: [url]http://www.caucho.com/resin-3.1/examples/db-jdbc/index.xtp[/url]
[url]http://www.caucho.com/resin-3.1/doc/database-tags.xtp[/url]
二.Web.Xml配置
<
resource-ref
>
< description >Admin connect to DB </ description >
< res-ref-name >jdbc/honghu_admin </ res-ref-name >
< res-type >javax.sql.DataSource </ res-type >
< res-auth >Container </ res-auth >
</ resource-ref >
< description >Admin connect to DB </ description >
< res-ref-name >jdbc/honghu_admin </ res-ref-name >
< res-type >javax.sql.DataSource </ res-type >
< res-auth >Container </ res-auth >
</ resource-ref >
三.SqlBean类
package ucshop.common;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class SqlBean {
private static DataSource dataSource;
static {
init();
}
/*
* 私有构造函数,不能显式new实例,确保只有一个实例
*/
private SqlBean() { }
private static void init() {
try {
Context initContext = new InitialContext();
dataSource = (DataSource)initContext.lookup( "java:comp/env/jdbc/ucshop");
} catch(NamingException nameEx) {
System.out.println( "lookup失败");
nameEx.printStackTrace();
}
}
/**
* 打开数据库连接
* @return 数据库连接,失败则为null
*/
public synchronized static Connection getConnection(){
try {
return (dataSource != null)? dataSource.getConnection(): null;
} catch (SQLException sqlEx) {
System.out.println( "连接失败");
sqlEx.printStackTrace();
}
return null;
}
/**
* 关闭数据库连接
* @param conn
* @param statement
*/
public static void closeConnection(Connection conn, Statement statement, ResultSet resultSet) {
try{
if (resultSet != null){
resultSet.close();
resultSet = null;
}
if (statement != null){
statement.close();
statement = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch(SQLException sqlEx) {
System.out.println( "关闭数据库连接出错");
sqlEx.printStackTrace();
} finally {
try{
if (resultSet != null){
resultSet.close();
resultSet = null;
}
if (statement != null){
statement.close();
statement = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch(SQLException sqlEx) {
System.out.println( "关闭数据库连接还是出错");
sqlEx.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class SqlBean {
private static DataSource dataSource;
static {
init();
}
/*
* 私有构造函数,不能显式new实例,确保只有一个实例
*/
private SqlBean() { }
private static void init() {
try {
Context initContext = new InitialContext();
dataSource = (DataSource)initContext.lookup( "java:comp/env/jdbc/ucshop");
} catch(NamingException nameEx) {
System.out.println( "lookup失败");
nameEx.printStackTrace();
}
}
/**
* 打开数据库连接
* @return 数据库连接,失败则为null
*/
public synchronized static Connection getConnection(){
try {
return (dataSource != null)? dataSource.getConnection(): null;
} catch (SQLException sqlEx) {
System.out.println( "连接失败");
sqlEx.printStackTrace();
}
return null;
}
/**
* 关闭数据库连接
* @param conn
* @param statement
*/
public static void closeConnection(Connection conn, Statement statement, ResultSet resultSet) {
try{
if (resultSet != null){
resultSet.close();
resultSet = null;
}
if (statement != null){
statement.close();
statement = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch(SQLException sqlEx) {
System.out.println( "关闭数据库连接出错");
sqlEx.printStackTrace();
} finally {
try{
if (resultSet != null){
resultSet.close();
resultSet = null;
}
if (statement != null){
statement.close();
statement = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch(SQLException sqlEx) {
System.out.println( "关闭数据库连接还是出错");
sqlEx.printStackTrace();
}
}
}
}
附件:http://down.51cto.com/data/2351130
本文转自 Icansoft 51CTO博客,原文链接: http://blog.51cto.com/android/113379