java.sql.SQLException:未选择任何数据库
如上所述,我在“选择数据库”时遇到问题 我使用的是xampp,我在MySQL中创建了一个数据库,并将其命名为“employees” 这是我的java代码:java.sql.SQLException:未选择任何数据库,java,mysql,database,jdbc,Java,Mysql,Database,Jdbc,如上所述,我在“选择数据库”时遇到问题 我使用的是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");
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”中所看到的,我尝试使用员工提供的访问数据库
我不熟悉数据库编程。
我需要找到数据库的路径吗?如何以及在哪里可以找到它?您需要在连接字符串中指定要使用的数据库:
conn = DriverManager.getConnection("jdbc:mysql://localhost/employees?user=root&password=");
更改连接字符串,使其连接到本地主机上的正确数据库
conn = DriverManager.getConnection("jdbc:mysql://localhost/employees?user=root&password=");
或者,您可以指定表的“完整”路径,即数据库。tablename
:
sql = "SELECT id, first, last, age FROM employees.employees";