This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Single JDBC class for multiple Java Applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Single JDBC class for multiple Java Applications" Watch "Single JDBC class for multiple Java Applications" New topic
Author

Single JDBC class for multiple Java Applications

Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Hello All,
Iam building a simple Java-JDBC program using MS Access Database. I have 4 tables and each will be accessed using different Java Application programs. I don't want to write JDBC code for each class I want to put the JDBC code in a separate class and call the JDBC connection code in Java programs. Can you please send me the code so that i can develop the logic. Your help is very much appreciated.

Thanks in advance,

regards,
Raghu
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081


please send me the code so that i can develop the logic

why dont you write your own code ? if you face any difficulty then many rachers are here.



I don't want to write JDBC code for each class I want to put the JDBC code in a separate class


Look for DAO Design Pattern

Shailesh
[ April 27, 2005: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Shailesh,
I dont mean that i dont want to write code. I am specifying my case that i have the idea to write JDBC connection code seperartely. I am stuck up with a code thats why iam asking a suggesstion.

Thanks,
Raghu
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by Raghuraman Muthuswamy:
I am stuck up with a code thats why iam asking a suggesstion


OK, good enough , put the code here and provide
details

so we can suggest our view


Shailesh
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
The following code DBInterface.java is what i did to create Database(JDBC) code:

package single;

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

public class DBInterface
{
private static DBInterface dbi = null;
private static Connection c;

DBInterface()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection("jdbc dbc:bank");
}
catch(Exception e)
{
System.out.println("Exception: "+e);
}
}

public static synchronized DBInterface getInstance()
{
if (dbi==null)
{
dbi = new DBInterface();
}
return dbi;
}

public Connection getConnection()
{
return c;
}

}

This is the code DB.java which uses the DBInterface.java for getting Database connection:

package single;

import java.sql.*;

public class DB
{
public void processDB()
{
try
{
DBInterface dbi = DBInterface.getInstance();
Connection c = dbi.getConnection();

Statement Stmt = c.createStatement();
ResultSet myResult = Stmt.executeQuery("select * from interestRate");

String itype="",irate="",tdate="";

while (myResult.next())
{
itype = myResult.getString("interesttype");
irate = myResult.getString("interestrate");
tdate = myResult.getString("transferdate");

}
myResult.close();
Stmt.close();
c.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}


DBInterface.java compiled successfully.
But when i compiled DB.java it says...

DB.java :11: cannot resolve symbol
Symbol: class DBInterface
Location: class single.DB

DB.java: 11: cannot resolve symbol
symbol: variable DBInterface
location: class single.DB

I have put both java files in the same package. I dunno why DB.java is not recognizing DBInterface.java. Kindly suggest me where iam wrong.

Thanks in advance,
Raghu
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

you have simple classpath problem use this to compile

javac -classpath your_path_of_single_fodler DB.java

Shailesh
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Sailesh,
I have stored both DB.java and DBInterface.java in
program files\apache group\tomcat 4.1\webapps\examples\web-inf\classes\single

so to compile DB.java do i need to use the path above. Can you provide me the exact syntax to compile the file DB.java. As i mentioned previously DBInterface.java is compiling successfully.

Thanks in advance,

Regards,
Raghu
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081



alternatively you can add same path in you environment classpath


Shailesh
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Now i have stored the files in c:\single directory. I compiled the file DBInterface.java its compiling perfectly. I tried to compile DB.java, it gave me the same error. Then i compiled it using
javac -classpath c:\single DB.java still it gives me the same error. Is it the correct way i compiled. Please tell me how to solve this issue.

Thanks
Raghu
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Sailesh,
Now both the files which are in C:\single directory. I compiled DB.java with javac -classpath c: DB.java now its compiling. But the original files which are stored in c:/program files.....it says invalid flag. I think its not taking the blank space after program in program files. Can you provide any suggestion on this.

Sorry for troubling you.

Regards,
Raghu
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Try to put it at the root.
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Shailesh and Adeel,
I am wondering about the fact that both files are in the same folder. Then why DB couldnt recognize the DBInterface. Can you explain me why the error was thrown.

Adeel, I cant get you. You mentioned put it at the root directory. How to do that.

Waiting for your reply.

regards,
Raghu
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Just put your files directly to your C drive. Then check, that the error is because of the space or something else.

Your .class files are also on the same location? I mean after compilation of interface you will have a .class file for that interface. Is it on the same location??
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Adeel,
Yes the class file for DBInterface.java is on the same location. I copied the files into C: its working but how can i compile the files in web-inf\classes directory. I want to compile the files here because iam gonna access these two files with JSP using Tom Cat. So i must compile the files from tomcat directory. I think the space between the name of directory is the concern. can you suggest me how to run the files in c:/program files/apache group/tomcat 4.1......classes/single.

Thanks in advance,

regards
Raghu
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

better do one thing if using windows 2000

right click on my computer --> go to properties

then click on advance tab -->> click on button environment variable

there you can see user variable and system variable

in user variable search for a variable classpath if you dont find create you new with same name and append\add this in value


start new command prompt and compile class

Shailesh
[ April 27, 2005: Message edited by: Shailesh Chandra ]
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
I copied the files into C: its working but how can i compile the files in web-inf\classes directory. I want to compile the files here because iam gonna access these two files with JSP using Tom Cat. So i must compile the files from tomcat directory. I think the space between the name of directory is the concern. can you suggest me how to run the files in c:/program files/apache group/tomcat 4.1......classes/single.


If I understand what you are saying, I believe you need to move your class files to the Tomcat location i.e.

and then either redeploy the application or restart Tomcat, to be on the safeside.

If you have correctly coded your JSP pages, it should see your class files. Also the TOMCAT container does not care about your individual CLASSPATH settings.

So, if you have already successfully compiled your class files, you don't need to compile them from the Tomcat directories.

If I may offer a suggestion. In my opinion, I always compile and edit my source code in a seperate directory structure(development area) and then package the final components and/or move the class files to the Tomcat directory for deployment.

I hope this helps.
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Shailesh and Adeel,
I have put all the .java files in a seperate folder and compiled the files. Then i pasted the class files in web-inf/classes directory. Now everything is working perfectly. Thanks for your valuble suggesstions. Keep up the good work. Meet you with next query soon.

Regards,
Raghu
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Originally posted by Craig Jackson:
If I may offer a suggestion. In my opinion, I always compile and edit my source code in a seperate directory structure(development area) and then package the final components and/or move the class files to the Tomcat directory for deployment.
[/QB]


Dear Craig,
I moved the files into a seperate directory and compiled files i have put in tomcat. Now its working perfectly. Thankyou for the suggesstion.

Regards,
Raghu
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Its glad to hear that your app is now working perfectly.
hema vintha
Greenhorn

Joined: Apr 04, 2005
Posts: 1
Originally posted by Raghuraman Muthuswamy:


Dear Craig,
I moved the files into a seperate directory and compiled files i have put in tomcat. Now its working perfectly. Thankyou for the suggesstion.

Regards,
Raghu


Hi Raghu,

I think its problem with packages(something like, if one class is accessing the other class in the same package,then such class should be compiled outside the package they both belong to)

I had the same problem earlier.If u compile in the following way it should work too

program files\apache group\tomcat 4.1\webapps\examples\WEB-INF\classes>javac single\DB.java


in my case i had files as bookstore\action\ConnectionPool.java
and bookstore\javabean\LoginServlet.java where LoginServlet.java uses ConnectionPool.java

it worked when i compiled LoginServlet.java as ..\WEB-INF\classes>javac bookstore\action\LoginServlet.java

-Hema
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
use javac -d . *.java
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Single JDBC class for multiple Java Applications
 
Similar Threads
code for JSP JDBC
sqlj connection problem
can't connect Mysql 5.2 server on '127.0.0.1' (10061)
what changes have to be made while migrating from 1.4 mysql jdbc to 1.5 jdbc
how to run servlets in internet