環境:
jdk1.4 + tomcat 5
一.環境變量:
path: c:/j2sdk1.4.2/bin;
java_home: c:/j2sdk1.4.2/
注:僅此兩個.
二。jdbc 驅動
copy 相應的jdbc驅動程式到tomcat5/common/lib
sql server 2000(或mysql) for jdbc/lib/目錄下三個.jar :
msbase.jar
mssqlserver.jar
msutil.jar
三.虛拟目錄與連接配接池
假設要在d:/jsgtest建自己的虛拟目錄
/myjsp
假設要建連接配接池jndi 名為aaa
假設連接配接資料庫為chengji(test資料庫中的表chengji)
則在
tomcat5/conf/catalina/localhost
目錄下建立檔案:
myjsp.xml
内容如下:
<context crosscontext="true" displayname="myjsp" docbase="d:/jsgtest" path="/myjsp" reloadable="true">
<resource name="aaa" type="javax.sql.datasource"/>
<resourceparams name="aaa">
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/test</value>
</parameter>
<name>maxactive</name>
<value>4</value>
<name>maxwait</name>
<value>5000</value>
<name>driverclassname</name>
<!-- 老驅動<value>org.gjt.mm.mysql.driver</value>-->
<value>com.mysql.jdbc.driver</value>
<name>username</name>
<value>root</value>
<name>password</name>
<value></value>
<name>maxidle</name>
<value>2</value>
</resourceparams>
</context>
測試:
在d:/jsgtest目錄下建檔案:test.jsp:
<%@ page contenttype="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
connection conn = null;
context initctx = new initialcontext();
if (initctx == null)
throw new exception("不能擷取context!");
context ctx = (context) initctx.lookup("java:comp/env"); //擷取連接配接池對象
object obj = (object) ctx.lookup("aaa"); //類型轉換
javax.sql.datasource ds = (javax.sql.datasource) obj;
conn = ds.getconnection();
statement stmt = conn.createstatement();
string strsql = "select * from chengji";
resultset rs = stmt.executequery(strsql);
while(rs.next()){
out.println(rs.getstring(1));
}
rs.close();
stmt.close();
conn.close();
out.println("連接配接池測試成功");
%>
測試路徑:
<a href="http://localhost:8080/myjsp/test.jsp">http://localhost:8080/myjsp/test.jsp</a>