title: Add data into MySQL Database using Eclipse
body: <p>I tried to make a simple programming that allows student's data to be added, updated, deleted and viewed for my assignments. I'm already stuck at adding part. What is the correct syntax /code to use in order to insert data into the database?</p>
  <p>I'm using Eclipse IDE software and XAMPP for this programming.</p>
  <pre><code>import java.awt.*;
  import javax.swing.*; 
  import java.awt.event.*;
  import java.sql.*;
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  
  public class PracticalExercise2 extends JFrame implements ActionListener{
  
      Connection      con;
      Statement       stmt;
      JLabel          lblname, lblmatricnumber, lblproject,lbltajuk;
      JTextField      txtname, txtmatricnumber , txtproject;
      JButton         btntest,btnadd,btnupdate,btndelete,btnview;
      String          user,pass;
      ResultSet       rs;
      JTabbedPane     myTab;
      JPanel          pnladd,pnlupdate,pnldelete,pnlview;
      
      public PracticalExercise2() {
          setLayout(null);
          setSize(400,400);
          setTitle(&quot;Registration Form using JTabbedPane&quot;);
          setVisible(true);
          setLocationRelativeTo(null);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          //GUI SETTING FOR FORM
          lbltajuk = new JLabel(&quot;Registration Form&quot;);
          lbltajuk.setBounds(10, 20, 350, 50);
          lbltajuk.setFont(new Font(&quot;Arial&quot;,Font.BOLD,32));
          lbltajuk.setForeground(Color.BLUE);
          add(lbltajuk);
          
          //JTabbedPane is also a panel
          myTab = new JTabbedPane();
          myTab.setBounds(20,70,350,200);
          add(myTab);
          
          pnladd      = new JPanel();
          pnlupdate   = new JPanel();
          pnldelete   = new JPanel();
          pnlview     = new JPanel();
          
          //Create and add Tab
          myTab.addTab(&quot;Add Data&quot;, pnladd);
          myTab.addTab(&quot;Update Data&quot;, pnlupdate);
          myTab.addTab(&quot;Delete Data&quot;, pnldelete);
          myTab.addTab(&quot;View Data&quot;, pnlview);
          
          btntest = new JButton(&quot;Test Connection&quot;);
          btntest.setBounds(120, 300, 150, 30);
          add(btntest);
          
          btntest.addActionListener(this);
          
          add();
      }
      
      
      public static void main(String[] args) {
          
          new PracticalExercise2();
      }
  
      void add()
      {
          pnladd.setLayout(null);
          
          lblname = new JLabel(&quot;Nama :&quot;);
          lblname.setBounds(20,20,100,30);
          pnladd.add(lblname);
          
          lblmatricnumber = new JLabel(&quot;No Matrik :&quot;);
          lblmatricnumber.setBounds(20,60,100,30);
          pnladd.add(lblmatricnumber);
          
          lblproject = new JLabel(&quot;Projek :&quot;);
          lblproject.setBounds(20,100,100,30);
          pnladd.add(lblproject);
          
          txtname = new JTextField();
          txtname.setBounds(110,20,200,30);
          pnladd.add(txtname);
          
          txtmatricnumber = new JTextField();
          txtmatricnumber.setBounds(110,60,200,30);
          pnladd.add(txtmatricnumber);
          
          txtproject = new JTextField();
          txtproject.setBounds(110,100,200,30);
          pnladd.add(txtproject);
          
          btnadd = new JButton(&quot;Add Data&quot;);
          btnadd.setBounds(120, 140, 100, 30);
          pnladd.add(btnadd);
              
          btnadd.addActionListener(this);
      }
      
      public void actionPerformed(ActionEvent e) {
          
          if (e.getSource() == btntest)
          {
              // 1. load driver
              
              try
              {
                  Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
      
              }
              catch (ClassNotFoundException x)
              {
                  JOptionPane.showMessageDialog(null, &quot;mySQL Driver cannot connect.&quot;);
              }
              
              // 2. create/open connection
              try {
                  
                  //testing the connection to database.
                  con = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/practicaltask2&quot;, &quot;root&quot;, &quot;&quot;);    
                  
                  JOptionPane.showMessageDialog(null, &quot;No connection error.&quot;);
                  
                  
              } catch (SQLException y) {
                  
                  JOptionPane.showMessageDialog(null, &quot;Connection error.&quot;);
              }
                  
          }
          
          if(e.getSource() == btnadd)
          {
              String name , matric , project;
              name    = txtname.getText();
              matric  = txtmatricnumber.getText();
              project = txtproject.getText();
              
              try {
                  PreparedStatement stmnt = con.prepareStatement(&quot;insert into pelajar(NULL,nama,nomatrik,projek)values(NULL,?,?,?)&quot;, Statement.RETURN_GENERATED_KEYS);
                  ((PreparedStatement) stmt).setString(2,name);
                  ((PreparedStatement) stmt).setString(3,matric);
                  ((PreparedStatement) stmt).setString(4,project);
                  
                  
                  JOptionPane.showMessageDialog(null, &quot;Record Added Successfuly&quot;);
                  
                  txtname.setText(&quot;&quot;);
                  txtmatricnumber.setText(&quot;&quot;);
                  txtproject.setText(&quot;&quot;);
                  
              
              }
              catch (Exception a)
              {
                  JOptionPane.showMessageDialog(null, &quot;Failed to add data&quot;);
              }
          }
  
      }
  }
  </code></pre>