
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class Emp{
public static void main(String[] args) {
System.out.println("Getting Employee Details Using resultset metadata: ");
Connection con = null;
String url = "jdbc:oracle:thin:@172.24.137.30:1521:ORA10G";;
String driver = "oracle.jdbc.driver.OracleDriver";
String user = "e206227";
String pass = "VeuFmodax";
try{
Class.forName(driver);
con = DriverManager.getConnection(url, user, pass);
try{
Emp e=new Emp();
Scanner sc=new Scanner(System.in);
int ch=sc.nextInt();
switch(ch)
{
case 1: e.viewDetails(con);
break;
case 2: e.addDetails(con);
break;
}
}
catch (Exception s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
public void viewDetails(Connection con)
{
try{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM employee");
while(rs.next())
{
System.out.print("\t"+rs.getString(1));
System.out.print("\t"+rs.getString(2));
System.out.print("\t"+rs.getString(3));
System.out.print("\t"+rs.getString(4));
System.out.println("\t"+rs.getString(5));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void addDetails(Connection con)
{
try
{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("insert into employee values('emp005','rakesh',23000,7418767778,'js.rakesh@
xyz.com')");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.