手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux下用JAVA和JSP连接MySQL连接池

[日期:2011-04-05] 来源:Linux社区  作者:Linux [字体:大 中 小]

今天在机器上做了一下tomcat如何连接mysql,记录如下!

先下载mysql jdbc   mysql-connector-Java-5.1.7-bin.jar
然后把它放到TOMCAT的安装目录LIB下面:我的是/home/tomcat_new/lib/

然后需要配置两个文件,一个是context.xml、另一个是web.xml
安装就不再叙述了,这里只写配置
一。安装myql
然后在mysql中创建库和表:
mysql> create database first;
mysql> grant all privileges on first.* to first@localhost identified by "first";
mysql> use first;
mysql> create table about(id int(8) primary key,name varchar(10));
mysql> insert into about vlaues('xyw1026','laojiang');
mysql> flush privileges;

mysql> select * from aout;
mysql>select * from aout;
+----+----------+
| id | name     |
+----+----------+
|  0 | laojiang |
+----+----------+
1 row in set (0.00 sec)

1.先修改context.xml
在<Context> </Context>之间加入以下
<Resource name="jdbc/mysql"    
         auth="Container"
         type="javax.sql.DataSource"
         driverClassName="com.mysql.jdbc.Driver" 
         url="jdbc:mysql://localhost/first"  我的mysql是安装在本机,如果是在其他机器上要写上地址和端口
                                                             url="jdbc:mysql://192.168.1.34:3306/first"  first 是我新建的数据库名字
         username="first"  数据库的用户名
         password="first"    数据库的密码
         maxActive="100"  
         maxIdle="30"
       maxWait="10000" />
2.修改web.xml
在<web-app>和</web-app>之间加入以下:
<resource-ref>
     <description>DB Connection</description>
     <res-ref-name>jdbc/mysqlx</res-ref-name>
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
   </resource-ref>
这步配置成功后写一个test.jsp来测试是否成功。
vi test.jsp

<%@ 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="first";
//密码
String userPasswd="first";
//数据库名
String dbName="first";
//表名
String tableName="about";
//联结字符串

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 name = rs.getString("name");
out.print(name+" ");
}
rs.close();
statement.close();
connection.close();
%>

更改完配置文件后,需要重新启动tomcat
在网页上测试http://192.168.1.34:8081/test.jsp
出现:laojiang 证明调用mysql成功。

不过今天看到tomcat后台总是报一个错误,具体是什么意思在网上也查了很久,还不是很清楚,据一篇文档说明是tomcat的问题,但是好像没有影响使用。
报错如下:

严重: A web application registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

看这里 http://www.linuxidc.com/Linux/2011-04/34138p2.htm

linux
相关资讯       Tomcat 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 大事发生 发表于 2011/11/1 15:31:11
兄弟拜托,仔细看看,你用的是连接池吗? 呀呢***铃啊