JDBC Program For Age Calculator
For Explanation Watch video ::
Code :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
class Test
{
private static final String query = " select (sysdate-dob)/365.25 from emp88 where eid = ?";
public static void main(String arg[]){
try(Scanner scn = new Scanner(System.in)){
//take eid from user
System.out.println("Enter Emp id : ");
int eid = scn.nextInt();
try(Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger");
PreparedStatement ps = con.prepareStatement(query);){
//set the eid
ps.setInt(1, eid);
//ResultSet Object
ResultSet rs = ps.executeQuery();
rs.next();
//get the age
float age = rs.getFloat(1);
System.out.println("Age Of the employee: "+age);
}
}catch(SQLException se) {
se.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment