Skip to main content

JDBC FAQ Part6

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;
}
      }
}
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());
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);
con.close();
   }
}

Comments

Popular posts from this blog

దోసకాయ పూర్ణం | cucumber curry

How to merge Objects in Javascript

 let person = {     firstName: 'Rocky',     lastName: 'g',     age: 23 }; let job = {     jobTitle: 'Angular Developer',     location: 'UK' }; let employee = {     ...person,     ...job }; console.log(employee);

క్యారెట్ ఊరగాయ | carrot pickle