• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

File Access object  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class which contains a method that performs file access. In this method I call the DriverManager.getConnection() method.
My problem is, I won't always be able to provide the username and password that getConnection() requires.
Is there a way to perform file access without having to do this?
Thanks in advance.
Kevin

 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin,
I'm not sure I follow. DriverManager.getConnection() is use for JDBC. How is that connected with your file access?
Can you give me a small psuedo-code of what you need? I may be able to help you after I see your psuedo-code or if you can clarify your requirements.
Thanks,
-Peter
 
Kevin Adams
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Tran:
Kevin,
I'm not sure I follow. DriverManager.getConnection() is use for JDBC. How is that connected with your file access?
Can you give me a small psuedo-code of what you need? I may be able to help you after I see your psuedo-code or if you can clarify your requirements.
Thanks,
-Peter


Sure, here ya go...
Let's say I have a file that contains codes and their descriptions, like so:
AP ACCOUNTS PAYABLE
IC INVENTORY CONTROL
I want to connect to a database and retrieve 1 row from the file (by doing this: SELECT DESC FROM FILE WHERE CODE = "IC")
Usually what I would've done would be to do the following:

Since this piece of code will always be instantiated by other objects, which have already been authenticated, I don't want to have to give a username and password to DriverManager.getConnection().
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Kevin,
Sorry it took me so long to get back to this post. I got pulled into some other discussions, and I also have to do real work. Unfortunately, my employer doesn't pay me to stay on JavaRanch all day. I wish I was independently wealthy so I can hang out here all day.
Back to your problem.
I would suggest trying one of following techniques.
1. Use connection pooling.
2. Use a singleton pattarn to wrap your Connection object.
-Peter
[This message has been edited by Peter Tran (edited January 05, 2001).]
 
Kevin Adams
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool.
I'm not sure what all that means just yet, but I'll certainly take a look into it. Thanks, Peter, for all your help.
Kevin
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
A singleton pattern makes sure that there's only one instance of an object. There's a couple of ways to create one and a great book on design patterns can be found at:
James W. Cooper's: Java Design Patterns, A Tutorial at http://www.patterndepot.com/put/8/JavaPatterns.htm
Also, being able to read values from a file gives you several advantages, the most obvious is not having to recompile your code if you want to change any of the values.
Here's some code I made for a project:
<code>
import java.util.*;
import java.io.*;
public class STANDARDS {
static String path = "";
private static final String [] propKeys = {"SERVLET",
"DNS",
"FROM_NAME",
"FROM_EMAIL",
"SMTP_HOST",
"JDBC_DRIVER",
"JDBC_URL"};
private static String filepath = "";
private static HashMap ht = new HashMap(propKeys.length);
private STANDARDS () {
File f = null;
Properties options = new Properties();
File currentDir = new File(System.getProperty("user.dir"));
f = new File(currentDir,"prop.ini");
path = f.getAbsolutePath();
try {
options.load(new FileInputStream(f));
} catch (IOException e) {
System.out.println(e.toString());
}
for(int i = 0; i < propKeys.length; i++){
ht.put(propKeys[i],options.getProperty(propKeys[i]));
}
}
private static STANDARDS instance = new STANDARDS();
static public STANDARDS getInstance(){
if (instance == null) {
instance = new STANDARDS();
}
return instance;
}
static public String getSERVLET(){
return (String) ht.get("SERVLET");
}
static public String getDNS(){
return (String) ht.get("DNS");
}
static public String getHOME(){
return ((String) ht.get("DNS")) + ((String) ht.get("SERVLET"));
}
static public String getFROM_NAME(){
return (String) ht.get("FROM_NAME");
}
static public String getFROM_EMAIL(){
return (String) ht.get("FROM_EMAIL");
}
static public String getSMTP_HOST(){
return (String) ht.get("SMTP_HOST");
}
static public String getJDBC_DRIVER(){
return (String) ht.get("JDBC_DRIVER");
}
static public String getJDBC_URL(){
return (String) ht.get("JDBC_URL");
}
static public String getPath(){
return path;
}
static public void setFilePath(String path) {
filepath = path;
}
}
</code>
After this you would create a poolmanager, again using a singleton pattern (in the same way as STANDARDS) that would call this class (STANDARDS) with the appropriate method call. Now, everytime you want a connection then would get an instance of your poolmanager and call a getConnection method.
Hope this helps.
cheers,
Yoo-Jin.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!