Thursday May 17, 2007
记录一下, 这里只给出server.xml的配置方法,其它的配置及测试代码在 这里
<logger className="org.apache.catalina.logger.FileLogger"
directory="logs" prefix="localhost_log." suffix=".txt"
timestamp="true"/>
<context docBase="cmmi" path="/cmmi"
reloadable="true" crossContext="true">
<logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_lovehome_log."
suffix=".txt" timestamp="true"/>
<resource name="jdbc/access" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
driverClassName="sun.jdbc.odbc.JdbcOdbcDriver"
username="sa" password="12345"
url="jdbc:odbc:work"
defaultAutoCommit="true" removeAbondoned="true"
removeAbondonedTimeout="60" logAbondoned="true"/>
</context>
Thursday March 29, 2007
环境
xp + jdk1.5 + tomcat5.5.23 + eclipse3.2.2 + myeclipse5.1.1GA + sqlserver2000
使用连接池方式连接数据库,至于这方面的问题可参考 http://roamlog.cn/?p=39
写一个DBConnection.java封装对数据库的一些操作,实现SqlTestDS的getConnection()方法连接数据库
DBConnection.java
package database;
import java.sql.*;
public class DBConnection{
private Connection conn = null;
private Statement stmt = null;
ResultSet rs = null;
public ResultSet executeQuery(String sql) {
try {
conn =SqlTestDS.getConnection();
stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
public void executeUpdate(String sql) {
try {
conn = SqlTestDS.getConnection();
stmt =conn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeStmt() {
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeConn() {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Continue reading »
Wednesday March 28, 2007
前些天我也写过一篇类似的文章,关于连接池配置的,今天我重新写过一篇,介绍不同的配置方法
环境搭建我就不多说了,我的环境是:
xp + jdk1.5 + tomcat5.5.23 + eclipse3.2.2 + myeclipse5.1.1GA + sqlserver2000
首先写个连接数据源的java类
SqlTestDS.java
package database;
import javax.sql.DataSource;
import javax.naming.*;
import java.sql.*;
public class SqlTestDS {
private static DataSource ds = null;
static {
init();
}
private static void init() {
try {
// 1、创建命名服务环境
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("No Context");
// 2、从JNDI中查询数据源对象
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/sqlserver");
} catch (Exception e) {
e.printStackTrace();
}
}
// 将构造函数定义为private权限是为了保证全局只有一个SqlTestDS实例
// 也就是实现了单例模式
private SqlTestDS() {
}
public static Connection getConnection() throws SQLException {
if (ds == null) {
throw new SQLException("数据源对象为空!");
} else {
// 3、从数据源中获取数据库连接
return ds.getConnection();
}
}
}
Continue reading »
Tuesday February 27, 2007
配置server.xml,在< /host > 标签前加入
<context
docBase="C:/eclipse/workspace/myweb/hello"
path="/hello"
reloadable="true"
<resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="123456"
url="jdbc:mysql://localhost/testmysql"
defaultAutoCommit="true" removeAbondoned="true"
removeAbondonedTimeout="60" logAbondoned="true"/>
</context>
配置web.xml
<resource-ref>
<res-ref-name>jdb/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>