java web 连接mysql数据库

一:概述(overview

介绍一下JSP怎么通过tomcat,连接后台的mysql数据库。

 Introduce how to connect backend mysql data base.


二:拓扑图 (topological graph


实验环境(experimental envieonment):Centos5.8(kernel 2.6.18)+tomcat5.5+mysql5.0


三:JSP连接MySQL  (JSP connect Mysql)

  1. web服务器 搭建--->详见 http://haicang.blog.51cto.com/2590303/963670    
  2. mysql数据库服务器的搭建-->详见http://haicang.blog.51cto.com/2590303/930541



tomcat配置连接池  (tomcat deploy pools)

 

 
 
  1. tomcat连接池配置 
  2. vi/vim server.xml
  3.  
  4. Oracle数据库的连接池配置
  5. <host> </host>中配置如下信息 
  6. <Resource
  7. auth="Container"
  8. description="sqlserver Datasource"
  9. name="jdbc/ora"
  10. type="javax.sql.DataSource"
  11. maxActive="50"
  12. maxIdle="10"
  13. username="" ---->连接数据库的用户名
  14. maxWait="10000"
  15. driverClassName="oracle.jdbc.driver.OracleDriver"
  16. password=""----->连接数据库的用户密码
  17. url="jdbc:oracle:thin:@host:port/databases"
  18. removeAbandoned="true"
  19. removeAbandonedTimeout="60"
  20. logAbandoned="true"/>
  21.  
  22. MySQL数据库的连接池配置
  23.  
  24. <Resource
  25. name="jdbc/TestDB"
  26. auth="Container" type="javax.sql.DataSource"
  27. maxActive="100"
  28. maxIdle="30"
  29. maxWait="10000"
  30. username="javauser"
  31. password="javadude"
  32. driverClassName="com.mysql.jdbc.Driver"
  33. url="jdbc:mysql://localhost:3306/javatest"/>
  34.  
  35. SQL的连接池配置
  36. <Resource
  37. auth="Container"
  38. description="sqlserver Datasource"
  39. name="jdbc/sqlserver110"
  40. type="javax.sql.DataSource"
  41. maxActive="100"
  42. maxIdle="10"
  43. username=""
  44. maxWait="10000"
  45. driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
  46. password=""
  47. url="jdbc:microsoft:sqlserver:IP(端口);数据库名字;"reconnect=true"
  48. removeAbandoned="true"
  49. removeAbandonedTimeout="60"
  50. logAbandoned="true" />

tomcat5.5参数解释:

 
 
  1. tomcat5.5参数说明: 
  2. 1  maxActive: Maximum number of dB connections in pool. Make sure you 
  3.            configure your mysqld max_connections large enough to handle 
  4.            all of your db connections. Set to -1 for no limit 
  5.          连接池中最大的连接数 设为-1 表示不限制  注意数据的连接数要大于此连接数 
  6. 2  maxIdle: Maximum number of idle dB connections to retain in pool. 
  7.          Set to -1 for no limit.  See also the DBCP documentation on this 
  8.          and the minEvictableIdleTimeMillis configuration parameter      
  9.          保持在连接中最大的闲置连接数(在连接池最大的空闲连接数) 
  10. 3  maxWait: Maximum time to wait for a dB connection to become available 
  11.          in ms, in this example 10 seconds. An Exception is thrown if 
  12.          this timeout is exceeded.  Set to -1 to wait indefinitely 
  13.          等待一个连接成为可用连接的最大等待时间 单位毫秒ms 
  14. 4  driverClassName: Class name for the old mm.mysql JDBC driver is 
  15.          org.gjt.mm.mysql.Driver - we recommend using Connector/J though. 
  16.          Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.       
  17. 5    url: The JDBC connection url for connecting to your MySQL dB 
  18. 6    removeAbandoned="true" (abandoned dB connections are removed and recycled) 
  19.         解释:被遗弃的数据连接 回收到连接池中    默认为false 
  20. 7    removeAbandonedTimeout="60"(a dB connection has been idle before it is considered abandoned)单位秒 
  21.         解释:在一个连接空闲多少秒会被遗弃 
  22. 8   logAbandoned="true"  
  23.         记录被遗弃的数据连接 默认为false
在web应用程序的目录下创建WEB-INF/web.xml,并添加如下内容:

 
 
  1. web.xml configuration 
  2. <resource-ref> 
  3.  <description>Oracle Datasource example</description> 
  4.  <res-ref-name>jdbc/myoracle</res-ref-name> 
  5.  <res-type>javax.sql.DataSource</res-type> 
  6.  <res-auth>Container</res-auth> 
  7. </resource-ref> 

JSP连接数据库的用户:

    • MySQL configuration  
    • mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@localhost  
    •     ->   IDENTIFIED BY 'javadude' WITH GRANT OPTION; 
    • mysql> create database javatest; 
    • mysql> use javatest; 
    • mysql> create table testdata ( 
    •     ->   id int not null auto_increment primary key, 
    •     ->   foo varchar(25),  
    •     ->   bar int);       
    • mysql> insert into testdata values(null, 'hello', 12345); 
    • Query OK, 1 row affected (0.00 sec) 
    • mysql> select * from testdata; +----+-------+-------+ | ID | FOO | BAR | +----+-------+----


JSP测试页面:

  • <%@ page import="java.sql.*" %> 
  • <%@ page contentType="text/html; charset=gb2312" %> 
  • <%@ page language="java" %> 
  • <%@ page import="com.mysql.jdbc.Driver" %> 
  • <%@ page import="java.sql.*" %> 
  • <
  • String driverName="com.mysql.jdbc.Driver"
  • String userName="javauser"
  • String userPasswd="java"
  • String dbName="javatest"
  • String tableName="testdata"
  • String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd; 
  • Class.forName("com.mysql.jdbc.Driver").newInstance(); 
  • Connection connection=DriverManager.getConnection(url); 
  • Statement statement = connection.createStatement(); 
  • String sql="SELECT * FROM "+tableName; 
  • ResultSet rs = statement.executeQuery(sql); 
  • while (rs.next()) 
  •  String foo = rs.getString("foo"); 
  •  String bar = rs.getString("bar"); 
  • out.print(foo+" "); 
  • out.print(bar+" "); 
  • rs.close(); 
  • statement.close(); 
  • connection.close(); 
  • %>

/*第一次编排带源码的博客,排版不是太好,多多包涵*/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值