The java utility program is used to Read from a properties file,make DB connections,read from a file and Write into a file.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
import java.io.*;
public class Utility {
public static String dbUserName;
public static String dbPassword;
public static String dbDriver;
public static String dbDriver;
public static String IP;
public static String sid;
public static String port;
public static void readProperties(String path) throws Exception
{
File f = new File(path);
InputStream fin = new FileInputStream(f);
Properties prop = new Properties();
prop.load(fin);
dbUserName = prop.getProperty("username");
dbPassword = prop.getProperty("password");
dbDriver = prop.getProperty("dbDriver");
IP = prop.getProperty("IP");
sid = prop.getProperty("sid");
port = prop.getProperty("port");
}
public static boolean connectToDB() {
if (con != null) {
return true;
}
try {
Connection con = null;
Class.forName(driver);
String connString = "jdbc:oracle:thin:@" + IP.trim() + ":"
+ port.trim() + ":" + sid.trim();
System.out.println("Connecting to the Database.....");
con = DriverManager.getConnection(connString, username, password);
if (con == null) {
System.out.println("Unable to get a connection");
return false;
} else {
System.out.println("Connected to DB....");
return true;
}
} catch (SQLException e) {
System.out.println("Unable to get DB connection............");
}
return false;
}
public static String readFile(String Path) throws IOException {
String currentLine = null;
String fileString = "";
FileReader fr = new FileReader(Path);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
fileString = fileString + currentLine;
}
br.close();
fr.close();
return fileString;
}
public static void writeFile(String path, String content) {
try {
char buffer[] = new char[content.length()];
content.getChars(0, content.length(), buffer, 0);
FileWriter fw = new FileWriter(path);
for (int i = 0; i < buffer.length; i++) {
fw.write(buffer[i]);
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}