2014-03-04 84 views
0

我對編程非常陌生,以前從未使用過MySQL,所以請耐心等待!我試圖將MySQL工作臺連接到我的eclipse項目,但我不知道從哪裏開始。有人可以解釋我需要經歷的過程以及如何實施它們?將MySQL工作臺連接到Eclipse

謝謝!

回答

1

在Eclipse

創建一個Java項目

創建的軟件包名稱com.example.sql

在您的項目

package com.example.sql; 

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

public class JDBCExample { 
    public static Connection con; 
    public static void main(String[] argv) { 
     try 
     { 
      connectionQuery(); 

      PreparedStatement statement = con.prepareStatement("SELECT * from table_name");/*write query inside of prepared statement*/ 
      ResultSet result = statement.executeQuery(); 
      System.out.println("DataBase table accessed"); 

      while(result.next()) 
      { 
       String retrievedid= result.getString("Column_name"); 

       System.out.println(retrievedid); 
      } 


      con.close(); 
     } 

     catch(Exception e) 
     { 
      e.printStackTrace(); 
      System.out.println(e.getMessage().toString()); 
     } 
    } 

    public static void connectionQuery() 
    { 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver"); 
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/boopathi","root","root"); 
      System.out.println("Remote DB connection established"); 
     } 
     catch (ClassNotFoundException e) 
     { 
      e.printStackTrace(); 
      System.out.println("Remote server could not be connected"); 
     } 
     catch (NullPointerException e) 
     { 
      e.printStackTrace(); 
      System.out.println("Remote server could not be connected"); 
     } 
     catch (SQLException e) 
     { 
      e.printStackTrace(); 
      System.out.println("Remote db connection establishment error"); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      System.out.println("False query"); 
     } 
    } 
} 

Refer this link also

+0

嗨包括mysqlconnector.jar!謝謝你的幫助!我不認爲你可以評論代碼或向我解釋?我希望能夠自己理解:) – user3166793

+0

我在下面提到的鏈接,這個鏈接一切都詳細解釋了 – Boopathi

+0

哦對不起,我錯過了,再次感謝! – user3166793