tom tom - 4 months ago 26
Java Question

JAVA 1.7 and Mysql 5.7 connection

I have Java jdk1.7xxx installed and a MySQL database v5.7 too.
My database is named "mydb".
I access it from the mysql console.

However when I try to access it from my java program it makes an error "source of data unknown, or pilot name not specified". So I am missing some pilot/extra file I guess. But I did import java.sql.* at the beginning of my program..

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

Adding the jar:

add the jar doesn't solve the issue

Answer

I think that in String url = "jdbc:mysql://localhost/mydb"; you forgot to write the port number. try String url ="jdbc:mysql://127.0.0.1:3306/mydb";

UPDATE

Ok, just try this code and run it from Eclipse and not from cmd. Also you use executeUpdate for INSERT. For SELECT you use 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();
               }
           }