• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Single JDBC class for multiple Java Applications

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have simple classpath problem use this to compile

javac -classpath your_path_of_single_fodler DB.java

Shailesh
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


alternatively you can add same path in you environment classpath


Shailesh
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to put it at the root.
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its glad to hear that your app is now working perfectly.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use javac -d . *.java
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic