txt file,where I have called all DB setting as Database name,Server name,username,pwd, and this file is in web content directory -config.txt, but it is unable to read from java class files.
public Statement myStatement() throws Exception {
File file=new File("DBSetting.txt");
FileInputStream fis = null; // declares a file input stream
//BufferedInputStream bis = null;
DataInputStream dis = null;
BufferedReader buffread=null;
try{
fis = new FileInputStream(file); // file inputstream opens file
dis = new DataInputStream(fis); // data input stream extracts data from file input stream
buffread=new BufferedReader(new BufferedReader(new InputStreamReader(dis))); // bufferedreader reads data from data input
//dis = new DataInputStream(bis);
connection=removeSpaces(buffread.readLine()); // reads data from file into variables
port=removeSpaces(buffread.readLine());
database=removeSpaces(buffread.readLine());
username=removeSpaces(buffread.readLine());
password=removeSpaces(buffread.readLine());
System.out.println("DB Settings -:Server Name and SQL Port - " +connection+ ", Port - "+port+", DB Name - "+database+", UserName - "+username+" , PWD-"+password);
//System.out.println(ay[ay.length-1]);
//System.out.println(connection);
dis.close();
}
catch(Exception ex){System.out.println(ex);}
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://"+connection + ":" + port + "/"+database;
System.out.println(url);
return DriverManager.getConnection(url, username ,password).createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
}
public static String removeSpaces(String s) { // removes white spaces present in strings
StringTokenizer st = new StringTokenizer(s," ",false);
String t="";
while (st.hasMoreElements()) t += st.nextElement();
return t;
}
public static void main(String args[])throws Exception
{
ConnectionManager con = new ConnectionManager();
con.myStatement();
}
}