java.sql.SQLException:未选择数据库

如上所述我有“选择数据库”的问题

我正在使用xampp,我在MySQL中创建了一个数据库,并将其命名为“employees”

这是我的java代码:

public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection("jdbc:mysql://localhost?user=root&password="); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM employees"; ResultSet rs = stmt.executeQuery(sql); 

如’sql’所示,我尝试使用FROM employees访问数据库

我是数据库编程的新手。 我需要找到数据库的路径吗? 我怎么能在哪里找到它?

更改连接字符串以使其连接到localhost上的正确数据库

 conn = DriverManager.getConnection("jdbc:mysql://localhost/employees?user=root&password="); 

或者,您可以指定表的“完整”路径,即database.tablename

 sql = "SELECT id, first, last, age FROM employees.employees"; 

您需要在连接字符串中指定要使用的数据库:

 conn = DriverManager.getConnection("jdbc:mysql://localhost/employees?user=root&password=");