Java: creating a mysql database and table in one class

I am creating a java program that creates a MySQL database and a table. I'm pretty new, so I study when I leave. I managed to create a database, and then I started creating a 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 that I can select the database when I already have the URL setting for my root.

String url = "jdbc:mysql://localhost:3306/";

Below is my code, any feedback is appreciated and thanks 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();
     }
   }
  }
 }
+4
source share
3 answers

You need USE Employessafter creating it.

...
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.executeUpdate("USE Employess");
stmt.executeUpdate(EMPLOYEE_TABLE);
...
+6
source

, @clinomaniac, , . .

root, , , USE USE yourDatabaseName, @clinomaniac.

USE :

stmt.executeUpdate("insert into Employess.MyEmployees3(id, firstName) values(100, 'A')");
stmt.executeUpdate("insert into Employess.MyEmployees3(id, firstName) values(200, 'B')");

create table:

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 )";
+4

You can also try adding the database name to the connection URL. Change the method getConnection()as follows.

public static Connection getConnection(String dbName) throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost:3306/" + ((dbName != null)? (dbName) : (""));
    String username = "root";
    String password = "";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}

Call getConnection(null)to create a database and call getConnection("Employess")after creating the database.

conn = getConnection(null);
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.close();
conn.close();

conn = getConnection("Employess");
stmt = conn.createStatement();
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.");
+3
source

Source: https://habr.com/ru/post/1693947/


All Articles