从 mysql 网站检索时未选择数据库 [英] No Database selected when retrieving from mysql website
本文介绍了从 mysql 网站检索时未选择数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 mysql 数据库,我试图从我们的网站主机 (godaddy) 中检索它.我遵循的格式似乎是正确的,但它告诉我:
I have a mysql database that I am trying to retrieve from our website host (godaddy). I followed a format that seems to be right but it tells me that:
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 的打印语句,可能认为连接可能为空并显示如下:
I did a print statement of the conn to maybe think the connection could of been null and was shown this:
com.mysql.jdbc.JDBC4Connection@2a6*****
有人知道是什么导致了这样的事情吗?
Anyone have any ideas what could cause something like this?
推荐答案
您的数据库 URL 应包含您的数据库名称.这通常是您的网址后跟/DBNAME".
Your URL for your database should include your database name. This is normally your URL followed by a "/DBNAME".
String URL = "jdbc:mysql://localhost:3306/mydb";
其中mydb"是您的数据库名称.
Where "mydb" is your database name.
这篇关于从 mysql 网站检索时未选择数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文