我有一个 mysql 数据库,我正试图从我们的网站主机(godaddy)检索。我遵循了一种似乎正确的格式,但它告诉我:
java.sql.SQLException: No database selected
代码:
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM item_master";
ResultSet rs = stmt.executeQuery(sql); //<-- This is where the error is.
// STEP 5: Extract data from result set
while (rs.next()) {
// Retrieve by column name
int id = rs.getInt("id");
// Display values
System.out.print("ID: " + id);
}
...
}
我做了一个 conn 的打印语句,可能认为连接可能为空,并显示如下:
com.mysql.jdbc.JDBC4Connection@2a6*****
任何人都有任何想法可能导致这样的事情?