我正在创建一个java程序,它创建一个MySQL数据库和表。我是个新手,所以我是边学边学。我能够创建数据库,然后继续创建表。当我运行我的程序时,我得到了这个异常错误
java.sql.SQLException: No database selected我知道我的错误,但我不知道我可以选择数据库时,我已经有我的URL设置到我的根。
String url = "jdbc:mysql://localhost:3306/";下面是我的代码,欢迎任何反馈,谢谢。
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();
}
}
}
}发布于 2018-02-22 05:30:58
您需要在创建它之后执行USE Employess。
...
stmt = conn.createStatement();
stmt.executeUpdate("CREATE DATABASE Employess");
stmt.executeUpdate("USE Employess");
stmt.executeUpdate(EMPLOYEE_TABLE);
...发布于 2018-02-22 05:36:02
@clinomaniac提供的答案是正确的,您应该接受它是正确的。这一个是为了添加更多的上下文。
当您以根用户身份创建数据库时,为了使用它,您必须选择它,因此您可以通过发出@clinomaniac已经说过的USE语句USE yourDatabaseName来执行此操作。
在不使用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 )";发布于 2018-02-22 14:42:23
您还可以尝试将数据库名称添加到连接url。像这样修改您的getConnection()方法。
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;
}建库时调用getConnection(null),建库后调用getConnection("Employess")。
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.");https://stackoverflow.com/questions/48915735
复制相似问题