Wednesday 11 December 2013

jdbc program for reading the content from the note pad

/*prepare properties text file having DB username, pwd
 user=root.
 pwd=admin
 D:\jdbc\prog\wed\Properties\props.txt
 Note: in the above file the keys user, password are fixed names.
 */
import java.sql.*;
import java.sql.DriverManager;
import java.util.*;
import java.io.*;
public class SelectTest {
public static void main(String args[]) throws Exception
{
FileInputStream fis=new FileInputStream("D:\\jdbc\\prog\\wed\\Properties\\props.txt");
Properties p=new Properties();
p.load(fis);
System.out.println("data in 'p' object is"+p.toString());
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin");
if(con==null)
System.out.println("Connection not established");
else
System.out.println("connection established");

}


}
/*
the above code is very useful to pass DB username and password from outside the application when DBA
is changing the DB username and passwords at regular intervals for security reasons
*/

No comments:

Post a Comment