I'm creating a java program that creates a MySQL database and table. I'm fairly new, so I'm learning as I go. I was able to create the Database and then I proceed to create the table. When I ran my program I got this exception error
java.sql.SQLException: No database selected
I understand the error that I have, but I don't know I can select the database when I already have my URL setup to my root.
String url = "jdbc:mysql://localhost:3306/";
Below is my code, any feedback is welcome and thank you.
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MysqlSetUp{
private static final String EMPLOYEE_TABLE = "create table MyEmployees3 ( "
+ " id INT PRIMARY KEY, firstName VARCHAR(20), lastName VARCHAR(20), "
+ " title VARCHAR(20), salary INT )";
public static Connection getConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/";
String username = "root";
String password = "";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void main(String args[]) {
Connection conn = null;
Statement stmt = null;
try {
conn = getConnection();
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.executeUpdate(EMPLOYEE_TABLE);
stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(100, 'A')");
stmt.executeUpdate("insert into MyEmployees3(id, firstName) values(200, 'B')");
System.out.println("CreateEmployeeTableMySQL: main(): table created.");
} catch (ClassNotFoundException e) {
System.out.println("error: failed to load MySQL driver.");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("error: failed to create a connection object.");
e.printStackTrace();
} catch (Exception e) {
System.out.println("other error:");
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
You need to USE Employess
after creating it.
...
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.executeUpdate("USE Employess");
stmt.executeUpdate(EMPLOYEE_TABLE);
...
The answer provided by @clinomaniac is right and you should accept it as the right one. This one is to add some more context.
When you create a database as root, in order to work with it you have to select it, so you do it by issuing a USE
statement USE yourDatabaseName
as already said by @clinomaniac.
Another way to work with your database without using the use
command is to prepend every command with your database name like this:
stmt.executeUpdate("insert into Employess.MyEmployees3(id, firstName) values(100, 'A')");
stmt.executeUpdate("insert into Employess.MyEmployees3(id, firstName) values(200, 'B')");
You would have to add this at your create table statement as well:
private static final String EMPLOYEE_TABLE = "create table Employess.MyEmployees3 ( "
+ " id INT PRIMARY KEY, firstName VARCHAR(20), lastName VARCHAR(20), "
+ " title VARCHAR(20), salary INT )";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With