jdbc连接MySQL的问题?(困扰了好久)!请高手们赐教。
我写了段连接MySql程序如下:
package connectmysql;
/**
* <p>Title: 连接MySQL数据库</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.sql.*;
import com.mysql.jdbc.Driver;
public class conSql {
Connection con;
ResultSet rs;
Statement stmt;
public conSql() {
//定义连接类型及连接数据库 ACCESS
try {
// Load the JDBC driver
String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
//注册MySQL驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//数据库名
String dbName = "bookdb";
//表名
String tableName = "books";
//用户
String username = "dbuser";
//口令
String password = "1234";
String url="jdbc:mysql://localhost/"+dbName;
//连接MYSQL
con = DriverManager.getConnection(url,username,password);
con = DriverManager.getConnection(url);
//创建一个JDBC声明
stmt = con.createStatement();
}
catch (ClassNotFoundException e) {
// Could not find the database driver
System.out.println("Could not find the database driver!");
}
catch (SQLException e) {
// Could not connect to the database
System.out.println("Could not connect to the database!");
System.err.println(e.getMessage());
}
}
//定义数据库执行返回值 RS;
public ResultSet getResult(String strSQL) {
try {
rs = stmt.executeQuery(strSQL);
return rs;
}
catch (SQLException sqle) {
System.out.println(sqle.toString());
return null;
}
}
//定义数据库修改保存类
public boolean updateSql(String strSQL) {
try {
stmt.executeUpdate(strSQL);
con.commit();
return true;
}
catch (SQLException sqle) {
System.out.println(sqle.toString());
return false;
}
}
//定义关闭数据库类
public void closeConnection() {
try {
stmt.close();
con.close();
}
catch (SQLException sqle) {
System.out.println(sqle.toString());
}
}
//显示整个表
public void showTable(){
try {
rs = stmt.executeQuery("SELECT * FROM books");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1)
//用逗号分隔各列
System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
}
System.out.println("");
}
closeConnection();
}
catch (Exception ex) {
System.err.print("Exception: ");
System.err.println(ex.getMessage());
}
}
//主程序
public static void main(String[] args) {
conSql da = new conSql();
da.showTable();
}
}
结果执行时报连接错误如下:
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.<init>(Connection.java:491)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at connectmysql.conSql.<init>(conSql.java:39)
at connectmysql.conSql.main(conSql.java:122)
** END NESTED EXCEPTION **
Could not connect to the database!
Exception: null
但我的MySQL5.0版本完全正常,且我可以从cmd中登录并操作MySQL,开机时就把MySQL服务给启动了,为什么在程序中就不行,让我困扰了好久,请大虾们赐教!!!