我試圖用JDBC連接器連接Java和MySQL。所以,我從official cite下載了連接器,將它添加到classpath並添加到eclipse的庫中。 我現在正在使用的代碼下面Java + MySQL與JDBC連接錯誤
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class LocalhostDBConnection
{
LocalhostDBConnection()
{
Connection connection;
try {
// Название драйвера
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
// Create a connection to the database
String serverName = "localhost";
String mydatabase = "AvtoKovriki";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "root";
String password = "";
connection = DriverManager.getConnection(url, username, password);
System.out.println("is connect to DB" + connection);
String query = "Select * FROM users";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.execute(query);
String dbtime;
while (rs.next())
{
dbtime = rs.getString(1);
System.out.println(dbtime);
} // end while
connection.close();
} // end try
catch (ClassNotFoundException e)
{
e.printStackTrace();
// Could not find the database driver
} catch (SQLException e)
{
e.printStackTrace();
// Could not connect to the database
}
}
}
但在字符串:結果集RS = stmt.execute(查詢);有一個錯誤「Type mismatch:can not convert from boolean to ResultSet」。 我不明白是什麼問題。需要一些幫助。
謝謝你的幫助。 – 2012-03-23 17:27:29