菜鸟的提问( 亿万火急)

jimmy7887 2003-05-06 12:29:44
怎样在tomcat中 用一个数据库连接池呢????
最好能有一个例子。
高分相送。
万分感激!!
...全文
24 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dreamingmouse 2003-05-06
  • 打赏
  • 举报
回复
对不起 错了 tomcat 连接池配置需要在server.xml里面修改设置
dreamingmouse 2003-05-06
  • 打赏
  • 举报
回复
package mysqltest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.gjt.mm.mysql.Driver;
import java.io.File;
import java.io.FileInputStream;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class test {
/**
* JDBC URL, initialized from com.mysql.jdbc.testsuite.url
* system property, or defaults to jdbc:mysql:///test
*/

public test() {
}
public static void main(String[] args) {
Connection conn = null;

try
{
File file = new File("c://dd.bmp");
FileInputStream fis = new FileInputStream(file);
Object sd = (Object)fis;
int lenth = 0;
lenth= (int)file.length();
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://192.168.238.35/mail?useUnicode=true&characterEncoding=gb2312";
String str = "小dd";
conn= DriverManager.getConnection(url,"root","");
PreparedStatement ps = conn.prepareStatement("INSERT INTO test VALUES (?)");
ps.setString(1,new String(str.getBytes("gb2312")));
// ps.setObject(2,sd);
FileInputStream dsd = (FileInputStream)sd;
//Statement stat=conn.createStatement();
// ResultSet rs=stat.executeQuery("select * from custom_folder");
// System.out.println(rs.next()) ;
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}

}
}
dreamingmouse 2003-05-06
  • 打赏
  • 举报
回复
你要看着修改 其实就是修改和增加这几个节点
jimmy7887 2003-05-06
  • 打赏
  • 举报
回复
就是把你上面那些写进server.xml吗?
dreamingmouse 2003-05-06
  • 打赏
  • 举报
回复
贴出部分server.xml内容 但已涵盖修改部分 我用的是mysql数据库 顺便给你测试代码 如下
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import= "java.sql.* "%>
<%@ page import= "javax.sql.DataSource"%>
<%@ page import= "javax.naming.*"%>
<html>
<head>
<title>DB Test</title>
</head>
<body>

<%
String foo = "Not Connected";
int bar = -1;

try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");

DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/jdbc/TestDB");
out.println("get here!!");
if (ds != null) {
Connection conn = ds.getConnection();
out.println("get here!!!!");
if(conn != null) {
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery(
"select id, foo, bar from testdata");
out.println("get here!!!!!");
//out.println(rst.next());
if(rst.next()) {
out.println("get here haha");
foo=rst.getString(2);
bar=rst.getInt(3);
out.println("foo is "+foo);
out.println("bar is "+bar);
}
conn.close();
}
}
}catch(Exception e) {
e.printStackTrace();
}
%>

<h2>Results</h2>


</body>
</html>

dreamingmouse 2003-05-06
  • 打赏
  • 举报
回复
<!-- Tomcat Root Context -->
<Context path="" docBase="ROOT" debug="0">
<Resource name="jdbc/Mysql" auth="SERVLET" type="javax.sql.DataSource"/>
<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 0 for no limit.
-->
<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></value>
</parameter>

<!-- Class name for mm.mysql JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.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://192.168.238.35:3306/javatest</value>
</parameter>
</ResourceParams>
<ResourceParams name="jdbc/Mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://192.168.238.35/mail</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>

<parameter>

<name>password</name>

<value></value>

</parameter>

<parameter>

<name>maxActive</name>

<value>20</value>

</parameter>

<parameter>

<name>maxIdle</name>

<value>10</value>

</parameter>

<parameter>

<name>maxWait</name>

<value>-1</value>

</parameter>
</ResourceParams>
</Context>
</Host>

</Engine>

</Service>

<!-- The MOD_WEBAPP connector is used to connect Apache 1.3 with Tomcat 4.0
as its servlet container. Please read the README.txt file coming with
the WebApp Module distribution on how to build it.
(Or check out the "jakarta-tomcat-connectors/webapp" CVS repository)

To configure the Apache side, you must ensure that you have the
"ServerName" and "Port" directives defined in "httpd.conf". Then,
lines like these to the bottom of your "httpd.conf" file:

LoadModule webapp_module libexec/mod_webapp.so
WebAppConnection warpConnection warp localhost:8008
WebAppDeploy examples warpConnection /examples/

The next time you restart Apache (after restarting Tomcat, if needed)
the connection will be established, and all applications you make
visible via "WebAppDeploy" directives can be accessed through Apache.
-->
jimmy7887 2003-05-06
  • 打赏
  • 举报
回复
to dreamingmouse(满地毛毛)
在 server.xml里面怎样修改设置?
lynx1111 2003-05-06
  • 打赏
  • 举报
回复
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Name";
String user="sa";
String password="。。。。";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery ("SELECT * FROM aa");
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
你必须下在sql server 2000的驱动程序,并经兴建单配置!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧