2014-10-21 56 views
1

如上面我有一個問題,以「選擇數據庫」值java.sql.SQLException:沒有數據庫中選擇

我使用XAMPP,在那裏,我創造了在MySQL數據庫中,並把它命名爲「僱員」

這是我的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

我是新來與數據庫編程。 我是否需要找到數據庫的路徑?如何以及在哪裏可以找到它?

回答

2

更改連接字符串,使其連接到正確的數據庫在本地主機上

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

另外,您可以指定表格的「完整」路徑,即database.tablename

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

您需要指定要在連接字符串中使用的數據庫:

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