Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Database selected when retrieving from mysql website

Tags:

java

mysql

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

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 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?

like image 637
JoeyL Avatar asked Dec 12 '22 17:12

JoeyL


1 Answers

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";

Where "mydb" is your database name.

like image 168
Stu Whyte Avatar answered Dec 25 '22 09:12

Stu Whyte