1. 前提
导入jdbc驱动包,这是个大前提,很重要的。
2.配置context.xml以及web.xml
context.xml:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="******"--------------------------------------------------------------------------名字任意,但是和下面的一样,,通常为jdbc/***
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/数据库名字"
username="数据库用户名"
password="数据库密码"
maxActive="100"
maxIdle="30"
maxWait="10000" />
</Context>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>test</display-name> 可要可不要
<welcome-file-list>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DB Connection</description> 可要可不要
<res-ref-name>******</res-ref-name>--------------------------------------------------------------名字任意,但是和下面的一样,,通常为jdbc/***
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
3. 重启tomcat服务器
4. 配置contxet.xml和web.xml一定要在eclipse中配置--server中的contxet.xml和web.xml,不能在tomcat中的contxet.xml和web.xml配置,若是在tomcat中配置的话,
每次启用tomcat的时候,会覆盖tomcat中的配置文件的。
5.测试代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<%
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String sql=null;
String username=null;
String alias=null;
String pwd=null;
String strMsg=null;
try{
Context cxt = new InitialContext();
DataSource ds = (DataSource)cxt.lookup("java:/comp/env/数据库名字");
conn = ds.getConnection();
sql="select * from 数据表";
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
if(rs.next()){
strMsg="连接ok";
out.println(rs.getString("数据表中的属性:id等"));
out.println(rs.getString("数据表中的属性:name等"));
out.println("连接ok");
}else{
out.println("rs.next() fail");
}
rs.close();
} catch(Exception e){
out.println("连接失败:"+e.getMessage());
//e.printStackTrace();
}
try{
if(stmt!=null) stmt.close();
}catch(Exception e){}
try{
if(conn!=null) conn.close();
}catch(Exception e){}
%>
</body>
</html>
导入jdbc驱动包,这是个大前提,很重要的。
2.配置context.xml以及web.xml
context.xml:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="******"--------------------------------------------------------------------------名字任意,但是和下面的一样,,通常为jdbc/***
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/数据库名字"
username="数据库用户名"
password="数据库密码"
maxActive="100"
maxIdle="30"
maxWait="10000" />
</Context>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>test</display-name> 可要可不要
<welcome-file-list>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>DB Connection</description> 可要可不要
<res-ref-name>******</res-ref-name>--------------------------------------------------------------名字任意,但是和下面的一样,,通常为jdbc/***
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
3. 重启tomcat服务器
4. 配置contxet.xml和web.xml一定要在eclipse中配置--server中的contxet.xml和web.xml,不能在tomcat中的contxet.xml和web.xml配置,若是在tomcat中配置的话,
每次启用tomcat的时候,会覆盖tomcat中的配置文件的。
5.测试代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<%
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
String sql=null;
String username=null;
String alias=null;
String pwd=null;
String strMsg=null;
try{
Context cxt = new InitialContext();
DataSource ds = (DataSource)cxt.lookup("java:/comp/env/数据库名字");
conn = ds.getConnection();
sql="select * from 数据表";
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
if(rs.next()){
strMsg="连接ok";
out.println(rs.getString("数据表中的属性:id等"));
out.println(rs.getString("数据表中的属性:name等"));
out.println("连接ok");
}else{
out.println("rs.next() fail");
}
rs.close();
} catch(Exception e){
out.println("连接失败:"+e.getMessage());
//e.printStackTrace();
}
try{
if(stmt!=null) stmt.close();
}catch(Exception e){}
try{
if(conn!=null) conn.close();
}catch(Exception e){}
%>
</body>
</html>