如上所述,我在“选择数据库”时遇到问题
我正在使用 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
访问数据库
我是数据库编程新手。 我需要找到数据库的路径吗?我如何以及在哪里可以找到它?
最佳答案
更改连接字符串以使其连接到本地主机上的正确数据库
conn = DriverManager.getConnection("jdbc:mysql://localhost/employees?user=root&password=");
或者,您可以指定表的“完整”路径,即database.tablename
:
sql = "SELECT id, first, last, age FROM employees.employees";
关于java.sql.SQLException : No database selected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26496408/