大哥们,这个问题实在解决不了了:jsp连接mysql问题
发表于 : 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名字空间,然后通过上下文来访问数据库也不行,所以不知道是什么问题,那位大哥能帮小弟解决一下这个问题啊!!!不胜感激。。。
问题描述:通过本地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名字空间,然后通过上下文来访问数据库也不行,所以不知道是什么问题,那位大哥能帮小弟解决一下这个问题啊!!!不胜感激。。。