配置:Tomcat5.5+JEE(jsdk1.5)+WINXP
还是简单的说一说文件配置:
1:修改%tomcat%/conf/server.xml在后加如下内容.
name="jdbc/DBPool" //数据源名称
type="javax.sql.DataSource"
password="xxxxxxxx"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://127.0.0.1:3306/hptest"
maxActive="4"/>
2.修改%tomcat%/conf/context.xm;在后加
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"/>
3.修改%tomcat%/conf/web.xml
MySQL DB Connection Pool
jdbc/DBPool
javax.sql.DataSource
Container
Shareable
这样配置就算差不多了.如果具体的还不懂可见上次发的文章.
4.写一个程序测试.(写一个WEB程序)
我的是Myeclipse 写的程序,这里不能从电脑上贴图真有点不方便(我想哭).
1:写一个连接类:
DBPool.java
package com.test;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBPool {
private static DataSource pool;
static {
Context env = null;
try {
env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource)env.lookup("jdbc/DBPool");
if(pool==null)
System.err.println("'DBPool' is an unknown DataSource");
} catch(NamingException ne) {
ne.printStackTrace();
}
}
public static DataSource getPool() {
return pool;
}
}
2:写一个Servlet:
其中有是用来连接数据库和显示查询结果.
Mytest.java
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Mytest extends HttpServlet {
/**
* Constructor of the object.
*/
public Mytest() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String id=(String)request.getParameter("id");
Connection con=null;
try{
con=DBPool.getPool().getConnection();
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from userinf where userid='"+id+"'");
if(rst.next()){
out.println("
ID号:"+rst.getInt("userid"));
out.println("
用户名:"+com.test.ASSICTOGBR2312.trans(rst.getString("name")));
out.println("
地址:"+rst.getString("address" ));
out.println("
生日"+rst.getDate("year" ));
}
else{
out.println("没有这个ID");
stmt.close();
}
}
catch(Exception e){
e.printStackTrace();
}
finally{ //一定要注意这里对数据库的关闭不然
//自己想了
try{
if(con!=null){
con.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
3:写一个JSP页面:
Myjsp.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'MyJsp.jsp' starting page
<!--
-->
This is my JSP page.
print input search id:
4:其中有转字,为了不显示乱码
package com.test;
import java.io.*;
public class ASSICTOGBR2312 {
public static String trans(String ass){
String res=null;
byte temp[];
try{
temp=ass.getBytes("iso-8859-1");
res=new String(temp);
}
catch(UnsupportedEncodingException en) {
System.out.println(en.toString());
}
return res;
}
}
5:web.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
This is the description of my J2EE component
This is the display name of my J2EE component
Mytest
com.test.Mytest
Mytest
/servlet/Mytest
6: 其中数据库结构如下:
数据库名:hptest
表:userinf
用下面的命令建一个数据库和表
create database hptest;
create table userinf (
userid int(4) not null,
name char(10) not null,
address varchar(50),
year date,
constraint fk_userinf primary key(userid));
)
insert into userinf values(19,'hp','cq','1982-10-22');
[@more@]
还是简单的说一说文件配置:
1:修改%tomcat%/conf/server.xml在后加如下内容.
name="jdbc/DBPool" //数据源名称
type="javax.sql.DataSource"
password="xxxxxxxx"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://127.0.0.1:3306/hptest"
maxActive="4"/>
2.修改%tomcat%/conf/context.xm;在后加
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"/>
3.修改%tomcat%/conf/web.xml
MySQL DB Connection Pool
jdbc/DBPool
javax.sql.DataSource
Container
Shareable
这样配置就算差不多了.如果具体的还不懂可见上次发的文章.
4.写一个程序测试.(写一个WEB程序)
我的是Myeclipse 写的程序,这里不能从电脑上贴图真有点不方便(我想哭).
1:写一个连接类:
DBPool.java
package com.test;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBPool {
private static DataSource pool;
static {
Context env = null;
try {
env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource)env.lookup("jdbc/DBPool");
if(pool==null)
System.err.println("'DBPool' is an unknown DataSource");
} catch(NamingException ne) {
ne.printStackTrace();
}
}
public static DataSource getPool() {
return pool;
}
}
2:写一个Servlet:
其中有是用来连接数据库和显示查询结果.
Mytest.java
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Mytest extends HttpServlet {
/**
* Constructor of the object.
*/
public Mytest() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
String id=(String)request.getParameter("id");
Connection con=null;
try{
con=DBPool.getPool().getConnection();
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from userinf where userid='"+id+"'");
if(rst.next()){
out.println("
ID号:"+rst.getInt("userid"));
out.println("
用户名:"+com.test.ASSICTOGBR2312.trans(rst.getString("name")));
out.println("
地址:"+rst.getString("address" ));
out.println("
生日"+rst.getDate("year" ));
}
else{
out.println("没有这个ID");
stmt.close();
}
}
catch(Exception e){
e.printStackTrace();
}
finally{ //一定要注意这里对数据库的关闭不然
//自己想了
try{
if(con!=null){
con.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
3:写一个JSP页面:
Myjsp.jsp
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'MyJsp.jsp' starting page
<!--
-->
This is my JSP page.
print input search id:
4:其中有转字,为了不显示乱码
package com.test;
import java.io.*;
public class ASSICTOGBR2312 {
public static String trans(String ass){
String res=null;
byte temp[];
try{
temp=ass.getBytes("iso-8859-1");
res=new String(temp);
}
catch(UnsupportedEncodingException en) {
System.out.println(en.toString());
}
return res;
}
}
5:web.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
This is the description of my J2EE component
This is the display name of my J2EE component
Mytest
com.test.Mytest
Mytest
/servlet/Mytest
6: 其中数据库结构如下:
数据库名:hptest
表:userinf
用下面的命令建一个数据库和表
create database hptest;
create table userinf (
userid int(4) not null,
name char(10) not null,
address varchar(50),
year date,
constraint fk_userinf primary key(userid));
)
insert into userinf values(19,'hp','cq','1982-10-22');
[@more@]
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9650775/viewspace-920824/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9650775/viewspace-920824/