我安装了 MYSQLSERVER 5.1 。然后我安装了mysql-connector-java-3.0.8-stable-bin.jar并将驱动器c放入文件夹核心C:\ core.Then在计算机属性中,我创建了变量名为CLASSPATH和变量值的用户变量:C:\ core \ mysql-connector-java-3.0.8-stable-bin.jar。
现在我创建了数据库EMPLOYEE4 MY JAVA代码IS:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
class MySQLTest{
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection dbCon = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/EMPLOYEE4", "root", "root");
String query ="select count(*) from EMPLOYEE4 ";
Statement stmt = null;
ResultSet rs = null;
//getting PreparedStatment to execute query
stmt = dbCon.prepareStatement(query);
//Resultset returned by query
rs = stmt.executeQuery(query);
while(rs.next()){
int count = rs.getInt(1);
System.out.println("count of stock : " + count);
}
} catch (Exception ex) {
ex.printStackTrace();
//Logger.getLogger(CollectionTest.class.getName()).log(Level.SEVERE, null, ex);
} finally{
//close connection ,stmt and resultset here
}
}
}
我收到错误
SQL Connection exceptionjava.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:339 )
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:78 2)
at com.mysql.jdbc.Connection.createNewIO(Connection.j ava:1585)
at com.mysql.jdbc.Connection.<init>(Connection.java:5 24)
at com.mysql.jdbc.Driver.connect(Driver.java:359)
at java.sql.DriverManager.getConnection(DriverManager .java:512)
at java.sql.DriverManager.getConnection(DriverManager .java:193)
at DbHelper.getConnection(DbHelper.java:34)
at DbHelper.main(DbHelper.java:466)
** END NESTED EXCEPTION **
答案 0 :(得分:1)
此:
java.io.IOException: Unexpected end of input stream
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:339 )
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:78 2)
表示您正在通过classpath ok获取JDBC驱动程序(请参阅完全限定的包名称)。您无法连接到您的数据库,我怀疑您的MySQL数据库没有运行,或者没有配置为接受该端口上的连接。
你能连接到那个端口吗? (通过简单的telnet)或使用netstat确认某些内容正在侦听该端口?