Java – no database selected when retrieving from MySQL website

I have a MySQL database and I try to retrieve it from our website host (GoDaddy) I followed a seemingly correct format, but it told me:

java.sql.sqlException: No database selected

Code:

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);
        }
...
}

I made Conn's print statement. I may think that the connection may be null and display it as follows:

com.MysqL.jdbc.JDBC4Connection@2a6*****

What does anyone think will lead to such a thing?

Solution

Your database URL should contain your database name This is usually your URL followed by "/ dbname"

String URL =“jdbc:MysqL:// localhost:3306 / mydb”;

"Mydb" is your database name

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>