配置Tomcat7 Sqlserver连接池。
在项目Web-inf下面web.xml加了如下代码<resource-ref><description>Sqlserver2005</description><res-r...
在项目Web-inf下面web.xml加了如下代码
<resource-ref>
<description>Sqlserver2005</description>
<res-ref-name>jdbc/sqlserver2005</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
然后再conf的context.xml里加了
<Resource name="jdbc/sqlserver2005"
auth="Container"
type="javax.sql.DataSource"
password="sa"
username="sa"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=student"
maxActive="100" maxIdle="30" maxWait="5000"/>
<!-- Default set of monitored resources -->
数据库名是student,表名是Student,
测试代码是:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
<head>
</head>
<body>
<%
out.print("hello");
DataSource ds = null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/student");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = "select * from Student";//自行修改这一条语句。
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(1));
out.print(rs.getString(3));
}
}catch(Exception ex){
ex.printStackTrace();
}
%>
</body>
</html>
错误提示是javax.naming.NameNotFoundException: Name [student] is not bound in this Context. Unable to find [student].怎么改。 展开
<resource-ref>
<description>Sqlserver2005</description>
<res-ref-name>jdbc/sqlserver2005</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
然后再conf的context.xml里加了
<Resource name="jdbc/sqlserver2005"
auth="Container"
type="javax.sql.DataSource"
password="sa"
username="sa"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=student"
maxActive="100" maxIdle="30" maxWait="5000"/>
<!-- Default set of monitored resources -->
数据库名是student,表名是Student,
测试代码是:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<html>
<head>
</head>
<body>
<%
out.print("hello");
DataSource ds = null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/student");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = "select * from Student";//自行修改这一条语句。
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(1));
out.print(rs.getString(3));
}
}catch(Exception ex){
ex.printStackTrace();
}
%>
</body>
</html>
错误提示是javax.naming.NameNotFoundException: Name [student] is not bound in this Context. Unable to find [student].怎么改。 展开
1个回答
展开全部
请参考
首先配置:META-INF/context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/db_name" />
</Context>
再配置:WEB-INF/web.xml:在web-app元素下
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
程序:
InitialContext ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
stmt.execute(“执行的SQL语句”);
stmt.close();
conn.close();
首先配置:META-INF/context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="" reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root" password="root"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/db_name" />
</Context>
再配置:WEB-INF/web.xml:在web-app元素下
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
程序:
InitialContext ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
stmt.execute(“执行的SQL语句”);
stmt.close();
conn.close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询