一,在context.xml中的</Context>的前面添加
<Resource name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=bukao;user=sa;password=sa"
maxActive="10"
maxIdle="3"
maxWait="10000" />
二,在web.xml的</web-app>前面中添加
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
三,测试代码为:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
try{
Context initCtx = new InitialContext();
DataSource ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/test");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("select stu_name from student where stu_yuan='电子信息工程学院'");
while (rst.next()){
byte[] temp_t=rst.getString("stu_name").getBytes("ISO8859-1");
String temp=new String(temp_t);
out.println(temp);
}
conn.close();
} catch (Exception e){
e.printStackTrace();
}
%>