高手sos!!!
用一个.jsp程序,查询mysql的数据库中的一个表,结果:数据库连接失败。
但是我在dos下,用这个用户连接mysql,却可以查到数据。
具体问题:
运行到
Connection con=DriverManager.getConnection(url,user,password);
就出现异常,页面显示:“数据库连接失败”。
---------------------------------------------------------
打印异常如下:
java.sql.SQLException: Communication link failure: java.io.IOException, underlying cause:
Unexpected end of input stream ** BEGIN NESTED EXCEPTION ** java.io.IOException MESSAGE:
Unexpected end of input stream STACKTRACE: java.io.IOException: Unexpected end of input
stream at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:1096) at
com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:626) at
com.mysql.jdbc.Connection.createNewIO(Connection.java:1562) at
com.mysql.jdbc.Connection.(Connection.java:491) at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346) at java.sql.DriverManager.getConnection(DriverManager.java:512) at
...
java.lang.Thread.run(Thread.java:534) ** END NESTED EXCEPTION **
-----------------------------------------------------------------------------------------
下面是我的详细操作步骤,请高手指点!!!
环境:
win2000;
tomcat4.1;
F:\mysql-4.1.8-win;
1.
创建了一个用户;
用户名:test
密码:test
----命令:
grant select,insert,update,delete on testaddressbook.* to test@localhost identified by
"test";
--创建成功.
2.
创建testaddressbook数据库和表addressbooktable;
mysql -uroot -proot < F:\tomcat4.1\webapps\addressbook\sql\test.sql
--创建成功
####################### test.sql
DROP DATABASE IF EXISTS testaddressbook;
create database testaddressbook;
use testaddressbook;
create table addressbooktable
(
id int auto_increment primary key,
name varchar(30),
address varchar(255),
phone varchar(20)
);
insert into addressbooktable
(name,address,phone)
values
('jjx','zhejiang yuyao','0574-2222222');
3.
将mysql-connector-java-2.0.14-bin.jar,
放到F:\tomcat4.1\common\lib之下。
4.启动tomcat,运行测试程序:testmysqljdbc.jsp,
提示:数据库连接不上。
####################### testmysqljdbc.jsp
<html><head><title>测试</title></head>
<body><center>
<%@ page language="java" import="java.sql.*" contentType="text/html;charset=gb2312" %>
<%
String driver="org.gjt.mm.mysql.Driver";
String url="jdbc:mysql://localhost:3306/testaddressbook";
String user="test";
String password="test";
try{
Class.forName(driver);
}catch(Exception e){
out.println("无法加载驱动程序!!"+driver);
e.printStackTrace();
}
try
{
Connection con=DriverManager.getConnection(url,user,password);
/* if(!con.isClosed( ))
out.println("数据库连接成功!");
*/
Statement sqlStmt=con.createStatement (java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
String sqlQuery="select * from addressbooktable";
ResultSet sqlRst=sqlStmt.executeQuery (sqlQuery);
out.println("<BR>");
while (sqlRst.next()) {
out.println(sqlRst.getString(1));
out.println(sqlRst.getString(2));
out.println(sqlRst.getString(3));
}
con.close( );
}
catch(SQLException ee)
{out.println("数据库连接失败!");
}
%>
</center>
</body>
</html>
--转自