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

Posted on 2010-01-08
Last Modified: 2013-12-14
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.
Question by:guavamay
    LVL 9

    Accepted Solution

    I think the problem is in these lines:


    Change them too:


    But it helps if you post the full exception stacktrace.

    Author Closing Comment

    Thanks for your help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    sumHeights2  challenge 7 51
    mergeTwo  challenge 13 53
    array6 challenfge 6 48
    Passing list of object to Oracle Database Procedure 3 30
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now