读书人

继续问:sql2005数据库连接池有关问题

发布时间: 2011-12-28 22:45:21 作者: rapoo

继续问:sql2005数据库连接池问题
我的数据库连接池已经做好了配置:
所以该配置的xml文件如下:
MyWeb.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
  
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
maxActive="4"
maxIdle="2"
username="sa"
maxWait="5000"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
password="123456"
url="jdbc:microsoft:sqlserver://localhost/EdocServer_db"/>
  
</Context>

server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Server>
<Listener className="org.apache.catalina.core.AprLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
name="jdbc/mysql"
type="javax.sql.DataSource"
maxActive="4"
maxIdle="2"
username="sa"
maxWait="5000"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
password="123456"
url="jdbc:microsoft:sqlserver://localhost/EdocServer_db"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
</GlobalNamingResources>
<Service
name="Catalina">
<Connector
port="8080"
redirectPort="8443"
minSpareThreads="25"
connectionTimeout="20000"
maxSpareThreads="75"
maxThreads="150">
</Connector>
<Connector
port="8009"
redirectPort="8443"
protocol="AJP/1.3">
</Connector>
<Engine
defaultHost="localhost"
name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host
appBase="webapps"
name="localhost">


</Host>
</Engine>
</Service>
</Server>


web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>MyWeb </display-name>
<servlet>
<servlet-name>Logon </servlet-name>
<servlet-class>userinfo.Logon </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Logon </servlet-name>
<url-pattern>/userinfo/Logon </url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html </welcome-file>
<welcome-file>index.htm </welcome-file>
<welcome-file>index.jsp </welcome-file>
<welcome-file>default.html </welcome-file>
<welcome-file>default.htm </welcome-file>
<welcome-file>default.jsp </welcome-file>
</welcome-file-list>
<resource-ref>
<res-ref-name>jdbc/mysql </res-ref-name>
<res-type>javax.sql.DataSource </res-type>
<res-auth>Container </res-auth>
</resource-ref>
</web-app>
web.xml在eclipse里会有一个黄色警告错误:但我不知道哪里有错???

整个数据库操作的代码是这样的:
package userinfo;
import java.sql.*;

import javax.naming.*;
import javax.sql.*;



/**
* 数据库相关的操作
*/
public class Conn {


public static synchronized Connection getDSConnection( String user, String password ) throws SQLException
{
try
{
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup( "java:comp/env" );
DataSource ds = (DataSource)envCtx.lookup( "jdbc/mysql" );
return ds.getConnection( user, password );
}
catch(NamingException ex )
{
ex.printStackTrace( System.err );
return null;
}
}


public static synchronized Connection getConnection( ) throws SQLException
{
try
{ DataSource ds = null;
InitialContext ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql"); //每次单步执行到这里就ds的数据中的url的确=null;
Connection conn = ds.getConnection();//到这里就不能继续下去了 ,就抛出了一个异常:Cannot create JDBC driver of class '' for connect URL 'null'
return conn;
}
catch( NamingException ex )
{
ex.printStackTrace( System.err );
return null;
}
}



public static Connection getConnection( String user, String password ) throws SQLException
{

String drivername = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
// // 定义连接字符串
String CONNSTR = "jdbc:microsoft:sqlserver://localhost:1433:" + user + "sa" + password + "123456";
try
{
Class.forName(drivername);

}
catch( Exception ex )
{
ex.printStackTrace( System.err );
}
return DriverManager.getConnection(CONNSTR);
}

// 取得某个表中可用的主键值
public static synchronized int getMaxID( Connection conn, String username )
throws SQLException
{
int nMaxID = 0;


Statement st = conn.createStatement();

// 增加同步,避免同时由多个进程执行
String sql = "SELECT userid FROM userinfo WHERE username = '" + username + "'";
ResultSet rs = st.executeQuery( sql );
while( rs.next() )
{
nMaxID = rs.getInt( 1 );
}
rs.close();
if( nMaxID > 0 )
{
// 存在该表的记录
nMaxID++;
sql = "UPDATE userinfo SET userid=" + nMaxID + " WHERE username = '"
+ username + "'";
}
else
{
// 不存在该表的记录
nMaxID = 1;// 从1开始
sql = "INSERT INTO userinfo ( username,userid ) "
+ "VALUES ( '" + username + "'," + nMaxID + ")";
}
st.executeUpdate( sql );
st.close();

return nMaxID;
}

// 关闭连接,释放资源
public static void close( ResultSet rs, Statement st, Connection conn )
{
try
{
if( rs!=null ) rs.close();
}
catch( SQLException ex ) { };

try
{
if( st!=null ) st.close();
}
catch( SQLException ex ) { };

try
{
if( conn!=null ) conn.close();
}
catch( SQLException ex ) { };
}

}

到底是怎么回事
请大家帮帮忙,真的很郁闷~




[解决办法]
URL也不对url="jdbc:microsoft:sqlserver://localhost/EdocServer_db"

url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=EdocServer_db"/>
[解决办法]
server.xml 里面
<Environment
name="simpleValue"

??
我这里是
 <Resource
name="jdbc/mysql"

不太一样哦?你的怎么是Environment ??
[解决办法]
tomcat版本是5.5以上吗?

读书人网 >Java Web开发

热点推荐