Jdk + jdbc + tomcat 缁煎????缃?
JDBC?版??搴?杩??ャ????
????绯葷???缃?锛?windowsXp/????qlserver2000+SP3/JDK5.0/TOMCAT5.58/sqlserver2000 driver for JDBC
????绗?涓?姝ワ?----瀹?瑁?J2SDK:
??????UN瀹??圭???癸?http://java.sun.com/j2se锛??辨??缃?绔?锛?涓?杞?2SDK??瀹?瑁???浠躲??????????楂???????JDK 5.0 Update 2??
????瀹?瑁??舵??涓や釜???╁??瑁?璺?寰???椤甸???绗?涓?涓?瀹?瑁?璺?寰???浠ラ??渚塊????浠?灏辨?懼??:/JDK5.0涓?濂戒???绗?浜?涓?灏辯?ㄩ?璁よ礬寰?锛????ㄥ????
????瀹?瑁?瀹?姣???锛?瑕?璁劇疆??澧?????
?????????佃??---灞???--楂?绾?--??澧?????锛?
?????ㄤ??㈢??妗?妗?---绯葷?????(S)???闆緩涓や釜绯葷???????
????涓?涓?JAVA_HOME
?????ㄥ??????涓?杈??ワ?JAVA_HOME锛?娉ㄦ??澶у??锛?锛??ㄥ?????間腑杈??ワ?C:/JDK5.0
????涓?涓?CLASSPATH
?????ㄥ??????涓?杈??ワ?CLASSPATH锛?娉ㄦ??澶у??锛?锛??ㄥ?????間腑杈??ワ?D:/JDK5.0/bin;.;D:/JDK5.0/lib;D:/JDK5.0/lib/dt.jar;D:/JDK5.0/lib/tools.jar;
?????闆緩涓や釜绯葷???????锛?杩?瑕??ㄥ???ュ氨????涓?涓?绯葷?????PATH???????兼?????㈠??C:/JDK5.0/bin;
????杩???DK灏卞??瑁?濂戒?锛???浠???浠ユ?闆緩涓?D:/涓??闆緩涓?璁頒???锛??ㄩ???㈡?插?ヤ??㈣?娈典唬???ㄦ?ユ?楠?JDK??????瑁???????
????public class test{
??????public static void main(String args[]){
????????System.out.println("this is a test program.");
??????}
????}
?????插??淇?瀛?锛??存?規??浠跺??涓?est.java
????娉ㄦ??1锛???濂借??宸辨?蹭?????锛??ㄧ?椤典??存?ュ??跺???戒??虹?拌??娉???璇???
????娉ㄦ??2锛?绗?涓??ヤ腑public class test??test瑕?璺?java??浠剁????浠跺??涓??鎬??鳳?澶у???涔?瑕??稿??锛???????璇???
????寮?濮?????杩?琛?? 杈???md杩???OS?戒護??绀虹??????
?????ㄧ???d腑杈??モ??D:????杞?
????杈??モ??javac test.java????杞?
????杈??モ??java test????杞?
????濡????劇ず?衡??this is a test program.????琛ㄧずJDK瀹?瑁???????
????绗?浜?姝ワ?----瀹?瑁?Tomcat:
??????omcat瀹??圭????ttp://jakarta.apache.org/site/downloads/index.html锛???omcat Downloads????Tomcat5锛????????扮??????5.5.9??
????瀹?瑁???:/Tomcat5.5涓?
????TOMCAT5.5浠ヤ???????DK1.5浠ヤ?????瀹?瑁???????????涓?锛??????ㄧ郴缁?????????浠諱??存?廣??
????瀹?瑁?瀹?姣???锛?浣???浠ュ?ㄧ郴缁????????頒?涓?TOMCAT?????″?ㄣ??
??????E??寮?Tomcat Downloads????Tomcat5锛?http://localhost:8080椤甸????ヨ?界????omcat??榛?璁?SP椤甸?????璇存??TOMCAT瀹?瑁?????锛?
????绗?涓?姝ワ?----瀹?瑁?JSP璁塊??SQL Server 2000??椹卞?ㄧ?搴?JDBC:
????JDBC涓?杞藉?ㄥ井杞???瀹??歸〉锛?http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=86212D54-8488-481D-B46B-AF29BB18E1E5锛?涓?杞姐??
????涓?杞藉??瑁?????榛?璁よ礬寰???C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC
?????舵?稿???涓?涓???浠舵??C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib璺?寰?涓???涓?涓?.JAR??缂╂??浠?
????绗?涓?姝ユ??浠??ㄧ??澧?????涓??闆緩??绯葷?????CLASS_PATH???????間腑杩藉??
????C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib/msbase.jar;C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib/mssqlserver.jar;C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib/msutil.jar;
?????ョ??灏?msbase.jar??mssqlserver.jar??msutil.jar?瘋??:/Tomcat 5.5/common/lib
?????跺?????闆????OMCAT???″???
?????ㄤ?娈典唬???ユ?璇??版??搴?杩??ユ????涓?????
<%@? ? page? ? contentType="text/html;charset=gb2312"%>? ? ? ? ? ? ? ? ?
<%@? ? page? ? import="java.sql.*"%>? ? ? ? ?
<html>? ? ? ? ? ? ? ? ?
<body>? ? ? ? ? ? ? ? ?
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();? ? ? ? ? ? ? ? ?
String? ? url="jdbc:microsoft:sqlserver://127.0.0.1:4133;DatabaseName=pubs";? ? ? ?//pubs涓?QLSERVER2000??榛?璁ゆ?版??搴?? ? ? ? ?
String? ? user="sa";
//??sa?挎?㈡??浣???SQLSERVER2000?ㄦ?峰??? ? ? ? ?
String? ? password="sa";
//??sa?挎?㈡??浣???SQLSERVER2000瀵???? ? ? ? ?
Connection? ? conn=? ? DriverManager.getConnection(url,user,password);? ? ? ? ? ? ? ? ?
Statement? ? stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);? ? ? ? ? ? ? ? ?
String? ? sql="select? ? *? ? from? ? titles";
//titles涓?QLSERVER2000??榛?璁ゆ?版??搴?pubs涓???榛?璁よ〃? ? ? ? ? ? ? ? ?
ResultSet? ? rs=stmt.executeQuery(sql);? ? ? ? ? ? ? ? ?
while(rs.next())? ? {%>? ? ? ? ? ? ? ? ?
?ㄧ??绗?涓?涓?瀛?娈靛??瀹逛負锛?<%=rs.getString(1)%>? ? ? ? ? ? ? ? ?
?ㄧ??绗?浜?涓?瀛?娈靛??瀹逛負锛?<%=rs.getString(2)%>? ? ? ? ? ? ? ? ?
<%}%>? ? ? ? ? ? ? ? ?
<%out.print("?版??搴???浣?????锛?????浣?");%>? ? ? ? ? ? ? ? ?
<%rs.close();? ? ? ? ? ? ? ? ?
stmt.close();? ? ? ? ? ? ? ? ?
conn.close();? ? ? ? ? ? ? ? ?
%>? ? ? ? ? ? ? ? ?
</body>? ? ? ? ? ? ? ? ?
</html>
????娉??缃?涓?????浠g????<%=rs.getString(2)%>? ??涓??????鳳?姝g‘浠g????搴?璇ュ?繪?????楓??
Tomcat?版??缂??叉?杩???
????绗?涓?姝ワ?----??缃?Tomcat
??????omcat 5.5/conf/server.xml??<GlobalNamingResources>涓?娣誨??锛?
????<Resource name="JDBC for MySQL" type="javax.sql.DataSource" password="sa" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" maxIdle="2" maxWait="5000" username="sa" url="jdbc:microsoft:sqlserver://127.0.0.1:4133;DatabaseName=pubs" maxActive="4"/>
????????杩?Tomcat Administration?婚???剁?$???????骞剁??esources->Data Sources->Create New Data Source??寤烘?扮???版??婧???
Property Value
JNDI Name:? JDBC for MySQL
Data Source URL:? jdbc:microsoft:sqlserver://127.0.0.1:4133;DatabaseName=pubs
JDBC Driver Class:? com.microsoft.jdbc.sqlserver.SQLServerDriver
User Name:? sa
Password:? sa
Max. Active Connections:? 4
Max. Idle Connections:? 2
Max. Wait for Connection:? 5000
Validation Query:? ??
?
?????ョ??????ave????淇?瀛?锛?骞跺????ommit Changes灏??存?規?存?拌??omcat???″?ㄣ??
??????omcat 5.5/webapps/[椤圭????绉?/WEB-INF/web.xml??Tomcat 5.5/conf/web.xml??<web-app>涓?娣誨??锛?
????<resource-ref>
?? ???? <description>MySQL Connection Pool</description>
?? ???? <res-ref-name>JDBC for MySQL</res-ref-name>
????????<res-type>javax.sql.DataSource</res-type>
????????<res-auth>Container</res-auth>
???? </resource-ref>
??????omcat 5.5/webapps/[椤圭????绉?/META-INF/context.xml??Tomcat 5.5/conf/context.xml??<Context>涓?娣誨??锛?
????<ResourceLink name="JDBC for MySQL" global="JDBC for MySQL" type="javax.sql.DataSource"/>
????绗???姝ワ?----娴?璇?
????????Tomcat???″?????涓?涓?test.jsp锛?
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Test of SQL Server connection pool</title>
</head>
<body>
<%
out.print("Start
");
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("SQL Server connection pool runs perfectly!");%>
<%
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from titles";
//titles涓?QLSERVER2000??榛?璁ゆ?版??搴?pubs涓???榛?璁よ〃
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
?ㄧ??绗?涓?涓?瀛?娈靛??瀹逛負锛?<%=rs.getString(1)%>
?ㄧ??绗?浜?涓?瀛?娈靛??瀹逛負锛?<%=rs.getString(2)%>
<%}%>
<%out.print("?版??搴???浣?????锛?????浣?");%>
<%rs.close();
stmt.close();
conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html>
Log4j?ュ?绠$??
1??棣???灏?log4j-1.2.9.jar?????ョ被璺?寰?涓???
2??Log4J蹇?椤誨?ㄥ??ㄧ???跺??浠g???ц???瀹?????濮??????跺??濮?????瀹瑰?涓?锛?
import java.io.*;
import javax.servlet.*;
import org.apache.log4j.*;
public class Log4JInit extends HttpServlet {
public void init() throws ServletException {
String prefix = getServletContext().getInitParameter("SERVLET_ROOT_PATH");
String file = getServletConfig().getInitParameter("LOG4J_FILE");
// 浠?Servlet???拌?誨??log4j????缃???浠?
if (file != null) {
PropertyConfigurator.configure(prefix + file);
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)throws
IOException, ServletException {}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws
IOException, ServletException {}
}
3??web.xml??瀹歸??缃?濡?涓?锛??蜂?????浠舵?懼??璺?寰?锛?/webapps/[椤圭????绉?/WEB-INF/web.xml??Tomcat 5.5/conf/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
" http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>globalparameter</servlet-name>
<servlet-class>edu.common.GlobalParameter</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>globalparameter</servlet-name>
<url-pattern>/globalparameter</url-pattern>
</servlet-mapping>
<context-param>
<param-name>SERVLET_ROOT_PATH</param-name>
<param-value>C:/Java/Tomcat 5.5/webapps/ROOT/WEB-INF/classes</param-value>
</context-param>
<context-param>
<param-name>LOG4J_FILE</param-name>
<param-value>/log4j.properties</param-value>
</context-param>
........
</web-app>
娉ㄦ??锛?涓??㈢??load-on-startup搴?璁句負1锛?浠ヤ究??eb瀹瑰?ㄥ???ㄦ?跺?寵??ヨ??ervlet??log4j.properties??浠舵?懼?ㄦ?圭??properties瀛???褰?涓?锛?涔???浠ユ??瀹??懼?ㄥ?跺????褰?涓???搴?璇ユ??.properties??浠堕??涓?瀛??撅?杩??鋒?逛究绠$????
4??log4j.properties????缃?,?蜂???璺?寰???webapps/椤圭????绉?WEB-INF/log4j.properties
?ㄩ??缃???浠朵腑??og4j.properties涓?杩??瘋?劇疆锛?
# ?朵腑锛?Log4j??渚???appender??浠ヤ???绉?锛?
# org.apache.log4j.ConsoleAppender锛??у?跺?幫?锛?
# org.apache.log4j.FileAppender锛???浠訛?锛?
# org.apache.log4j.DailyRollingFileAppender锛?姣?澶╀駭??涓?涓??ュ???浠訛?锛?
# org.apache.log4j.RollingFileAppender锛???浠跺ぇ灏??拌揪??瀹?灏哄?哥???跺??浜х??涓?涓??扮????浠訛?锛?
# org.apache.log4j.WriterAppender锛?灏??ュ?淇℃??浠ユ??煎??????頒換????瀹????版?癸?
# ??缃??ュ?淇℃?????煎?锛?甯?灞?锛?锛??惰??娉?涓猴?
# log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
# log4j.appender.appenderName.layout.option1 = value1
# log4j.appender.appenderName.layout.option = valueN
# ?朵腑锛?Log4j??渚???layout??浠ヤ???绉?锛?
# org.apache.log4j.HTMLLayout锛?浠?TML琛ㄦ?煎艦寮?甯?灞?锛?锛?
# org.apache.log4j.PatternLayout锛???浠ョ?墊椿?版??瀹?甯?灞?妯″?锛?锛?
# org.apache.log4j.SimpleLayout锛??????ュ?淇℃????绾у????淇℃??瀛?绗?覆锛?锛?
# org.apache.log4j.TTCCLayout锛??????ュ?浜х?????堕?淬??绾跨???绫誨??绛?绛?淇℃??锛?
#璁劇疆???圭??褰?锛?????????debug浠ヤ?绾у????淇℃???芥???闆?烘?ュ?版?ュ???浠朵腑
log4j.rootLogger=debug, stdout
#璁劇疆涓?ebug,琛ㄧず???芥???闆??ebug绾у????淇℃??
log4j.logger.debug=debug, R
#璁劇疆??info,琛ㄧず???芥???闆??nfo绾у????淇℃??
log4j.logger.info=info,s
#璁劇疆??error锛?琛ㄧず???芥???闆??rror绾у????淇℃??
log4j.logger.error = error,k
#杈???ebug绾у??浠ヤ???淇℃???版?у?跺??
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=(%F:%L) - %m%d%n
#????ebug绾у????淇℃????ebug.log
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=c:/debug.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern='.'yyyy-MM-dd'.txt'
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%F:%L] - %m /r/n
#????nfo绾у????淇℃????nfo.log
log4j.appender.s=org.apache.log4j.RollingFileAppender
log4j.appender.s.File= c:/info.log
log4j.appender.s.Append = true
# Control the maximum log file size
log4j.appender.s.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.s.MaxBackupIndex=1
log4j.appender.s.layout=org.apache.log4j.PatternLayout
log4j.appender.s.layout.ConversionPattern=%p %t %c - %m%d%n
#????rror绾у????淇℃????rror.log涓?
log4j.appender.k=org.apache.log4j.DailyRollingFileAppender
log4j.appender.k.File=C:/error.log
log4j.appender.k.Append = true
log4j.appender.k.DatePattern='.'yyyy-MM-dd'.txt'
log4j.appender.k.layout=org.apache.log4j.PatternLayout
log4j.appender.k.layout.ConversionPattern=%d [%F:%L] - %m /r/n
5????lass绫諱腑????璁劇疆濡?涓?锛?
static Logger debugLog=Logger.getLogger("debug")
static Logger infoLog=Logger.getLogger("info")
static Logger errorLog=Logger.getLogger("error")
杩??蜂???绾у????log淇℃??灏卞??浠ユ???ㄤ?????log??浠朵腑渚?濡?:
debugLog.debug("灏辨???闆?頒??㈢??debuginfo涓?");
InfoLog.info("灏辨???闆?頒??㈢??infoLog涓?");
灞???omcat??褰???琛ㄥ????
缂虹?????典?锛?濡???浣?璁塊??tomcat涓???涓?涓?web搴?????d?濡???浣?杈??ョ????涓?涓???褰???锛???涓?璇ョ??褰?涓?娌℃??涓?涓????ㄧ??welcome??浠訛??d?tomcat浼?灏?璇ョ??褰?涓?????????浠跺???烘?ワ?濡???浣??沖??借?涓?缂虹??琛?涓猴??d???浠ヤ慨??onf/web.xml??浠訛?灏??朵腑??锛?
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
淇??逛負锛?
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
?