You are on page 1of 1

/**

*
*/
package sample;
import java.sql.*;
import java.util.Scanner;

/**
* @author SHEEBA
*
*/
public class DBDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// Step 1: Register the driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Step 2: Create connection
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@MBTLT39:1521:XE", "smartuser",
"oracle123");
//Step 3: Create Statement
Statement st = con.createStatement();
//Get the values from the user
Scanner sc = new Scanner(System.in);
int eid = sc.nextInt();
String ename = sc.next();
String place = sc.next();
//Step 4: Execute the query
// Inserting a record in the table
String qry = "insert into employee
values("+eid+",'"+ename+"','"+place+"')";
st.executeUpdate(qry);
//View the record
qry = "select * from employee";
ResultSet res = st.executeQuery(qry);
while(res.next()) {
System.out.println(res.getInt("eid")+"
"+res.getString("ename")+" "+res.getString("place"));
}
//Step 5: Close the connection
con.close();
}catch(Exception exc) {
System.out.println("Error: "+exc);
}
}

You might also like