wood burning stoves 2.0*
The moose likes Java in General and the fly likes Declaring a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Declaring a variable" Watch "Declaring a variable" New topic
Author

Declaring a variable

Sim Raina
Ranch Hand

Joined: Nov 20, 2003
Posts: 33
hi
i am doing this sql program like where i have to connnect it to the database to get information, i am done with my whole project, ia m testing and debugging now since i am using 27 servlets i wanna declare once and read them ocver and obver again , i know this is easy but can't seem to figure it out
for exi am declaring this in every servlets and other files to connect
i wanna do it so i delcare as a global variable and call them again and again
public static final String FINAL_URL= "jdbc:mysql://localhost:3306/poject/";
public static final String FINAL_CLASSNAME = "com.mysql.jdbc.Driver";
any suggestions;
thanks anyways
later


if u can't impress people with ur intelligence , confuse them with ur bullshit
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8710
    
    6

Originally posted by Sim Raina:

public static final String FINAL_URL= "jdbc:mysql://localhost:3306/poject/";
public static final String FINAL_CLASSNAME = "com.mysql.jdbc.Driver";

If you declare a variable public and static, it is accessible by any other class. If your declaration above appeared in Class1, Class2 could access them by saying:


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Sim Raina
Ranch Hand

Joined: Nov 20, 2003
Posts: 33
i already tried it . it doesn't work for some reason
it can't find the file , do i have to import something?
i don remember
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8710
    
    6

Originally posted by Sim Raina:
it doesn't work for some reason

What doesn't work? Does it not compile? What error message do you get?
Sim Raina
Ranch Hand

Joined: Nov 20, 2003
Posts: 33
it doesn't compile this is the part of error i am getting
---------- Compile ----------
C:\mysql\data\project\readfile.java:55: cannot resolve symbol
symbol : variable Testing
location: class readfile
FileReader fFile = new FileReader(Testing.FINAL_SERVER_FILE_NAME);
^
C:\mysql\data\project\readfile.java:88: cannot resolve symbol
symbol : variable Testing
location: class readfile
Class.forName(Testing.FINAL_CLASSNAME).newInstance();
^
C:\mysql\data\project\readfile.java:89: cannot resolve symbol
symbol : variable FINAL_URL
location: class readfile
String p_url = Testing.FINAL_URL;
this is the TESTING CLASS
public class Testing
{
public static final String FINAL_URL="jdbc:mysql://localhost:3306/project/";
public static final String FINAL_CLASSNAME ="com.mysql.jdbc.Driver";
public static final String FINAL_SERVER_FILE_NAME = "Proxy.txt";
public static final String FINAL_MONTH_FILE_NAME = "month.txt";
public static final String FINAL_TEST_FILE_NAME = "test.txt";
}

do u have any idea what is going on
Sim Raina
Ranch Hand

Joined: Nov 20, 2003
Posts: 33
it can' read the file
both of the files are at the same location
but don know what am i missing
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8710
    
    6

The compiler looks in the classpath for a class file matching Testing.class, then looks in the classpath for a source file matching Testing.java. My guess is you don't have the current directory, ".", in the classpath.
Sim Raina
Ranch Hand

Joined: Nov 20, 2003
Posts: 33
thanks bud i changed the classpath , it is working fine
thanks dude
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
To go a little advanced on you ... duplicated code is a large flashing sign to stop and think a while. How about if you made a class who's whole job was to handle this:

How nice to have all that tricky connect code in one place instead of 27. But now maybe we duplicate this in every servlet:

How about we move that code someplace we can reuse it.

This is the principle of "never do any work you can get someone else to do." Here's a short excerpt:

"Excuse me Smithers. I need to know the total bills that have been paid so far this quarter. No, don’t trouble yourself. If you’ll just lend me the key to your filing cabinet I’ll go through the records myself. I’m not that familiar with your filing system, but how complicated can it be? I’ll try not to make too much of a mess."
Smithers actually understands his filing system, so he can probably do the work faster than we can, and he’s much less likely to mess everything up. In seeking to do his job for him, we’re just making things worse. They’ll get a lot worse when he switches over to that new filing system next week. We’d be far better off with the stereotypical tyrant boss.
"SMITHERS! I need the total bills that have been paid since the beginning of the quarter. No, I’m not interested in the petty details of your filing system. I want that total, and I’ll expect it on my desk within the next half millisecond."

I hope that got some useful thoughts going!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaring a variable
 
Similar Threads
access to non-static variables
Switch Case constants
interface question
why a transient variable can be declared with static modifier??
Interface variables