大哥们,这个问题实在解决不了了:jsp连接mysql问题

Web、Mail、Ftp、DNS、Proxy、VPN、Samba、LDAP 等基础网络服务
回复
kgn28
帖子: 3
注册时间: 2008-09-22 18:57

大哥们,这个问题实在解决不了了:jsp连接mysql问题

#1

帖子 kgn28 » 2009-08-24 14:29

软件版本:mysql5.1和tomcat6和jdbc5.1
问题描述:通过本地java程序,可以读取mysql中的数据,但是通过tomcat的容器中的jsp却不可以。jdbc的jar确实又被加载到环境中,因为com.mysq.jdbc.Driver这个类可以找找到。
本地程序:
import java.sql.*;

public class MysqlCon {

public MysqlCon(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ch01", "root","kgn1986");
Statement stmt = conn.createStatement();
String sql = "select * from user_info";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String args[]){
new MysqlCon();
}
}
40 可以访问数据库,结果如下:
kgn@ubuntu:~/program/java$ java MysqlCon
Yingkui Shen
Ling Zeng
Family Happy
kgn@ubuntu:~/program/java$
但是一个简单的jsp程序,如下:
1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
2 <%@ page import="java.sql.*"%>
3 <%@ page import="java.io.*"%>
4 <%@ page import="javax.sql.*"%>
5 <%@ page import="javax.naming.*"%>
6 <html>
7 <body>
8 <br>database querying test.</br>
9 <%
10 try {
11 Class.forName("com.mysql.jdbc.Driver");
12 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/kgn?user=root&password=kgn1986");
13
23 } catch (Exception ex) {
24 StringWriter sw = new StringWriter();
25 PrintWriter pw = new PrintWriter(sw);
26 ex.printStackTrace(pw);
27 out.println(sw.toString());
28 out.println(ex);
29 }
30 %>
31 </body>
32 </html>
出现问题:
database querying test.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 1,251,095,183,275 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago. at sun.reflect.GeneratedConstructorAccessor6.newInstance(Unknown Source)..................省略一大堆例外。
jdbc驱动已经放入到了/usr/share/tomcat6/lib下,并且在jre_home/lib/ext下也已经有了。按理在/usr/share/tomcat6/lib下面已经没有必要放入jdbc驱动了,看到网上有人说tomcat加载类的过程会去加载jre_home/lib/ext下面的jar包。另外我java和tomcat的环境都没有设置,但是并不影响通过service tomcat6 restart启动tomcat。

google了N久,放入到JNDI名字空间,然后通过上下文来访问数据库也不行,所以不知道是什么问题,那位大哥能帮小弟解决一下这个问题啊!!!不胜感激。。。
kgn28
帖子: 3
注册时间: 2008-09-22 18:57

Re: 大哥们,这个问题实在解决不了了:jsp连接mysql问题

#2

帖子 kgn28 » 2009-08-25 18:27

问题解决了。
原来我的tomcat是通过apt安装的,估计是启动脚本的问题,环境搞乱了,有的类没有加载进来,后来直接去下载了一个完整的安装包,解压一下,就可以直接使用了。
还有就是java环境我没有设置到环境变量里面,仔细看看tomcat的启动脚本,它是有自动检测的,就是换成root用户去执行话,估计bashrc里面的设置成忽略掉本机用户的设置,所以在tomcat的启动脚本开头地方自己加上一个JAVA_HOME就可以了。
总结一条就是千万不要用apt安装tomcat。问题一大堆。
头像
yus253
帖子: 389
注册时间: 2009-05-31 20:52
联系:

Re: 大哥们,这个问题实在解决不了了:jsp连接mysql问题

#3

帖子 yus253 » 2010-01-27 21:47

apt-get那个会出现这个问题吗?还好我的tomcat是自己编译的安装的。连环境都没配……真行
The root of bioengineering is the desire to control the organic long enough to improve it. Domesticated plants and animals are examples of technos -- logic applied to life.
头像
yus253
帖子: 389
注册时间: 2009-05-31 20:52
联系:

Re: 大哥们,这个问题实在解决不了了:jsp连接mysql问题

#4

帖子 yus253 » 2010-01-27 21:48

你是通过jdbc-odbc连接的吗?还是jtds呀?
The root of bioengineering is the desire to control the organic long enough to improve it. Domesticated plants and animals are examples of technos -- logic applied to life.
回复