Program to insert record into Database server using prepared Statement.

// Program to insert record into DB server using prepared Statement.

import java.sql.*;

class PreparedStatementExample
{
    public static void main(String[] args)throws SQLException
    {
        // registering the driver
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        // create connection object
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "chaitu");
        // Create statement object
        PreparedStatement pstmt = con.prepareStatement("insert into emp values(?,?,?,?)");
        // Giving values to the positional parameters
        pstmt.setInt(1,4);
        pstmt.setString(2,"rajesh");
        pstmt.setInt(3,40000);
        pstmt.setString(4,"chennai");
        int n = pstmt.executeUpdate();
        //close the connection
        con.close();
    }
}

No comments:

Post a Comment