3

I have the following code in my java class, but I am unable to create a single table, as it wouldn't select a database

//STEP 1. Import required packages
import java.sql.*;

public class DBTest {
    // Database credentials
    private static final String USER = "root";
    private static final String PASS = "1234";

    private static Connection conn = null;
    private static Statement stmt = null;

    static final String DB_URL = "jdbc:mysql://localhost/";

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        getConnection();
    }

    public static Connection getConnection() throws SQLException, ClassNotFoundException {

        try {
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            createTable(conn);

        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            } // nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            } // end finally try
        } // end try
        return conn;
    }

    public static void createTable(Connection conn) throws SQLException {
        System.out.println("Creating database...");
        stmt = conn.createStatement();

        // Open a connection
        System.out.println("Connecting to a selected database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        System.out.println("Connected database successfully...");

        // Execute a query
        System.out.println("Creating table in given database...");

        String sql = "CREATE TABLE REGISTRATION " + "(id INTEGER not NULL, " + " first VARCHAR(255), "
                + " last VARCHAR(255), " + " age INTEGER, " + " PRIMARY KEY ( id ))";

        stmt.executeUpdate(sql);
        System.out.println("Created table in given database...");
    }

}

Please Note: The only library, which I have imported is java.sql and that I have got my connector inside my referenced library, so everything should be set up! enter image description here

With all this in mind, can anyone please let me know why I get a No database selected SQLException?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user3153278
  • 297
  • 2
  • 4
  • 20

2 Answers2

6

In yours DB_URL variable is missing the name of the Database:

You need just:

jdbc:mysql://localhost/my_db_name

where my_db_name is the name of the Database you created.

antoniodvr
  • 1,259
  • 1
  • 14
  • 15
  • Thank you for your comment! The thing is, I do not have a database, and I wanted to create a database from the code, so that whenever someone executes this code, everything which they will create will happen on localhost. – user3153278 Nov 12 '15 at 20:20
  • 2
    http://stackoverflow.com/questions/717436/create-mysql-database-from-java – GriffeyDog Nov 12 '15 at 20:24
  • VoodooBiiT, I have not created a dataBase anywhere! This is the main problem I am trying to resolve! How can I create an instance of a database, so that no matter who uses my code, a new database unit will always get created? – user3153278 Nov 12 '15 at 21:23
2

You can only create a table if you are connected to a database. Currently you are only connected to the server, so statements like CREATE TABLE cannot be executed, as there as nothing to create that table in.

You either need to specify the database in your connection string:

jdbc:mysql://localhost/<database>

Or you need to explicitly need to switch to a database using:

connection.setCatalog("<database>");

From the comments it looks like your actual question is how to create a new database from java, for an answer to that question, you need to look at: Create MySQL database from Java.

Community
  • 1
  • 1
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197