2014-01-26 57 views
2

我想知道如何連接到Xampp MySQL中託管的數據庫。如何連接到Java中的數據庫連接

這是我到目前爲止在我的java代碼連接,但我不確定我在做什麼。

public static void main(String[] args) { 
    try { 
     Connection con = DriverManager.getConnection(host, username, password); 
     String host = "jdbc:derby://localhost:1527/Employees"; 
     String uName = "root"; 
     String uPass= "password"; 
    } 
    catch (SQLException err) { 
    System.out.println(err.getMessage()); 
    } 

} 
  1. 什麼將主機URL是什麼?
  2. 我需要一個JDBC Jar文件來連接到數據庫嗎?

我已經通過phpMyAdmin建立了一個數據庫和表。只是不知道如何繼續。

我使用Netbeans的寫我的Java代碼連接到其經XAMPP的phpMyAdmin創建一個本地數據庫。

最後,我想用Java創建數據庫連接並在IDE中調用表。希望得到一些幫助。

+0

當您嘗試連接時是否出現錯誤? – lakshman

回答

3

這是項目結構

Project Structure

嘗試用這種

更新

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
    } catch (ClassNotFoundException e) { 
     System.out.println("Where is your MySQL JDBC Driver?"); 
     e.printStackTrace(); 
     return; 
    } 

    System.out.println("MySQL JDBC Driver Registered!"); 
    Connection connection = null; 

    try { 
     connection = DriverManager 
     .getConnection("jdbc:mysql://localhost:3306/testspring","root", "password"); 

    } catch (SQLException e) { 
     System.out.println("Connection Failed! Check output console"); 
     e.printStackTrace(); 
     return; 
    } 

    if (connection != null) { 
     System.out.println("You made it, take control your database now!"); 
    } else { 
     System.out.println("Failed to make connection!"); 
    } 
} 

這是爲我工作

我下載了從罐子Java2s.com

Refer

+0

問題。我在哪裏添加jar文件?到我的Netbeans項目目錄?我是否必須複製zip文件中的所有文件? – Jeiman

+1

創建一個文件夾庫並添加它們的jar –

+0

我是否必須重命名jar文件或保留它的命名約定?我還可以寫什麼代碼來提及與數據庫的連接已經成功? – Jeiman

0

也許你應該在使用它們之前定義你的字符串。

主機URL似乎很好。

您必須在您的服務器中安裝驅動程序,或將其放入您項目的/lib文件夾中。您填寫找到它在%DERBY_HOME%/lib文件夾中,名爲derby.jar,假設%DERBY_HOME%是Derby的目錄安裝。

+0

我相信我正在使用mysql,這意味着我需要將url路徑更改爲以下權限,'String host =「jdbc:mysql:// localhost:3306/db_test14」;'?請指教。 – Jeiman

+0

你是對的,假設MySQL位於端口3306上,並且數據庫名稱是'db_test14'。那麼你需要一個[MySQL JDBC驅動程序](http://dev.mysql.com/downloads/connector/) – Happy

1

這是一個代碼(Java 7的風格,嘗試,有資源,更簡潔的方式),從您的數據庫連接和檢索數據。

public static final String SELECT_QUERY = "SELECT * FROM your_table_name"; 
public static void main(String[] args) { 
    String host = "jdbc:derby://localhost:1527/Employees"; 
    String uName = "root"; 
    String uPass = "password"; 

    try (Connection conn = DriverManager.getConnection(host, uName, uPass); 
     Statement stmt = conn.createStatement(); 
     ResultSet rs = stmt.executeQuery(SELECT_QUERY)) { 

     while (rs.next()) { 
      //read your lines one ofter one 
      int id = rs.getInt("id"); 
      String somePropertyValue = rs.getInt("some_column_name"); 
      // etc. 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 
} 

此外,在類路徑添加JDBC驅動程序(* .jar文件),如果您是從命令行中運行,或者這個jar添加到您的項目,如果您在使用IDE(Eclipse中,IDEA等。每一個都有點不同)。

順便說一句,如果您的代碼編譯如果變量聲明是他們使用後?該代碼甚至無法編譯。

0
package com.util; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 



public class DatabaseUtil1 { 

    public static final String DRIVER="oracle.jdbc.driver.OracleDriver"; 
    public static final String URL="jdbc:oracle:thin:@DIPAK-PC:1521:xe"; 
    public static final String USERNAME="system"; 
    public static final String PASSWORD="password"; 

    public static Connection getConnection() throws ClassNotFoundException, SQLException 
    { 
     Connection con= null; 

     Class.forName(DRIVER); 
     con=DriverManager.getConnection(URL,USERNAME,PASSWORD); 

     System.out.println(con); 
     return con; 
    } 
    public static void closePreparedStatement(PreparedStatement pst) throws SQLException 
    { 
     if(pst!=null) 
     { 
      pst.close(); 
     } 

    } 
    public static void closeConnection(Connection con) throws SQLException 
    { 
     if(con!=null) 
     { 
      con.close(); 
     } 

    } 



} 
0

導入軟件包:要求包含包含數據庫編程所需的JDBC類的軟件包。大多數情況下,使用import java.sql。*就足夠了。

註冊JDBC驅動程序:要求初始化驅動程序,以便可以打開與數據庫的通信通道。

打開連接:需要使用DriverManager.getConnection()方法創建一個Connection對象,該對象表示與數據庫的物理連接。