我试图用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