2014-10-16 51 views
0

好吧,我真的很新的Java,我不知道如何真正做到這一點。如果你們能幫助我,那麼我可能會了解更多。我有我的主類(Mysql.java)和一個Connection類(Connection.java)現在我希望它使用Connection.java部分連接到數據庫。但我想從Mysql.java使用它。我現在這個權利:用Java連接到MySQL

Mysql.java

public class Mysql { 

    public static void main(String[] args) { 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     int w = screenSize.width; 
     JFrame frame = new MainFrame(w/5, 100, 2*w/5, 0); 
     frame.show(); 
     new Connection(); 
    } 


} 
class MainFrame extends JFrame { 
    MainFrame(int width, int height, int x, int y) { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setTitle("Todothelydoo - To do planner"); 
     setSize(width, height); 
     setLocation(x, y); 
     JPanel mainPanel = new JPanel(); 
     mainPanel.setBackground(Color.blue); 
    } 
} 

Connection.java

public class Connection { 
    public static Connection getConnection() throws SQLException { 
     Connection conn = null; 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver"); 
      String url = "jdbc:mysql://h2318966.stratoserver.net/"; 
      String user = "root"; 
      String password = ""; 
      conn = (Connection) DriverManager.getConnection(url, user, password); 
      System.out.println("Connection established"); 
     } 
     catch(ClassNotFoundException e) 
     { 
      System.out.println(e.getMessage()); 
      System.exit(0); 
     } 
     catch(SQLException e) 
     { 
      System.out.println(e.getMessage()); 
      System.exit(0); 
     } 
     return conn; 
    } 
} 

我試着用在mysql.java但即時通訊新的連接可能是一個完全白癡。如果你們能幫助我,那會很棒。

+1

您需要在您的'Mysql'類的主要方法中爲'Connection'實例調用'getConnection()'方法。 – Philip 2014-10-16 18:27:11

+0

您有一個名爲'Connection'的類,並且您正嘗試在同一個類中使用'java.sql.Connection'。這可能會導致名稱衝突 - 給你的班級打電話。 – 2014-10-16 18:33:18

+0

我得到這個:new Msqlconn.getConnection();這是錯的嗎?我不明白。我很抱歉 – user2773749 2014-10-16 18:34:26

回答

0

您可以在url字符串中使用您自己的主機和數據庫名稱的以下代碼。我已經測試過它。

注:包含的mysql-connector.jar在您的項目

Mysql.java

import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.Toolkit; 
    import java.sql.SQLException; 

    import javax.swing.JFrame; 
    import javax.swing.JPanel; 

    public class Mysql { 

    public static void main(String[] args) { 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     int w = screenSize.width; 
     JFrame frame = new MainFrame(w/5, 100, 2*w/5, 0); 
     frame.show(); 
     try { 
      new Connection().getConnection(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


} 
class MainFrame extends JFrame { 
    MainFrame(int width, int height, int x, int y) { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setTitle("Todothelydoo - To do planner"); 
     setSize(width, height); 
     setLocation(x, y); 
     JPanel mainPanel = new JPanel(); 
     mainPanel.setBackground(Color.blue); 
    } 
} 

Connection.java

import java.sql.DriverManager; 
import java.sql.SQLException; 

public class Connection { 
    public static java.sql.Connection getConnection() throws SQLException { 
     java.sql.Connection conn = null; 
     try 
     { 
      Class.forName("com.mysql.jdbc.Driver"); 
      String url = "jdbc:mysql://localhost/database_name"; 
      String user = "root"; 
      String password = ""; 
      conn = DriverManager.getConnection(url, user, password); 
      System.out.println("Connection established"); 
     } 
     catch(ClassNotFoundException e) 
     { 
      System.out.println(e.getMessage()); 
      System.exit(0); 
     } 
     catch(SQLException e) 
     { 
      System.out.println(e.getMessage()); 
      System.exit(0); 
     } 
     return conn; 
    } 
} 
+0

該代碼似乎工作,但每次我運行它的控制檯只是說:com.mysql.jdbc.Driver 我安裝了它。爲什麼這件事說這個? – user2773749 2014-10-16 20:40:35

+0

我已經告訴過你在你的項目構建路徑中包含一個JAR文件。請下載並在您的項目中包含'mysql-connector-java-5.1.23-bin.jar'文件。你可以在這裏找到它。 [鏈接](http://www.java2s.com/Code/JarDownload/mysql/mysql-connector-java-5.1.23-bin.jar.zip) – 2014-10-17 12:15:48

0

公共類Mysql的擴展Connectionn {

public static void main(String[] args) { 
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
    int w = screenSize.width; 
    JFrame frame = new MainFrame(w/5, 100, 2*w/5, 0); 
    frame.show(); 
    Connectionn con=new Connectionn(); 
    try { 
     con.getConnection(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

}

+0

公共類Connectionn {0} {0} public void getConnection()throws SQLException {連接conn = null; 嘗試 { Class.forName(「com.mysql.jdbc.Driver」); String url =「jdbc:mysql:// hostname:port/dbname」; String user =「username」; String password =「password」; conn = DriverManager.getConnection(url,user,password); System.out.println(「建立連接」); } 趕上(ClassNotFoundException的E) { } 趕上(的SQLException E) { 的System.out.println(e.getMessage()); 系統。出口(0); } } } – 2014-10-16 21:49:49

+0

1先創建Mysql類並擴展Conection類,在Mysql類中創建連接類對象。調用連接類方法(getConnection())和mysql-connector-java-5.1.14-bin.jar添加到您的項目(Build Path)庫中。然後嘗試它會起作用 – 2014-10-16 21:55:57