Domanda

I made a Java code that it inserts the data in the specific table, but when I ask the table to display the data, the added data by Java application are not visible.

class_1

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

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

public class IHM_2 extends JFrame
{
 static final long serialVersionUID = 1L;
 JFrame frame= new JFrame();
 JTextField text1 = new JTextField();
 JTextField text2=new JTextField();
 JTextField text3=new JTextField();
 JTextField text4=new JTextField();

 JPanel panel = new JPanel();

 JLabel lblIdentificateur = new JLabel("Identificateur:");
 JLabel lblLogin = new JLabel("Login:");
 JLabel lblPassword = new JLabel("Password:");
 JButton Ajouter = new JButton("Ajouter");
 JLabel lblProfil = new JLabel("Profil:");



 public static void main(String[] args) 
 {
  new IHM_2();
 }  


 public IHM_2() 
 {
    frame= new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel.setBorder(null);
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    panel.setLayout(null);


    lblLogin.setBounds(54, 108, 75, 14);
    panel.add(lblLogin);


    text2.setBounds(199, 105, 129, 20);
    panel.add(text2);
    text2.setColumns(10);

    lblPassword.setBounds(54, 146, 96, 14);
    panel.add(lblPassword);

    text3.setBounds(199, 143, 129, 20);
    panel.add(text3);
    text3.setColumns(10);



    Ajouter.addActionListener(new ActionListener() 
    {

        public void actionPerformed(ActionEvent e) 
        {

          JButton source= (JButton)e.getSource();
          if (( source==Ajouter )) 
          {
            DAO_2 dr=new DAO_2();
            dr.operation();
            frame.dispose(); 
          }
        }
    });     
    Ajouter.setBounds(264, 232, 89, 23);
    panel.add(Ajouter);
    frame.setVisible(true);
   }

 }

classe_2

public class Metier_2 
{
 String x2,x3;

 IHM_2 obj=new IHM_2();

 public Metier_2()
 {
  x2=obj.text2.getText();
  x3=obj.text3.getText();
 }
}  

class 3

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

public class DAO_2 
{

public DAO_2()
{

}

 public void operation()
 {
    Metier_2 er = new Metier_2();
    try 
    {  
     Class.forName("com.mysql.jdbc.Driver");
     System.out.println("Driver O.K.");
     String url = "jdbc:mysql://localhost:3306/kirly";
     String user = "root";
     String passwd = "*****";
     Connection conn = DriverManager.getConnection(url, user, passwd);
     String login=er.x2; 
     String password=er.x3;

      PreparedStatement ps = conn.prepareStatement("Insert into user_2(login,mdp) Values (?,?)");

      ps.setString(1,login);
      ps.setString(2,password);

      ps.executeUpdate();
      ps.close();
      conn.close();
    } 
   catch (Exception e) 
   {
    e.printStackTrace();
   }
   finally
    {
         System.out.println("Bloc Finally\n");
     }

  }
  }
È stato utile?

Soluzione

From a quick browse of your code I've spotted an issue that I think is likely to be causing your problem. Your JButton Ajouter from your IHM_2 instance created in main() is doing the following in it's ActionListener;

DAO_2 dr=new DAO_2();
dr.operation();

running dr.operation(); then creates a

Metier_2 er = new Metier_2();

which then uses the text values from the NEW IHM_2window to get the values to parse to the SQL query. The trouble is that er holds a reference to a completely new instance of a IHM_2 JFrame, and the fields will be empty. So each time you are running the query you are passing it empty strings. Hence why the values in your textFields from your original JFrame are never making it to the table.

For this to work, your Metier_2 object needs to hold a reference to the IHM_2 object created in your main() method, not a brand new one. Or better yet, just pass your IHM_2 instance directly to your new DAO_2 through it's constructor, and then you wouldn't even need the Metier_2 class at all.

I hope this helps. Let me know how you get on.

Altri suggerimenti

Without seeing the code and table in question, it's hard to give a definitive answer, but two possibilities leap to mind:

  1. You didn't use autocommit for the JDBC connection in Java, or you didn't explicitly commit the transaction.
  2. The program you're using to view the data is operating on a different connection than your Java program.

In both cases, it's really the same answer -- databases are designed to provide consistent views of the data, not the most up-to-date view of the data. If program1 opens a connection to the database and starts a long-running transaction, and program2 comes along and executes a fast 'connect, insert, commit, end connection' sequence, the results of that commit are unlikely to be seen by the first program, because it's transaction is already in progress. Once program1 completes its transaction, it will have access to the updates from program2.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top