import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc8
{
public static void main(String[] args)
{
try
{
// Loading & Register the Type-4 Driver Class(OracleDriver). But this line is optional
Class.forName("oracle.jdbc.OracleDriver");
}
catch(ClassNotFoundException cn)
{
cn.printStackTrace();
}
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String check=null; // to check user want to enter more data or not
do
{
System.out.print("Enter id : ");
int id=Integer.parseInt(br.readLine());
System.out.print("Enter Name : ");
String name=br.readLine();
System.out.print("Enter Salary : ");
float sal=Float.parseFloat(br.readLine());
System.out.print("Enter Designation");
String job=br.readLine();
int rc=st.executeUpdate("insert into emp values("+id+",'"+name+"',"+sal+",'"+job+"')");
System.out.println(rc+" row inserted");
System.out.println("Do you insert more? (yes/no)");
check=br.readLine();
}while(!check.equalsIgnoreCase("no"));
System.out.println("----------------End of Application -----------------");
}
catch(SQLException|IOException e)//Multi Catch Block supported in jdk1.7 or higher version
{
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc8
{
public static void main(String[] args)
{
try
{
// Loading & Register the Type-4 Driver Class(OracleDriver). But this line is optional
Class.forName("oracle.jdbc.OracleDriver");
}
catch(ClassNotFoundException cn)
{
cn.printStackTrace();
}
// Establishing the connection
try(Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","system","manager"))
{
// Creating statement to execute query
Statement st=con.createStatement();
// Executing Query
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String check=null; // to check user want to enter more data or not
do
{
System.out.print("Enter id : ");
int id=Integer.parseInt(br.readLine());
System.out.print("Enter Name : ");
String name=br.readLine();
System.out.print("Enter Salary : ");
float sal=Float.parseFloat(br.readLine());
System.out.print("Enter Designation");
String job=br.readLine();
int rc=st.executeUpdate("insert into emp values("+id+",'"+name+"',"+sal+",'"+job+"')");
System.out.println(rc+" row inserted");
System.out.println("Do you insert more? (yes/no)");
check=br.readLine();
}while(!check.equalsIgnoreCase("no"));
System.out.println("----------------End of Application -----------------");
}
catch(SQLException|IOException e)//Multi Catch Block supported in jdk1.7 or higher version
{
e.printStackTrace();
}
}
}
0 Comment to "Jdbc Program to insert data in 'emp' table in Oracle database using type-4 driver."
Post a Comment