2016-08-03 6 views
0

Java jdk1.7xxxがインストールされており、MySQLデータベースv5.7もあります。 私のデータベースの名前は "mydb"です。 私はそれをmysqlコンソールからアクセスします。JAVA 1.7とMySQL 5.7の接続

しかし、自分のJavaプログラムからアクセスしようとすると、「ソースが不明です」または「パイロット名が指定されていません」というエラーが発生します。だから私はパイロット/余分なファイルがないと思う。しかし、私は、インポートjava.sqlのをしました*私のプログラムの冒頭で..

import javax.swing.*; 
import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.event.*; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.*; 
import java.sql.*; 
import java.util.*; 


public class DPIA_impacts extends JFrame { 
    // Labels 
    JLabel lb_title; 
    JTable tableau = null; 
    static JPanel p_global, p_title, p_center, p_south; 
    JScrollPane scroll=null; 
    JButton btn_Save, btn_Export; 
    //DefaultTableModel model; 
ArrayList t1=new ArrayList(); 
ArrayList t2=new ArrayList(); 
ArrayList t3=new ArrayList(); 


public DPIA_impacts(){ 
    //declarations 
    p_global = new JPanel(); 
    p_title = new JPanel(); 
    p_center = new JPanel(); 
    p_south = new JPanel(); 
    lb_title = new JLabel("DPIA meeting: fill the table !"); 
    btn_Save = new JButton("Save"); 
    btn_Export = new JButton("Export"); 
    scroll = new JScrollPane(); 
    // add tool tip text to the Save and Export buttons 
    btn_Save.setToolTipText("Save to the database Impacts table"); 
    btn_Export.setToolTipText("Exports the contents of the Jtable to an Excel file"); 
    // add Action Listener to the buttons 
    btn_Save.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e){ 
      //Execute when button is pressed 
      System.out.println("You clicked the button SAVE"); 
      String url = "jdbc:mysql://localhost/mydb"; 
      String login = "root"; 
      String passwd = "toor"; 
      Connection cn = null; 
      Statement st = null; 

      try { 
       //chargement du driver 
       Class.forName("com.mysql.jdbc.Driver"); 
       // Recup connexion 
       cn = DriverManager.getConnection(url, login, passwd); 
       // Creation d'un statement 
       st = cn.createStatement(); 
       String sql = "SELECT * FROM impacts"; 
       st.executeUpdate(sql); 

       //instruction.executeQuery(req); 
      } // Try 
      catch (SQLException ex) 
      { 
       System.out.println("Connexion à la base de données impossible"); 
       ex.printStackTrace(); 
      } 
      catch(ClassNotFoundException ex) 
      { 
       System.out.println("Pilote de connexion introuvable"); 
       ex.printStackTrace(); 
      //}//end catch 
      } finally { 
       try{ 
        cn.close(); 
        st.close(); 
       } catch (SQLException sqle){ 
        sqle.printStackTrace(); 
       } 
      } 
    }}); 
    btn_Export.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e){ 
      //Execute when button is pressed 
      System.out.println("You clicked the button EXPORT"); 
     } 
    }); 
    //add the components to the righ panels 
    p_title.add(lb_title); 
    p_center.add(scroll);  
    p_south.add(btn_Save, BorderLayout.WEST); 
    p_south.add(btn_Export, BorderLayout.EAST); 

    String req = "SELECT * FROM impacts"; 
    int i =0; 

    Connect resultat = new Connect(req,1,"BaseDeDonnees"); 

    // connexion a la base de donnees 
}//end constructor 

public static void main(String args[]) { 
    //Create and set up the window. 
    DPIA_impacts f = new DPIA_impacts(); 
    f.setTitle("DPIA: check the impacts"); 
    //f = new JFrame("DPIA: Impacts"); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //Set up the content pane 

f.getContentPane().setLayout(new BorderLayout()); 
f.getContentPane().add(p_title, BorderLayout.PAGE_START); 
f.getContentPane().add(p_center, BorderLayout.CENTER); 
f.getContentPane().add(p_south, BorderLayout.PAGE_END); 
f.pack(); 
f.setSize(500, 300); 
f.setVisible(true); 
}//end main 

}//end programm ! 

screenshot Error

jarファイルを追加:。

add the jar doesn't solve the issue

+0

完全なエラーログを追加しますか? @tom – SkyWalker

+1

@SkyWalker:done、screenshot added – tom

+0

テキストが表示されるときにスクリーンショットを使用しないでください(コマンドラインからテキストをコピーすることもできます)。 –

答えて

0

を私は String url = "jdbc:mysql://localhost/mydb";にあなたが忘れてしまったと思いますポート番号を書き込む。

[OK]を、ちょうどこのコードを試してみてくださいとEclipseからそれを実行していないCMDからUPDATE String url ="jdbc:mysql://127.0.0.1:3306/mydb";

してみてください。 また、にはexecuteUpdateを使用します。 SELECTの場合はexecuteQueryを使用します。

//Execute when button is pressed 
      System.out.println("You clicked the button SAVE"); 
      String url = "jdbc:mysql://127.0.0.1:3306/mydb"; 
      String login = "root"; 
      String passwd = "toor"; 
      Connection cn = null; 
      Statement st = null; 
      ResultSet rs = null; 
      System.out.println("Connecting to database.."); 
      try { 

       cn = DriverManager.getConnection(url, login, passwd); 
       System.out.println("Database Connected"); 
       st = cn.createStatement(); 
       String sql = "SELECT * FROM impacts"; 
       rs = st.executeQuery(sql); 
       while (rs.next()){ 
        //do something 
       } 


       //instruction.executeQuery(req); 
      } // Try 
      catch (SQLException ex) 
      { 
       System.out.println("Connexion à la base de données impossible"); 
       ex.printStackTrace(); 
      } 
      finally { 
       try{ 
        if (cn != null){ 
         cn.close(); 
         }    
         if (st != null){ 
          st.close(); 
          } 
         if (rs != null){ 
          rs.close(); 
          } 
       } catch (SQLException sqle){ 
        sqle.printStackTrace(); 
       } 
      } 
+0

Hello Ioanna、残念ながら、以前とまったく同じエラーが発生しました – tom

+0

mysql jdbcドライバjarをクラスパスに追加しましたか? –

+0

こんにちはIoanna、私は瓶を追加しようとしました、スクリーンショットを参照してください、動作しません。多分私はそれを正しくしません... – tom