JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » 違規輔導區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
話題被移動
該話題已被移動 - browser , 2005-12-15 15:40
如果您尚不清楚該話題被移動的原因,請參考論壇規則以及本版公告或者聯系本版版主。
本主題所含的標籤
無標籤
作者 Re:请教连接池出错 [Re:yinbodotcc]
yinbodotcc





發文: 7
積分: 0
於 2005-12-15 12:03 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这个是我写的测试程序,由于用连接池没有成功,于是就改了一下,注释部分用的是连接池

String userName=((String)(request.getParameter("username"))).trim();
String passWord=((String)(request.getParameter("password"))).trim();

//Context initContext=null;
//Context envContext=null;
DataSource ds=null;
Connection con=null;
Statement st=null;
ResultSet rs=null;
try
{
  //说明:我曾经花费了一整天去用屏蔽处的连接池,但是实践没有成功,我机会
  //用尽了我所有的方法,所以后来只好改面目,让大家失望了!!!!!!!!!
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  con=DriverManager.getConnection("jdbc:mysql://localhost:3306/yay?user=root&password=qwe123");
  st=con.createStatement();
  //initContext=new InitialContext();
  //envContext=(Context)initContext.lookup("jdbc/TestDB");
  //ds=(DataSource)envContext.lookup("jdbc/TestDB");
  //con=ds.getConnection();
  //st=con.createStatement();
  String query="select * from person where id='"+userName+"' and password='"+passWord+"'";
  rs=st.executeQuery(query);

  if(rs.next())
  {
    out.println("您是合法用户");
  }
  else
    out.println("非法用户");
  
}
catch(Exception e)
{
out.printEnvelope;
}
finally
{
  
  
  if(rs!=null)rs=null;
  if(st!=null)st=null;
if(con!=null)con=null;
  if(ds!=null)ds=null;
}

下面是web.xml部分
<web-app 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"
version="2.4">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

</web-app>

接下来是server.xml中加入的部分
<Context path="/yayBBS" docBase="yayBBS" debug="0" crosscontext="true" reloadable="true">


<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>

<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"/>

<ResourceParams name="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<!-- Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>

<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>qwe123</value>
</parameter>

<!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next
if you want to use this driver - we recommend using Connector/J though
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
-->

<!-- Class name for the official MySQL Connector/J driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>

<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/yay?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>

我这个东西搞了好长时间了,还是没有搞定,麻烦各位帮我解决一下,不胜感激呀


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
712 请教连接池出错 yinbodotcc 779 2005-12-14 11:34
508 Re:请教连接池出错 browser 58 2005-12-14 22:29
645 Re:请教连接池出错 yinbodotcc 4469 2005-12-15 12:03
» JWorld@TW »  違規輔導區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8