Link to home
Start Free TrialLog in
Avatar of sujatharathinam
sujatharathinam

asked on

How to read the jdbc url from a property file?

hi all,
I am doing a module in Swing. When I try to load the jdbc url from a property file, I got the following error. Could anybody guide me to rectify the problem?


Java(TM) Plug-in: Version 1.4.0
Using JRE version 1.4.0-beta3 Java HotSpot(TM) Client VM
User home directory = C:\WINDOWS

Proxy Configuration: Manual Configuration
     Proxy: http=192.168.1.28:80,https=192.168.1.28:80,ftp=192.168.1.28:80,gopher=192.168.1.28:80
     Proxy Overrides: 192.168.1.1,<local>





----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
p:   reload proxy configuration
q:   hide console
r:   reload policy configuration
s:   dump system properties
t:   dump thread list
x:   clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
An Exception occurred in Connection::getConnection()java.security.AccessControlException: access denied (java.io.FilePermission \usr\local\tomcat\webapps\newsagency\development\db.properties read)

java.lang.NullPointerException

     at au.com.redback.outlet.TableTest.getPayment(TableTest.java:91)

     at au.com.redback.outlet.JTableDemo.display(JTableDemo.java:83)

     at au.com.redback.outlet.JTableDemo.start(JTableDemo.java:73)

     at sun.applet.AppletPanel.run(AppletPanel.java:358)

     at java.lang.Thread.run(Thread.java:539)

Thanks in advance,

Sujatha
Avatar of skinsella
skinsella

post your code please...
Avatar of sujatharathinam

ASKER

hi skinsella,
this is my code from where i am loading the property file.

package au.com.redback.outlet;

import java.sql.*;
import java.io.*;
import java.util.*;

public class PoolManager
{
      static private PoolManager instance;
      static private int clients;

      private LogWriter logWriter;
      private PrintWriter pw;

      private Vector drivers = new Vector();
      private Hashtable pools = new Hashtable();


      private PoolManager() throws Exception
      {
            init();
      }


      public static synchronized PoolManager getInstance() throws Exception
      {
            if (instance == null) {
                  instance = new PoolManager();
            }
            clients++;
            return(instance);
      }


      private void init()throws Exception
      {
            // Log to System.err until we have read the logfile property
            pw = new PrintWriter(System.err, true);
            logWriter = new LogWriter("PoolManager", LogWriter.INFO, pw);
            File f = new File("/usr/local/tomcat/webapps/newsagency/development/db.properties");
                FileInputStream fis = new FileInputStream(f);
            Properties dbProps = new Properties();
            try {
                  //dbProps.load(is);
                          dbProps.load(fis);
            }
            catch (Exception e) {
                  logWriter.log("Can't read the properties file. " + "Make sure db.properties is in the CLASSPATH", LogWriter.ERROR);
                  return;
            }
            String logFile = dbProps.getProperty("logfile");
            if (logFile != null) {
                  try {
                        pw = new PrintWriter(new FileWriter(logFile, true), true);
                        logWriter.setPrintWriter(pw);
                  }
                  catch (IOException e) {
                        logWriter.log("Can't open the log file: " + logFile + ". Using System.err instead", LogWriter.ERROR);
                  }
            }
            loadDrivers(dbProps);
            createPools(dbProps);
      }

      private void loadDrivers(Properties props)
      {
            String driverClasses = props.getProperty("drivers");
            StringTokenizer st = new StringTokenizer(driverClasses);
            while (st.hasMoreElements()) {
                  String driverClassName = st.nextToken().trim();
                  try {
                        Driver driver = (Driver)
                        Class.forName(driverClassName).newInstance();
                        DriverManager.registerDriver(driver);
                        drivers.addElement(driver);
                        logWriter.log("Registered JDBC driver " + driverClassName, LogWriter.INFO);
                  }
                  catch (Exception e) {
                        logWriter.log(e, "Can't register JDBC driver: " + driverClassName, LogWriter.ERROR);
                  }
            }
      }

      private void createPools(Properties props) throws Exception
      {
         
            Enumeration propNames = props.propertyNames();
            while (propNames.hasMoreElements()) {
                  String name = (String) propNames.nextElement();
                  if (name.endsWith(".url")) {
                        String poolName = name.substring(0, name.lastIndexOf("."));
                        String url = props.getProperty(poolName + ".url");
                        if (url == null) {
                              logWriter.log("No URL specified for " + poolName, LogWriter.ERROR);
                              continue;
                        }
                        String user = props.getProperty(poolName + ".user");
                        String password = props.getProperty(poolName + ".password");
                        String maxConns = props.getProperty(poolName + ".maxconns", "0");
                        //maxConns="2";
                        maxConns = props.getProperty(poolName + ".maxconns");
                        int max;
                        try {
                              max = Integer.valueOf(maxConns).intValue();
                        }
                        catch (NumberFormatException e) {
                              logWriter.log("Invalid maxconns value " + maxConns + " for " + poolName, LogWriter.ERROR);
                              max = 0;
                        }
                        String initConns = props.getProperty(poolName + ".initconns", "0");
                        //initConns="20";

                        initConns = props.getProperty(poolName + ".initconns");
                        int init;
                        try {
                              init = Integer.valueOf(initConns).intValue();
                        }
                        catch (NumberFormatException e) {
                              logWriter.log("Invalid initconns value " + initConns + " for " + poolName, LogWriter.ERROR);
                              init = 0;
                        }
                        String loginTimeOut = props.getProperty(poolName + ".logintimeout", "5");
                        int timeOut;
                        try {
                              timeOut = Integer.valueOf(loginTimeOut).intValue();
                        }
                        catch (NumberFormatException e) {
                              logWriter.log("Invalid logintimeout value " + loginTimeOut + " for " + poolName, LogWriter.ERROR);
                              timeOut = 5;
                        }
                        String logLevelProp = props.getProperty(poolName + ".loglevel", String.valueOf(LogWriter.ERROR));
                        int logLevel = LogWriter.INFO;
                        if (logLevelProp.equalsIgnoreCase("none")) {
                              logLevel = LogWriter.NONE;
                        }
                        else if (logLevelProp.equalsIgnoreCase("error")) {
                              logLevel = LogWriter.ERROR;
                        }
                        else if (logLevelProp.equalsIgnoreCase("debug")) {
                              logLevel = LogWriter.DEBUG;
                        }
                        // System.out.println("Before calling connectionpool");
                        ConnectionPool pool = new ConnectionPool(poolName, url, user, password, max, init, timeOut, pw, logLevel);
                        pools.put(poolName, pool);
                  }
            }
      }

      public Connection getConnection(String name)
      {
            Connection conn = null;
            ConnectionPool pool = (ConnectionPool)pools.get(name);
            if (pool != null) {
                  try {
                        conn = pool.getConnection();
                  }
                  catch (SQLException e) {
                        logWriter.log(e, "Exception getting connection from " + name, LogWriter.ERROR);
                  }
            }
            return(conn);
      }


      public void freeConnection(String name,Connection con)
      {
            ConnectionPool pool = (ConnectionPool) pools.get(name);
            if (pool != null) {
                  pool.freeConnection(con);
            }
      }


      public synchronized void release()
      {
            // Wait until called by the last client
            if (--clients != 0) {
                  return;
            }
            Enumeration allPools = pools.elements();
            while (allPools.hasMoreElements()) {
                  ConnectionPool pool = (ConnectionPool) allPools.nextElement();
                  pool.release();
            }
            Enumeration allDrivers = drivers.elements();
            while (allDrivers.hasMoreElements()) {
                  Driver driver = (Driver) allDrivers.nextElement();
                  try {
                        DriverManager.deregisterDriver(driver);
                        logWriter.log("Deregistered JDBC driver " + driver.getClass().getName(), LogWriter.INFO);
                  }
                  catch (SQLException e) {
                        logWriter.log(e, "Couldn't deregister JDBC driver: " + driver.getClass().getName(), LogWriter.ERROR);
                  }
            }
      }
}
ASKER CERTIFIED SOLUTION
Avatar of skinsella
skinsella

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.20082413.html
https://www.experts-exchange.com/questions/Q.20105221.html
https://www.experts-exchange.com/questions/Q.20147107.html
https://www.experts-exchange.com/questions/Q.20148355.html
https://www.experts-exchange.com/questions/Q.20149649.html
https://www.experts-exchange.com/questions/Q.20152751.html
https://www.experts-exchange.com/questions/Q.20179768.html
https://www.experts-exchange.com/questions/Q.20191558.html
https://www.experts-exchange.com/questions/Q.20193219.html
https://www.experts-exchange.com/questions/Q.20193231.html
https://www.experts-exchange.com/questions/Q.20193232.html
https://www.experts-exchange.com/questions/Q.20244129.html
https://www.experts-exchange.com/questions/Q.20179283.html
https://www.experts-exchange.com/questions/Q.20270570.html
https://www.experts-exchange.com/questions/Q.20270623.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20150319.html
https://www.experts-exchange.com/questions/Q.20150322.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
sujatharathinam,
You have already been asked to clean up your opened questions.  I will give you 3 days to maintain these open questions before I ask administration to suspend your account.  I have provided easy links to your questions below.  You must take care of each opened question by the time I return. I will be posting this in each of your opened questions.  If you need help with deletions, post a commnet in your question if experts have replied indicating your desire to delete, then post a question in community support and list the questions: https://www.experts-exchange.com/Community_Support/

Experts, leave your recommendations for this question's disposition or I may simply delete this question without a refund.

SpideyMod
Community Support Moderator @Experts Exchange

DO NOT ACCEPT THIS COMMENT AS AN ANSWER.

Questions Asked
20
Last 10 Grades Given
A A B B A A B A A A
Question Grading Record
10 Answers Graded / 10 Answers Received

https://www.experts-exchange.com/questions/20282856/Shell-Scripting.html
https://www.experts-exchange.com/questions/20330448/Dumping-data-into-sql-file-from-database.html
https://www.experts-exchange.com/questions/20495382/Symmetric-Algorithm.html
https://www.experts-exchange.com/questions/20488726/Need-a-resolution-for-Reference-counting-error.html
https://www.experts-exchange.com/questions/20326321/Differences-between-JavaBeans-and-Enterprise-JavaBeans.html
https://www.experts-exchange.com/questions/20292542/Can-I-know-the-list-of-printers-in-the-network.html
https://www.experts-exchange.com/questions/20292539/Can-I-know-the-list-of-printers-in-the-network.html
https://www.experts-exchange.com/questions/20270570/How-to-read-the-jdbc-url-from-a-property-file.html
https://www.experts-exchange.com/questions/20150319/From-JTable-Swing.html
https://www.experts-exchange.com/questions/20179283/How-do-I-convey-a-message-to-the-client-browser-that-a-operation-is-in-progress.html
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange