tomcat MySQL数据源的实际操作流程与代码

数据库 MySQL
本文主要向大家讲述的是实现tomcat MySQL数据源的实际操作流程与在实际操作中我们要用到的代码的描述,以下就是文章的主要内容。

以下的文章主要向大家描述的是tomcat MySQL数据源的实际操作流程以及在其实际操作中所要用到的代码的描述,假如你对实现tomcat MySQL数据源的实际操作感兴趣的话,以下的文章将会满足你这一兴趣。

1.拷相应的driver.jar到Tomcat5\common\lib下

2.更改Tomcat5\conf下的context.xml

<Context>节点下加

  1. <Resource name="jdbc/MysqlConnectionPoolTest" auth="Czh" 
  2. type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver" 
  3. url="jdbc:mysql://127.0.0.1:3306/test" 
  4. username="root" password="000000" maxActive="20" maxIdle="10" 
  5. maxWait="-1"/>  

3.更改工程下的web.xml

<web-app>节点下加

  1. <resource-ref> 
  2. <description>DB Connection</description> 
  3. <res-ref-name>jdbc/MysqlConnectionPoolTest</res-ref-name> 
  4. <res-type>javax.sql.DataSource</res-type> 
  5. <res-auth>Czh</res-auth> 
  6. </resource-ref> 

4.tomcat MySQL数据源代码如下

  1. Context context = null;  
  2. Connection conn = null;  
  3. Statement stmt = null;  
  4. ResultSet rs = null;  
  5. public void DoQuery(String sql) {  
  6. try {  
  7. if(context==null)  
  8. {  
  9. context = new InitialContext();  
  10. }  
  11. // get ds  
  12. DataSource ds = (DataSource) context  
  13. .lookup("java:comp/env/jdbc/MysqlConnectionPoolTest");  
  14. // get conn  
  15. if(conn==null){  
  16. conn = ds.getConnection();  
  17. }  
  18. if(stmt==null){  
  19. stmt = conn.createStatement();  
  20. }  
  21. rs = stmt.executeQuery(sql);  
  22. while (rs.next()) {  
  23. String a = rs.getString("a");  
  24. String b = rs.getString("b");  
  25. }  
  26. } catch (Exception e) {  
  27. e.printStackTrace();  
  28. }  
  29. }  

注意有comp/env/

  1. context  
  2. .lookup("java:comp/env/jdbc/MysqlConnectionPoolTest");  

以上的相关内容就是对tomcat MySQL数据源的介绍,望你能有所收获。

【编辑推荐】

  1. Linux mysql就用fedora的自动安装
  2. Ubuntu mysql配置设置过程
  3. CentOS系统5编译安装mysql-4.1.22
  4. CentOS系统操作mysql的常用命令
  5. Ubuntu MySQL设置同步服务器
     
责任编辑:佚名 来源: cnblogs
相关推荐

2010-06-12 16:54:19

2010-06-07 17:30:16

MySQL配置SSL

2010-06-04 14:18:10

MySQL 分页存储过

2010-05-28 17:32:11

MySQL命令导数据

2010-05-27 17:35:36

MYSQL DELET

2010-05-19 10:37:06

MySQL expla

2010-05-20 17:56:43

2010-06-07 15:47:05

MySQL 数据库命令

2010-06-10 17:51:35

2010-06-12 09:53:19

2010-06-10 18:08:58

2010-05-25 16:31:23

MySQL配置

2010-05-17 11:26:49

MySQL 多级同步

2010-05-27 14:35:25

MySQL批量导入

2010-05-28 18:16:43

MySQL 操作日志

2010-05-18 10:07:01

MySQL命令

2010-05-12 11:27:01

MySQL SQL

2010-05-26 14:55:43

MySQL存储过程

2010-05-28 13:48:07

MySQL数据库密码

2010-05-18 09:40:01

MySQL修改表字段
点赞
收藏

51CTO技术栈公众号