本文主要向大家讲述的是实现tomcat MySQL数据源的实际操作流程与在实际操作中我们要用到的代码的描述,以下就是文章的主要内容。
以下的文章主要向大家描述的是tomcat MySQL数据源的实际操作流程以及在其实际操作中所要用到的代码的描述,假如你对实现tomcat MySQL数据源的实际操作感兴趣的话,以下的文章将会满足你这一兴趣。
1.拷相应的driver.jar到Tomcat5\common\lib下
2.更改Tomcat5\conf下的context.xml
<Context>节点下加
- <Resourcename="jdbc/MysqlConnectionPoolTest"auth="Czh"
- type="javax.sql.DataSource"driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://127.0.0.1:3306/test"
- username="root"password="000000"maxActive="20"maxIdle="10"
- maxWait="-1"/>
3.更改工程下的web.xml
<web-app>节点下加
- <resource-ref>
- <description>DBConnection</description>
- <res-ref-name>jdbc/MysqlConnectionPoolTest</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Czh</res-auth>
- </resource-ref>
4.tomcat MySQL数据源代码如下
- Contextcontext=null;
- Connectionconn=null;
- Statementstmt=null;
- ResultSetrs=null;
- publicvoidDoQuery(Stringsql){
- try{
- if(context==null)
- {
- context=newInitialContext();
- }
- //getds
- DataSourceds=(DataSource)context
- .lookup("java:comp/env/jdbc/MysqlConnectionPoolTest");
- //getconn
- if(conn==null){
- conn=ds.getConnection();
- }
- if(stmt==null){
- stmt=conn.createStatement();
- }
- rs=stmt.executeQuery(sql);
- while(rs.next()){
- Stringa=rs.getString("a");
- Stringb=rs.getString("b");
- }
- }catch(Exceptione){
- e.printStackTrace();
- }
- }
注意有comp/env/
- context
- .lookup("java:comp/env/jdbc/MysqlConnectionPoolTest");
以上的相关内容就是对tomcat MySQL数据源的介绍,望你能有所收获。
【编辑推荐】
- Linux MySQL就用fedora的自动安装
- Ubuntu mysql配置设置过程
- CentOS系统5编译安装mysql-4.1.22
- CentOS系统操作mysql的常用命令
- Ubuntu MySQL设置同步服务器