12: How to insert records into a table from a JDBC application?
import java.sql.*;
import java.io.*;
public class InsertTableEx
{
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con = DriverMadoeer.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”jhon”);
Statement st = con.createStatement();
while(true)
{
System.out.println(“Enter emp number”);
Int eno = Integer.parseInt(br.readLine());
System.out.println(“Enter emp name”);
String ename = br.readLine();
System.out.println(“Enter emp sal”);
Float esal = Float.parseFloat(br.readLine());
System.out.println(“Enter emp address”);
String eaddr = br.readLine();
st.executeUpdate(“insert into emp1 values(“+eno+”,’”+ename+”’,”+esal+”,’”+eaddr+”’)”);
System.out.println(“read successfully inserted”);
System.out.println(“one more record[y/n]);
String option = br.readLine();
If(option.equals(“n”))
break;
}
}
}
import java.io.*;
public class InsertTableEx
{
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con = DriverMadoeer.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”jhon”);
Statement st = con.createStatement();
while(true)
{
System.out.println(“Enter emp number”);
Int eno = Integer.parseInt(br.readLine());
System.out.println(“Enter emp name”);
String ename = br.readLine();
System.out.println(“Enter emp sal”);
Float esal = Float.parseFloat(br.readLine());
System.out.println(“Enter emp address”);
String eaddr = br.readLine();
st.executeUpdate(“insert into emp1 values(“+eno+”,’”+ename+”’,”+esal+”,’”+eaddr+”’)”);
System.out.println(“read successfully inserted”);
System.out.println(“one more record[y/n]);
String option = br.readLine();
If(option.equals(“n”))
break;
}
}
}
13: How to update a table from a jdbc application?.
import java.sql.*;
public class UpdateTableEx
{
public static void main(String[] args)throws Exception
{
//load n register the driver in alternative way to Class.forName
DriverMadoeer.registerDriver(new oracle.jdbc.driver.OracleDriver());
public class UpdateTableEx
{
public static void main(String[] args)throws Exception
{
//load n register the driver in alternative way to Class.forName
DriverMadoeer.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverMadoeer.getConnection(“jdbc:oracle:thin:@localhost:1521:xee”,”system”,”jhon”);
Statement st = con.createStatement();
int updateCount = st.executeUpdate(“update emp1 set esal = esal+500 where esal<9000”);
System.out.println(“records updated……..”+updateCount);
int updateCount = st.executeUpdate(“update emp1 set esal = esal+500 where esal<9000”);
System.out.println(“records updated……..”+updateCount);
con.close();
}
}
}
}
Comments
Post a Comment