JDBC INSERT Statement

0
6

In this tutorial you will learn about JDBC INSERT statement.

Subject: JDBC INSERT statement

Inserting data into a SQL database table is a simple two-step process:

First we need to create a Java Statement object.

Post creation of Java Statement object we need to execute a SQL INSERT command through the JDBC Statement object.

If anyone is aware of basic SQL queries, this is very easy to learn.

How to execute the JDBC INSERT statement

Here’s an example of how to create a Statement object, and then insert a record for a person named Mr. Rahul, of a town named Bangalore:

  1. // create a Statement from the connection
  2.  
  3. Statement statement = conn.createStatement ();
  4.  
  5. // insert the data
  6.  
  7. statement.executeUpdate ("INSERT INTO Customers "+ "VALUES (1001, ‘Rahul’, 'Mr.', ‘Bangalore’, 2001)");
  8.  
  9. JDBC Insert Example with Java Program:
  10.  
  11. /**
  12.  
  13. <span style="color: #1f497d; font-size: medium;"><span style="color: #1f497d; font-size: medium;"> * JdbcInsertExample.java </span></span><span style="color: #1f497d; font-family: Calibri; font-size: medium;"><span style="color: #1f497d; font-family: Calibri; font-size: medium;"><span style="color: #1f497d; font-family: Calibri; font-size: medium;">–</span></span></span><span style="color: #1f497d; font-size: medium;"><span style="color: #1f497d; font-size: medium;"> basic program about how to INSERT data into an SQL</span></span>
  14.  
  15. * Database using Java JDBC.
  16.  
  17. */
  18.  
  19. import java.sql.*;
  20.  
  21. class JdbcInsertExample {
  22.  
  23. public static void main (String [] args) {
  24.  
  25. try {
  26.  
  27. String url = "jdbc:msql://200.210.220.1:1114/Demo";
  28.  
  29. Connection conn = DriverManager.getConnection (url,"","");
  30.  
  31. Statement st = conn.createStatement ();
  32.  
  33. st.executeUpdate ("INSERT INTO Customers" +
  34.  
  35. "VALUES (1001, 'Rahul', 'Mr.', ‘Bangalore’, 2001)");
  36.  
  37. conn.close ();
  38.  
  39. }
  40.  
  41. catch (Exception e) {
  42.  
  43. System.err.println ("Got an exception! ");
  44.  
  45. System.err.println (e.getMessage ());
  46.  
  47. }
  48.  
  49. }

 

 

Readers might read also:

108 – Insert Statement Using Visual Basic Application for Managing Payroll System Module

Facebook Comments
(Visited 8 times, 2 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here