日期:2014-05-20  浏览次数:20763 次

java连mysql错误
import     java.io.*;
import     java.util.*;
import     java.sql.*;

public   class   ConMysql{

public   static   void   main(String   []args)   throws   IOException{
try{
Connection   con;
Statement   stmt;
ResultSet   rs;

Class.forName( "com.mysql.jdbc.Driver ");
DriverManager.registerDriver(new   com.mysql.jdbc.Driver());
String   dburl   =   "jdbc:mysql://localhost:3306/BookDB?useUnicode=true&characterEncoding=GB2312 ";
String   dbUser= "dbuser ";
String   dbPwd= "1234 ";
con   =   java.sql.DriverManager.getConnection(dburl,dbUser,dbPwd);

stmt   =   con.createStatement();
rs   =   stmt.executeQuery( "select   id,name,title,price   from   books ");

while(rs.next()){
String   col1   =   rs.getString(1);
String   col2   =   rs.getString(2);
String   col3   =   rs.getString(3);
float   col4   =   rs.getFloat(4);
System.out.println(col1+ "   "+col2+ "   "+col3+ "   "+col4+ "   ");
}

rs.close();
stmt.close();
con.close();
}
catch(Exception   e){
System.out.println(e.getMessage());
}

}

}


环境变量都已经配置好了,驱动也加入classpath里了。

javac   ConMysql.java   成功

java   ConMysql   出现下面错误:

D:\> java   ConMysql
Communication   link   failure:   java.io.IOException,   underlying   cause:   Unexpected   en
d   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. <init> (Connection.java:491)
                at   com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:346)
                at   java.sql.DriverManager.getConnection(Unknown   Source)
                at   java.sql.DriverManager.getConnection(Unknown   Source)
                at   ConMysql.main(ConMysql.java:21)


**   END   NESTED   EXCEPTION   **

------解决方案--------------------
at ConMysql.main(ConMysql.java:21)
看看你程序的这一行,我感觉是数据库没联上,不是驱动的事就是用户访问权限的事,要不就是密码不对
------解决方案--------------------
不知道,up
------解决方案--------------------
驱动程序当版本可能跟数据库服务器当版本不匹配。