Java Database Connectivity : Inserting Data

After viewing the data of a database in GUI, now we will learn how to insert the data through GUI in database.


Database Connectivity(JDBC)

1 Follow the 1-5 steps of Viewing Data in GUI.
   Note: It is not necessary to insert some rows in the table in the 2nd step.

2 In the design view,insert text-fields according to your database and a submit button.

3 Under your package and before class starts write the following codes in the source tab.
  import java.sql.*; 
  import javax.swing.JOptionPane;
4 Write the coding on the button’s actionPerformed event for the connectivity.

   try {

        Connection con= (Connection) DriverManager.getConnection(“jdbc:mysql://localhost:3306/database1“,”root”,””);
        Statement stmt=con.createStatement();
   catch(Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
5 Now, in the try block below the line Statement stmt=con.createStatement(); , write the code for taking input from the user through the text-fields you created earlier and for executing your sql query.

         String no = jTextField1.getText();

     String title = jTextField2.getText();
     String auth = jTextField3.getText();
     String sql=”Insert into library values(‘”+(no)+”‘,'”+(title)+”‘,'”+(auth)+”‘)”;


Note: In the brackets first symbol is a single apostrophe ( ) and then a double inverted comma ( ). Same will be followed before and after each variable.
7 Change the codes in blue according to your database.
8 Run the file(Shift + F6) and you are ready to enter the data through your GUI.
Run View

41 thoughts on “Java Database Connectivity : Inserting Data”

  1. Hi rohan, brother let me thank you for all the support you are providing to us. I really appreciate your work. Iam working on my project connected to a database, now my problem is how to deploy it after am done so that it i will contain all the resources including the database for the user to install. Thanks in advance bro.
    my email

  2. @itpioneer2008
    if u will run ur project on a system where MySQL is already installed, then you can run all sql queries(including the creation of database) as soon as the app runs i.e windowOpened property
    but if u are spreading ur app to all, then u have to setup a database server over the internet and then the app can connect to it through TCP/IP.

    Also, create a runnable file for ur project otherwise u will have to install a java IDE first to run the app.
    For this, u can read – How to make JAR files using NetBeans IDE

  3. hi rohan,nice tutorial. after following all the above steps,i get an error message in the dialog box:
    Communication link failure
    the last packet sent to the server was 0 milliseconds ago. The driver has not received any packets for the server.

    what could be the problem? please reply ASAP…thanks!

  4. @Anonymous It often occurs on Win7, Go to start and type 'MySQL Server Instance Config Wizard' and reconfigure your MySQL settings and try it again it will surely work…

  5. reconfigured it…but still doesnt work 🙁
    did a standard reconfig. but shows the same error! anything else that i can try out?

  6. hello,i did this an got an error..
    "You have an error in your SQL Syntax.Check the manual that corresponds to your MYSQL server version for the right syntax to use near "","","", at line1

  7. Anonymous said…

    reconfigured it…but still doesnt work 🙁
    did a standard reconfig. but shows the same error! anything else that i can try out?

    Check "Install as a windows service".

  8. Anonymous said…
    "You have an error in your SQL Syntax.Check the manual that corresponds to your MYSQL server version for the right syntax to use near "","","", at line1

    You have made a mistake in step 5.
    Check that you are not passing empty values.

    In future, please comment as Name/URL..

  9. nah! wont run! already installed as windows service. also tried out the example that you have specified here! still the same error! 🙁

  10. @Aditya

    1 Check, you imported the correct library.
    2 Check, if you are able to open MySQL command-line client.
    3 Ensure you chose 3306 as port number while configuring MySQL.
    4 Disable your anti-virus and try again!

  11. Thanx a lot for this awesome site..
    Problem :
    I tried the same with my form with many textfields but when i pressed submmit button
    error is "com.mysql.jdbc.Driver"

    Please HELP
    i am in an urgent need.

    using NetBeans 6.5
    My SQL 5.0.13
    my email id is

    Please help.
    Thanx in advance.

  12. @dani add the following code :

    Below this line
    String sql="Insert into library values('"+(no)+"','"+(title)+"','"+(auth)+"')";

  13. hi rohan…
    i m making a project using netbeans IDE 7.1.2, database: MS Access.
    I face the problem in inserting data from the form to database table.. pls give some solution to it


  14. I have problem during below Statement

    st.executeUpdate("Insert into test_sst_player_master (ss_player_code, ss_player_name, ss_national_code, ss_sport_code, ss_fav_rating, sst_rating, ss_club1, ss_club2, ss_is_team) values('id','"+n1+"','"+c1+"','"+s1+"','"+fn1+"','"+rn1+"','"+cl1+"','"+cl2+"',"N")");

  15. i need your help to complete my project.
    i'm currently working on a project in java and mysql as the backend.
    as a newbie to java am having some difficulty so i'll need your assistance to help complete the project

  16. hiii. when you used :String no = jTextField1.getText(); ..
    The attribute no is the one you used in your database?? Thanks

  17. nice tutorial !!!! how do you update a table that refers the foreign key of a parent table and its rows?

Leave a Comment

Your email address will not be published. Required fields are marked *