MyEclipse 中 Tomcat 配置数据源和连接池的问题~

来源:互联网 发布:游戏原画美工外包 编辑:程序博客网 时间:2024/05/01 08:53

一,在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();
    }
%>

原创粉丝点击