Insert Record JDBC Keyboard Input

Insert Record JDBC Keyboard Input

To take record particulars from keyboard and insert them in the Employee table.

Note: See that there exists an Employee table by the time this program is executed. This can be done with Create Table.

Example on Insert Record JDBC Keyboard Input

stmt.executeUpdate(“insert into Employee values(“+id+ “, ‘”+name+”‘,”+salary+”)”);

Observe the single quotes and double quotes in the above statement. empname is varchar and requires single quotes. Always get the variable outside the string.

The above code works for one record. By keeping in a loop, multiple records can be inserted.

Note 1: Before going into the program, it is advised to go through JDBC Programming Basic Steps where the meaning of Connection, Statement etc. are discussed.

Note 2: To know how to use try-catch-finally with the above code, example program is available at JDBC Example – Create Table.

4 thoughts on “Insert Record JDBC Keyboard Input

  1. abhimanyu kumar

    sir,please tell me why did you use “+id+ “ .why you use (+) after and before id and (” “) between +id+ .i am confused about it. please tell me.

    1. S. Nageswara Rao, Corporate Trainer Post author

      id is a variable which should be brought out of string. Observe, the parameter of executeUpdate(String) is string. That is, the whole insert record is in string form. Just observe this:

      String str = “SNRao”;
      System.out.println(“My name is str”);

      or
      System.out.println(“My name is ” + str);

      Now decide which prints My name is SNRao. So to print variables like str, they must be brought outside the string. At runtime, the variable value is exchanged with the actual value and then + concatenates the real value with the remaining part of string.

  2. ipsita

    Hello;
    while i execute dis progrmme employee id takes data from keyboard,but employy name it simply printed Enter employee name:not askig for keyboard input..
    plz help me….

Leave a Reply

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