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!
With all this in mind, can anyone please let me know why I get a No database selected SQLException?