Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

How to code 'ADD' button to create new record in Java

I just started learning Java. I created a simple form using Java Swing on NetBeans. I added some text fileds and button like 'add', 'delete' etc. I am using MySql db. What I want to do is.. by clicking 'add' button the program should be able to create new records in customer table in MySql db.
From design tab, I clicked 'add' button and right click, select event, Actionperformed and wrote the following code.

Here is my code:
private void UpdateActionPerformed(java.awt.event.ActionEvent evt) {
         System.out.println("Adding new record");
         Connection con = null;
         String url = "jdbc:mysql://localhost:3306/";
         String db = "testdb";
         String driver = "com.mysql.jdbc.Driver";

         try {
             con = DriverManager.getConnection(url+db, "root","pw");
             PreparedStatement st=con.prepareStatement
               ("insert into customer (fNameValue,lNameValue) values(?;?)");
               (jPanel2, "Data is succesfully added");
         catch(Exception e) {
             JOptionPane.showMessageDialog(jPanel2,"Error in submitting");



    * @param args the command line arguments
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Customer().setVisible(true);
I am gettiing 'my exception message ... Error in submitting'

What am I doing wrong? Would it be possible to get a sample code on to create new rercords?

Thanks so much.
1 Solution
I think the problem is in these lines:


Change them too:


But it helps if you post the full exception stacktrace.
guavamayAuthor Commented:
Thanks for your help.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now