当前位置: 代码迷 >> Java Web开发 >> TOMCAT配置有关问题!数据库连接,老出错
  详细解决方案

TOMCAT配置有关问题!数据库连接,老出错

热度:177   发布时间:2016-04-17 12:40:43.0
TOMCAT配置问题!数据库连接,老出错!
<Context docBase="" path="" reloadable="true" source="com.ibm.wtp.web.server:tomcat">
  <Logger className="org.apache.catalina.logger.FileLogger"
  prefix="tomcat_log." suffix=".txt"
  timestamp="true"/>
 
  <Resource name="jdbc/SqlServer"
  auth="Container"
  type="javax.sql.DataSource"
  reloadable="true"/>
   
  <ResourceParams name="jdbc/SqlServer">
  <parameter>
  <name>factory</name>
  <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
  </parameter>
   
 
  <parameter>
  <name>maxActive</name>
  <value>100</value>
  </parameter>
   
 
  <parameter>
  <name>maxIdle</name>
  <value>30</value>
  </parameter>
   
 
  <parameter>
  <name>maxWait</name>
  <value>10000</value>
  </parameter>
   
   
  <parameter>
  <name>username</name>
  <value>sa</value>
  </parameter>
  <parameter>
  <name>password</name>
  <value>123</value>
  </parameter>
   
   
  <parameter>
  <name>driverClassName</name>
  <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
  </parameter>
   
 
  <parameter>
  <name>url</name>
  <value>jdbc:microsoft:sqlserver://127.0.0.1:1433/BakBlog</value>
  </parameter>
  </ResourceParams>
  </Context>

以上是配置数据连接的Context

程序中:
<%@ page contentType = "text/html;charset=gb2312" %> 
<%@ page import = "java.sql.*" %> 
<%@ page import = "javax.sql.*" %> 
<%@ page import = "javax.naming.*" %> 

< html > 
< body > 
<% 
  DataSource ds = null ; 
  Connection conn = null ; 
  ResultSet rs = null ; 
  java.sql.Statement stmt = null ; 
  InitialContext ctx = null ; 
 
  try 
  { 
  ctx =new InitialContext (); 
  ds =( DataSource ) ctx . lookup ( "jdbc/SqlServer" );  
  conn = ds . getConnection (); 
  stmt = conn . createStatement (); 

  String strSql = " select * from admin" ; 表  
  rs = stmt . executeQuery ( strSql ); 
  while( rs . next ()) 
  { 
  out . println ( rs . getString ( 1 ) + "" );  
  out . println ( rs . getString ( 2 ) + "" ); 
  } 
  相关解决方案