操作系统:windows XP SP1
1.安装JDK 5.0 update 1
下载:http://java.sun.com/
假设安装路径为 D:/Java/jdk1.5.0_01
设置环境变量(控制面板->系统->高级)
JAVA_HOME=D:/Java/jdk1.5.0_01
classpath=.;D:/Java/jdk1.5.0_01/lib/dt.jar;D:/Java/jdk1.5.0_01/lib/tools.jar;
path=path;%JAVA_HOME%/bin
2.安装Tomcat 5.5.4
下载:http://jakarta.apache.org/site/binindex.cgi
(http://apache.freelamp.com/jakarta/tomcat-5/v5.5.4/bin/jakarta-tomcat-5.5.4.exe)
安装到d:/tomcat 5.5,安装Tomcat 5.5需要 JRE 5.0 ,安装过程中如果没有自动识别第一安装的jdk5.0的路径,需要手动指定JRE5.0的路径.
设置环境变量
CATALINA_HOME=D:/Tomcat 5.5
-------------------------------------------
配置D:/Tomcat 5.5/conf/server.xml
-------------------------------------------
在<host></host>之间加上下面的配置信息
<Context path="/DBTest" docBase="D:/DBTest" debug="1" reloadable="true">
<Resource name="jdbc/sqlserver" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test" username="sa"
password="password" maxActive="20" maxIdle="10" maxWait="-1" />
</Context>
其中path设置虚拟目录的名字,docBase为系统中的实际路径
<Resource>里配置了连接池的相关参数
注意将SQL Server的JDBC驱动(msutil.jar,msbase.jar,mssqlserver.jar)放到D:/Tomcat 5.5/common/lib或者D:/DBTest/WEB-INF/lib目录下
D:/DBTest的目录结构
|-----WEB-INF----web.xml
|-----classes
|-----lib
-------------------------------------------
配置D:/DBTest/WEB-INF/web.xml
-------------------------------------------
在<web=app></web-app>之间加上
<resource-ref>
<description>sqlserver</description>
<res-ref-name>jdbc/sqlserver</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
配置好后重新启动Tomcat
-----------------------------------------
JSP测试代码D:/DBTest/testdb.jsp
-----------------------------------------
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="java.sql.*,javax.sql.DataSource,javax.naming.*"%>
<html>
<head><title>DBCP</title></head>
<body bgcolor="#ffffff">
<h1>test Tomcat</h1>
<%
try
{
Context initCtx=new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/connectDB");
Connection conn=ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs =stmt.executeQuery("select * from test");
while(rs.next()) {%>
First:<%=rs.getString(1)%>
<%}%>
<%out.print("Successful!/n");%>
<%
rs.close();
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
注意先在sql server中建好相应的测试数据
好了,现在在浏览器中访问http://localhost:8080/DBTest/testdb.jsp,大功告成
附DatabaseConn.java:
* Created on Jan 4, 2006
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.publicClass;
import java.sql. * ;
import javax.naming. * ;
import javax.sql.DataSource;
/**
* @author dengtao 数据库连接工具类 Preferences - Java - Code Style - Code Templates
*/
public class DatabaseConn {
public static synchronized Connection getConnection() throws Exception {
return getConnectionEx();
}
public static synchronized Connection getConnectionEx() throws Exception {
Connection con = null ;
int i = 0 ;
while (con == null ) {
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup( " java:comp/env " );
DataSource ds = (DataSource) envCtx.lookup( " jdbc/sqlServer " );
con = ds.getConnection();
} catch (SQLException e) {
// e.printStackTrace();
} catch (NamingException f) {
// f.printStackTrace();
} // catch (java.util.NoSuchElementException w) {
// w.printStackTrace();
// }
i ++ ;
if (i > 100 ){
System.out.println( " 连接池获取连接超时 " );
return null ;
}
if (con == null ) {
System.out.println( " 连接池未能得到连接,正等待 " + i);
Thread.sleep( 1000 );
}
}
return con;
}
public static synchronized Connection getConnectionOld() throws Exception {
Connection con = null ;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup( " java:comp/env " );
DataSource ds = (DataSource) envCtx.lookup( " jdbc/sqlServer " );
con = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException f) {
f.printStackTrace();
}
if (con == null )
System.out.println( " 连接池未能得到连接 " );
return con;
}
}
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.publicClass.DatabaseConn;
import com.publicClass.ConstValue;
public class LoginService {
/**
* @param args
*/
public static int checkLogin(String fUserName,String fPassword){
int checkResult = ConstValue.LOGIN_FAILED ;
if (fUserName == null || fPassword == null || fUserName.equals( "" ) || fPassword.equals( "" ) ){
return checkResult;
}
Connection con = null ;
try {
con = DatabaseConn.getMemConnection();
Statement stm = con.createStatement();
String SqlStr = " select * from t_MemberInfo where username=' "
+ fUserName.trim() + " ' " ;
ResultSet result = stm.executeQuery(SqlStr);
if (result.next()) {
if ( ! fPassword.equals(result.getString( " password " ).trim())) {
checkResult = ConstValue.LOGIN_FAILED;
} else {
// checkResult = ConstValue.LOGIN_SUCC;
if (result.getInt( " enabled " ) > 0 )
checkResult = result.getInt( " id " );
else {
checkResult = ConstValue.LOGIN_USER_DISABLED ;
}
}
} else {
checkResult = ConstValue.LOGIN_NOUSER;
}
} catch (Exception e) {
System.out.println(e);
checkResult = ConstValue.EXCEPTION_CODE;
} finally {
try {
if (con != null )
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return checkResult;
}
}