Wednesday , January 18 2017
Home / jdbc / JDBC Insert Program Example

JDBC Insert Program Example

In this tutorials, we are going to understand JDBC insert program. In the previous tutorial we have implemented JDBC select Program example, it is recommended to have a look if you don’t know.

To do the insert operation in JDBC, the api given us executeUpdate(String qry) and execute(String qry) methods. We can apply the both executeUpdate() aand execute() methods on statement object.

execueteUpdate() Example :

executepdate() method returns the integer value. The value represents that the number of rows effected in the database.

int rowsEffected = stmt.executepUpdate(“insert command”);

execuete() Example :

execute() method returns boolean value. As we already discussed in the JDBC select example, we can use the execute() method for both select and non-select operations. If the resultant object contains ResultSet then the execute() method returns the true, it returns false if it is an update count or no records found.

boolean isResultSet = stmt.executep(“insert command”);

JDBC Insert Program Example :


package com.onlinetutorialspoint.jdbc; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.Scanner; 
 
public class Jdbc_InsertionOperation_Example { 
 
    public static void main(String[] args) throws Exception { 
 
        Connection connection = null; 
        Statement statement = null; 
        Scanner scanner = null; 
 
        try { 
            int studentNo = 0; 
            String studentName = null; 
            String studentAddress = null; 
            int studentAge = 0; 
            scanner = new Scanner(System.in); 
            if (scanner != null) { 
                System.out.println("Enter Student No"); 
                studentNo = scanner.nextInt(); 
                System.out.println("Enter Student Name"); 
                studentName = scanner.next(); 
                System.out.println("Enter Student Address"); 
                studentAddress = scanner.next(); 
                System.out.println("Enter Student Age"); 
                studentAge = scanner.nextInt(); 
            } 
             
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/onlinetutorialspoint", "root", "systemuser23!"); 
            if (connection != null) { 
                statement = connection.createStatement(); 
                String insertQuery = "insert into student values('" + studentNo + "','" + studentName +"','"+ studentAddress + "','"+studentAge+"')"; 
                int result = statement.executeUpdate(insertQuery); 
 
                if (result == 0) { 
                    System.out.println("Record Inserted Failed"); 
                } else { 
                    System.out.println(result+ " Record(s) Inserted "); 
                } 
            } 
 
        } catch (ClassNotFoundException cnfe) { 
 
            cnfe.printStackTrace(); 
        } catch (SQLException sqe) { 
            sqe.printStackTrace(); 
 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            try { 
                if (statement != null) { 
                    statement.close(); 
                } 
            } catch (SQLException sqe) { 
                sqe.printStackTrace(); 
 
            } 
 
            try { 
                if (connection != null) { 
                    connection.close(); 
                } 
            } catch (SQLException sqe) { 
                sqe.printStackTrace(); 
 
            } 
 
        } 
 
    } 
} 

Output :

Enter Student No
2003
Enter Student Name
chandrashekhar
Enter Student Address
hyderabad
Enter Student Age
30
1 Record(s) Inserted

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

ResultSetMetaData in JDBC Example

In this tutorial, we are going to learn about ResultSetMetaData in JDBC. The typical definition …

Leave a Reply

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