昨天搞好了下面这个3个东西,相信大家一看就明白了:3中链数据库的途径。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<html>
<body>
<%
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/test","root","123456");
Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from stu";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{%>
<%=rs.getString("xh")%>
<%=rs.getString("xm")%>
<%}%>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------------------
server.xml
<GlobalNamingResources>
<!-- Test entry for demonstration purposes -->
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users -->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="JDBC for MySQL"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
username="root"
password="123456"
maxIdle="2"
maxWait="5000"
url="jdbc:mysql://localhost/test"
maxActive="4"/>
</GlobalNamingResources>
contex.xml
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<ResourceLink name="JDBC for MySQL" global="JDBC for MySQL" type="javax.sql.DataSource"/>
</Context>
web.xml
<resource-ref>
<description>Connect Mysql</description>
<res-ref-name>JDBC for MySQL</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Test of MySQL connection pool</title>
</head>
<body>
<%
out.print("Start<br/>");
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("JDBC for MySQL");
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
out.print("MySQL connection pool runs perfectly!");
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql="select * from stu";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{%>
<%=rs.getString("xh")%>
<%=rs.getString("xm")%>
<br>
<%}%>
<%
conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------------
struts-config.xml
<data-sources>
<data-source key="A" type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName" value="org.gjt.mm.mysql.Driver" />
<set-property property="url" value="jdbc:mysql://localhost/test" />
<set-property property="username" value="root" />
<set-property property="password" value="123456" />
</data-source>
</data-sources>
三个需要的程序包:
commons-dbcp-1.2.1.jar
http://apache.linuxforum.net/dist/jakarta/commons/dbcp/binaries/
struts-legacy.jar
http://apache.linuxforum.net/dist/jakarta/struts/struts-legacy/
commons-pool-1.2.jar
http://apache.linuxforum.net/dist/jakarta/commons/pool/binaries/
H:\java\Tomcat 5.5\common\lib\mysql-connector-java-3.0.16-ga-bin.jar;
------------------------------------------------------------------------------------------------------------------------------------------
今天开始j2ee构架
但是星期六又要期末考试,哎----烦呀,这日子简直没法活了,中国的程序员,不是人才,而是奴隶,正的是。