在项目Web-inf下面web.xml加了如下代码Sqlserver2005jdbc/sqlserver2005javax.sql.DataSource
在项目Web-inf下面web.xml加了如下代码
Sqlserver2005
jdbc/sqlserver2005
javax.sql.DataSource
Container
然后再conf的context.xml里加了
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"/>
数据库名是student,表名是Student,
测试代码是:
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();
}
%>
错误提示是javax.naming.NameNotFoundException: Name [student] is not bound in this Context. Unable to find [student].怎么改。
展开