Retrieving database information from properties file:

You must have afile info.property for the program
and inside that
driver="......"
url="......"
username="...."
password="......."
import java.sql.*;
import java.io.*;
import java.util.*;
class demo
{
public static void main(String[] args)throws Exception{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
try{
Properties p=new Properties();
FileInputStream fis=new FileInputStream("info.property");
p.load(fis);
Class.forName(p.getProperty("driver"));
con=DriverManager.getConnection(p.getProperty("url"),p.getProperty("username"),p.getProperty("password"));
stmt=con.createStatement();
rs=stmt.executeQuery("select *from emp");
while(rs.next())
System.out.println(rs.getString(2));
}catch(Exception e){
System.out.println("the exception is::::->"+e);
}
finally{
try{
rs.close();
stmt.close();
con.close();}
catch(Exception e){
System.out.println("2nd try block exception:::->"+e);
}}}};

No comments:

Post a Comment