<%@ 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("zcwy");
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 wl_user";
//titles为SQLSERVER2000的默认数据库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>
--------------------------
运行报错如下
javax.servlet.ServletException: Name jdbc is not bound in this Context
-------------===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>
<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></web-app>
<%@ 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("zcwy");
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 wl_user";
//titles为SQLSERVER2000的默认数据库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>
--------------------------
运行报错如下
javax.servlet.ServletException: Name jdbc is not bound in this Context
-------------===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>
<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></web-app>
解决方案 »
- java 连接mysql
- jsp实时刷新问题请教!(急 在线等!!)
- session 共享问题,紧急,紧急,紧急,高手请进!!!!!!
- 面试题求解,大家都来看看啊
- 怎样才能使dreamwear识别struts标签?
- 如何获取file中文件名的扩展名?
- 这个问题弄得我一头雾水!
- web应用连数据库异常the network adapter could not establish the connection
- jsp+sql 非常简单的连接的问题 (救命。。。。)
- 如何获取客户端的IP地址?打虾帮我!!!!!
- 高分求救!如何弹出一个不被IE插件屏蔽的全屏窗口
- TOMCAT问题,昨天搞了一天没有弄出来,请高手帮忙!
Class.forName(驱动);
如果是tomcat,在tomcat的配置文件中还要配置。具体的到google搜索。我记得JR上有一篇专门讲DataSoure配置的文章。www.javaresearch.org
<GlobalNamingResources>
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
<Resource name="JDBC for MySQL" type="javax.sql.DataSource"/>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
<ResourceParams name="JDBC for MySQL">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>sa</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=zcwy</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
</ResourceParams>
</GlobalNamingResources>
try
{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
}
catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String url="jdbc:oracle:thin:@localhost:1521:DBTEST";
String user="jlrc";
String password="jlrc";
Connection conn = null;
try
{
conn = DriverManager.getConnection(url, user, password);
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
<ResourceParams name="jdbc/bn">
jsp中:
Context initCtx=new javax.naming.InitialContext();
Context envCtx=(Context)initCtx.lookup("java:comp/env");
DataSource ds=(DataSource)envCtx.lookup("jdbc/bn");