0% found this document useful (0 votes)
25 views2 pages

Java Database Insert Example

The document is a Java program that connects to an Oracle database to insert student records. It uses JDBC to establish a connection and PreparedStatement to execute insert operations for three students. The program handles exceptions and confirms the successful insertion of records before closing the connection.

Uploaded by

doddamaniasmita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Java Database Insert Example

The document is a Java program that connects to an Oracle database to insert student records. It uses JDBC to establish a connection and PreparedStatement to execute insert operations for three students. The program handles exceptions and confirms the successful insertion of records before closing the connection.

Uploaded by

doddamaniasmita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

package databaseconnectivity;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

public class InsertEx {

public static void main(String args[]) {

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con = DriverManager.getConnection(

"jdbc:oracle:thin:@localhost:1521:XE", "system", "1234");

PreparedStatement stmt = con.prepareStatement("insert into student values (?, ?)");

stmt.setInt(1, 101);

stmt.setString(2, "Asmita");

stmt.executeUpdate();

stmt.setInt(1, 102);

stmt.setString(2, "Vaishnavi");

stmt.executeUpdate();

stmt.setInt(1, 103);

stmt.setString(2, "Krishvi");
stmt.executeUpdate();

System.out.println("Records inserted!");

con.close();

} catch (Exception e) {

System.out.println(e);

You might also like