我已经安装了MYSQLSERVER 5.1。然后我安装了 mysql-connector-java-3.0.8-stable-bin.jar 并将驱动器 c 放入文件夹 core 为 C:\core。然后在计算机的属性中我创建带有变量的用户变量命名 CLASSPATH 和变量值:C:\core\mysql-connector-java-3.0.8-stable-bin.jar。
现在我创建了数据库 EMPLOYEE4 我的 JAVA 代码是:
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 **