java.sql.SQLException:未选择数据库 [英] java.sql.SQLException: No database selected
问题描述
如上所述我有一个问题是选择数据库
as above i have a problem to "select the database"
我正在使用xampp,我在MySQL中创建了一个数据库,并将其命名为employees
i am using xampp, there i created a database in MySQL and named it "employees"
这是我的java代码:
This is my java code:
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到达数据库员工
As seen in 'sql' I try to reach the database, using FROM employees
我是数据库编程新手。
我需要找到数据库的路径吗?如何以及在哪里可以找到它?
I am new to programming with databases. Do i need to find the path of the database? how and where can i find it?
推荐答案
更改连接字符串以使其连接到localhost上的正确数据库
Change your connection string to make it connect to the right database on localhost
conn = DriverManager.getConnection("jdbc:mysql://localhost/employees?user=root&password=");
或者你可以指定表的完整路径,即数据库.tablename
:
Alternatively you can specify the "full" path to the tables, i.e. database.tablename
:
sql = "SELECT id, first, last, age FROM employees.employees";
这篇关于java.sql.SQLException:未选择数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!