【发布时间】:2012-04-08 06:24:33
【问题描述】:
我正在尝试使用 JDBC 连接器连接 Java 和 MySQL。因此,我从official cite 下载了连接器,将其添加到类路径并添加到 eclipse 的库中。 现在我正在使用下面的代码
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
}
}
}
但在字符串中:ResultSet rs = stmt.execute(query);出现错误“类型不匹配:无法从布尔值转换为结果集”。 我不明白问题是什么。需要一些帮助。
【问题讨论】:
-
感谢您的帮助。