myeclipse2014 mysql连接池_MyEclipse jdbc数据连接池的配置

本文介绍了如何在MyEclipse 2014中配置MySQL连接池,包括创建Web工程、导入JDBC驱动、建立context.xml文件、修改web.xml文件以及通过JSP和Servlet测试数据源的步骤。
摘要由CSDN通过智能技术生成

软件版本myeclispe8.0,自带tomcat6.0.13。

jdbc:mysql-connector-java-5.1.13-bin.jar

第一步:建立工程。

在Myeclipse中file->new->web project。

因为在测试数据源(jsp)时用到了标签库,所以可以在这里选上jsdl支持,当然也可以在工程建好后右键工程文件夹->myeclipse->add JSTL

Libraries…实现同样的功能。

第二步:导入jdbc的jar包。

要直接将mysql-connector-java-3.1.7-bin.jar复制到“工程文件夹/WebRoot/WEB-INF/lib”文件夹下。这时,myeclipse会自动生成一个

Referenced Libraries,不用管。

第三步:建立context.xml文件。

在“工程文件夹/WebRoot/META-INF”下,新建context.xml文件。文件内容如下:

name="jdbc/mysql"

auth="Container"

type="javax.sql.DataSource"

maxActive="100"

maxIdle="30"

maxWait="10000"

username="root"

password=""

driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" />

解释:

name="jdbc/mysql"   //连接名,jndi中使用。具在JSP中用调用,servlet用 DataSource ds

= (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");调用。这里是tomcat的格式,不同的服务器可能有所不同。

auth="Container"

type="javax.sql.DataSource"

maxActive="100"

maxIdle="30"

maxWait="10000"

username="root"    //mysql的用户名

password=""        //mysql的用户密码,我这里是空

driverClassName="com.mysql.jdbc.Driver"   //驱动类名,一般确定

url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true" //javatest是mysql中要使用的数据库名

第四步:修改web.xml文件。

在web.xml文件中添加以下内容:                  //重要一定添加进去

DB Connection

jdbc/mysql

javax.sql.DataSource

Container

这部分内容一般是确定的。

OK,现在可以测试数据源是否好了。下面是JSP测试文件:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'MyJsp.jsp' starting page

select id, username, password from user

Results

Foo ${row.username}

Bar ${row.password}

当然,如果你用servlet测试数据源也是可以的,下面是一个servlet测试例子:

package fx;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

public class DsTest extends HttpServlet

{

/**

* Constructor of the object.

*/

public DsTest()

{

super();

}

/**

* Destruction of the servlet.

*/

public void destroy()

{

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

doPost(request,response);

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out

.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print("    This is ");

out.print(this.getClass());

out.println(", using the POST method");

try

{

Context ctx = new InitialContext();

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");

Connection conn = ds.getConnection();

ResultSet rs=conn.createStatement().executeQuery("select * from user");

rs.next();

out.print(rs.getString(2));

} catch (NamingException e) {

e.printStackTrace(out);

System.out.println(e.getMessage());

} catch (SQLException e) {

e.printStackTrace(out);

}

out.println("connection pool connected !!haha");

out.println(" ");

out.println("");

out.flush();

out.close();

}

/**

* Initialization of the servlet.

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException

{

// Put your code here

}

}

我的mysql数据库名为javatest,表名为user,有三列"id","username","password"。servlet运行结果将打印出user表中的第一个用户名。

注意:如果你新建servlet,myeclipse会自动帮你在web.xml中生成相应的mapping,而这部分内容可能“插”进

DB Connection

jdbc/mysql

javax.sql.DataSource

Container

中,造成错误。注意自己手动调整。      //要看一下,因为我的好像没有这段代码

别外,注意myeclipse中的servlet映射为“服务器ip:端口/工程文件名/servlet/servlet名”。

还有就是最好使用外部自己配置的Tomcat,并将mysql-connector-java-5.1.13-bin.jar包放到Tomcat的lib目录下。

最好不要用MyEclipse自带的Tomcat,因为我的MyEclipse提示org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

即Tomcat出现异常,找不到jdbc驱动包

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值