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 {

        Class.forName(“com.mysql.jdbc.Driver”);
        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)+”‘)”;

     stmt.executeUpdate(sql);

    
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

31 thoughts on “Java Database Connectivity : Inserting Data”

  1. @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

  2. @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…

  3. 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".

  4. 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..

  5. @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!

  6. @dani add the following code :
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");

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

  7. 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")");

  8. 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

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

  10. 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 *