db configuration file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 49
Reputation: Ankita B is an unknown quantity at this point 
Solved Threads: 0
Ankita B's Avatar
Ankita B Ankita B is offline Offline
Light Poster

db configuration file

 
0
  #1
Feb 18th, 2008
Hi,I wish to make a configuration file for db connection parameters.
I read about properties file.I have made a properties file that contains the following info---

String jdbcDriver="oracle.jdbc.driver.OracleDriver";
String connectionURLThin="jdbc:oracle:thin hostname:AIPUAT1";
String DbUserId="abc";
String DbUserpassword="abc";

and saved it as DbConnection.properties.

i have also made a java file that uses these properties as---

import java.sql.DriverManager;
import java.sql.Connection;
import java.util.Properties;
import java.io.*;


public class DbConfiguration{

String jdbcDriver="oracle.jdbc.driver.OracleDriver";
String connectionURLThin="jdbc:oracle:thin 10.192.4.215:1521:AIPUAT1";
String DbUserId="trn1";
String DbUserpassword="trn1";

public void classForName() {

String jdbcDriver;
String connectionURLThin;
String DbUserId;
String DbUserpassword;
Connection con = null;

Properties prop=new Properties();



try {

props.load(new FileInputStream("dbConnection.properties"));
jdbcDriver=props.getProperty("jdbcDriver");
System.out.println(jdbcDriver);


connectionURLThin=props.getProperty("connectionURLThin");
System.out.println(connectionURLThin);


DbUserId=props.getProperty("DbUserId");
System.out.println(DbUserId);


DbUserpassword=props.getProperty("DbUserpassword");
System.out.println(DbUserpassword);


Class.forName(jdbcDriver);

con=DriverManager.getConnection(connectionURLThin,DbUserId,DbUserpassword);

}

catch(Exception e){
e.printStackTrace();
}

}

public static void main(String[] args)
throws java.lang.InterruptedException {

DbConfiguration dbc = new DbConfiguration();

dbc.classForName();
}

}

i am developing a web based application using java and jsp.I have placed my files in webapps folder.Where should i put properties file?.. wt else needs tp be put in the java code?..and how should i lonk this so that other java files can make use of properties(even after being changed).
Last edited by Ankita B; Feb 18th, 2008 at 5:53 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: db configuration file

 
0
  #2
Feb 18th, 2008
Put it wherever you want, as long as you say in your code where is it so you can read it:
props.load(new FileInputStream("dbConnection.properties"));
When you make changes in your .properties files, you must restart the server
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: Ankita B is an unknown quantity at this point 
Solved Threads: 0
Ankita B's Avatar
Ankita B Ankita B is offline Offline
Light Poster

Re: db configuration file

 
0
  #3
Feb 18th, 2008
ok thx..i put it directle in C: drive but when i give
props.load(new FileInputStream("C:\dbConnection.properties"));

im gettin compile time error --illegal escape character '\'...
Last edited by Ankita B; Feb 18th, 2008 at 6:24 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: db configuration file

 
0
  #4
Feb 18th, 2008
Use: double '\': '\\'
or single '/'
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: Ankita B is an unknown quantity at this point 
Solved Threads: 0
Ankita B's Avatar
Ankita B Ankita B is offline Offline
Light Poster

Re: db configuration file

 
0
  #5
Feb 18th, 2008
yup got it!..thx..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: Ankita B is an unknown quantity at this point 
Solved Threads: 0
Ankita B's Avatar
Ankita B Ankita B is offline Offline
Light Poster

Re: db configuration file

 
0
  #6
Feb 19th, 2008
i used this properties file in my old java file.it takes value from properties file but is giving classNotFoundException..However when i use same driver ,url etc directly in the java class itself and not thru prop,it runs perfectly fine.i have set classpath etc too..what cud be wrong ere?..
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: db configuration file

 
0
  #7
Feb 19th, 2008
maybe your application is not expecting to find what you want it to find
mistakes are common you know, is pretty hard though, to try and figure out what the problem is, without having a look at the code that produces the actual Exception
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: Ankita B is an unknown quantity at this point 
Solved Threads: 0
Ankita B's Avatar
Ankita B Ankita B is offline Offline
Light Poster

Re: db configuration file

 
1
  #8
Feb 20th, 2008
i found the error..had put driver etc names in the properties file under quotes..which is not required..
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1377 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC