从mysql网站检索时没有选择数据库 [英] No Database selected when retrieving from mysql website

查看:183
本文介绍了从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的声明可能认为连接可能为null并显示为:

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应包含您的数据库名称。这通常是您的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆