-4

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();
}

}

Sachin Singh
  • 739
  • 4
  • 12
  • 29

2 Answers2

0

Hard to understand your question but I think you can find the answer you are looking for here: How to load resource from jar file packaged in a war file?

Community
  • 1
  • 1
barsju
  • 4,408
  • 1
  • 19
  • 24
0

In the webcontent directory? Really? This way the enduser would be able to open the file by URL. Don't expose this information in public. Rather put it in the classpath. Also rather make it a normal .properties file so that you don't need to go the hard way by parsing the file yourself.

E.g. db.properties:

driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/dbname
username = foo
password = bar

Then you can load and get data as follows:

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties"));
String driver = properties.getProperty("driver");
String url = properties.getProperty(url);
String username = properties.getProperty(username);
String password = properties.getProperty(password);
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555