Posted on 2007-03-20 14:47
skycity 阅读(615)
评论(0) 编辑 收藏 所属分类:
基本开发
请在你的tomcat安装目录中找到conf\Catalina\localhost子目录. 新建 mystruts.xml
加入以下代码
<Context path="/mystruts" docBase="mystruts" debug="0" privileged="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>root</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/sjsn?autoReconnect=true</value>
</parameter>
</ResourceParams>
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_admin_log." suffix=".txt"
timestamp="true"/>
</Context>
再在jsp中输入
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
Connection conn;
Statement stmt;
ResultSet rs;
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
conn=ds.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from user");
while(rs.next()){
out.println(rs.getString(2)+"<br>");
}
%>即可进行数据库查询
Lyyb2001