JDBC

JDBC Projects

JDBC Project 1

JDBC Using PreparedStatement example Insert a record
Previous Home Next
/* This program shows you how to insert the data in 
the database table using JDBC PreparedStatement.*/

package r4r;
import java.sql.*;
import java.util.Scanner;
public class InserData 
{
public static void main(String[] args) 
{
try
{
Scanner s=new Scanner(System.in);
System.out.println("Enter your name:-");
String s1=s.nextLine();
System.out.println("Enter the id:-");
int i=s.nextInt();
s.nextLine();
System.out.println("Enter the city:-");
String c=s.nextLine();
System.out.println("Enter sex:-");
String se=s.nextLine();
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement stmt=con.prepareStatement("insert
into user5 values(?,?,?,?)");
stmt.setString(1, s1);
stmt.setInt(2, i);
stmt.setString(3, c);
stmt.setString(4, se);
stmt.executeQuery();
System.out.println("successfully inserted");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

 

output:

Enter your name:-
tarun
Enter the id:-
12
Enter the city:-
Goa
Enter sex:-
m
successfully inserted
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: