A Alqtn

Greenhorn
+ Follow
since Jan 02, 2006
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by A Alqtn

Does Eclipse use different compiler other than Sun's JDK 1.5?

As far as I know Eclipse provides different graphics package (SWT) which is different than Swing.

If Integer i = 'a'; works in one implementation as you are reporting in the case of Eclipse and does not work on Sun's implementation then one of them must have a bug.

Alqtn
18 years ago
Hi everyone,

Java 5 introduced the concept of autoboxing which is helpful in some cases. However, if you try to do the following it won't work

Float f = 5;

Does any one know why? In the primitive counterpart example float f = 5; is legally perfect while the autoboxing version is not!!!

Is it a bug or what?

Alqtn
18 years ago
Hello All,

I have posted this https://coderanch.com/t/303446/JDBC/java/Creating-JAR topic on the JDBC forum but could not get any help.

I have created a simple Java application (ReadingAccessDB) that reads table rows from MS Access database. The program packages the java class file in jdbc folder (package jdbc; ), and the database file is one level up.

here is the code:



For example if the class file is in C:\jdbc\ReadingAccessDB.class, the database file "carpets.mdb" is in C:\

when using JVM "C:\java jdbc.ReadingAccessDB", the application works fine. However, I want to archive the application with the database file in a jar file so that it can be executed from any location. Could some one help

Alqtn

[ January 26, 2006: Message edited by: A Alqtn ]

[ January 26, 2006: Message edited by: A Alqtn ]
[ January 26, 2006: Message edited by: A Alqtn ]
18 years ago
Hello Everyone,

I have been working with a simple application that reads a table rows from MS Access database. The code is shown below:


import java.io.File;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;

public class ReadingAccessDB {
public static void main(String[] args) {
Connection conn = null;

try {
// 1) Load the driver
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);


// 2) Obtain a connection to the contacts database
File file = new File("carpets.mdb");
String databaseFile = file.getAbsolutePath();
System.out.println("database path " + databaseFile);
String url = "jdbc dbc river={Microsoft Access Driver (*.mdb)};DBQ=" + databaseFile;
conn = DriverManager.getConnection(url, "", "");

// 3) Create a JDBC statement for executing SQL expressions
Statement statement = conn.createStatement();

// 4) Execute an SQL expression and save the result in a ResultSet
ResultSet result = statement.executeQuery("select * from members");

// 5) Maniplute the result
System.out.println("Got Results:");
System.out.println("Member ID\tFirst Name\tLast Name");
System.out.println("---------\t----------\t---------");
while (result.next()) {
int memberID = result.getInt("memberID");
String firstName = result.getString("firstName");
String lastName = result.getString("lastName");
System.out.println(memberID + "\t" + firstName + "\t" + lastName);
}
}
catch (ClassNotFoundException ex) {
System.out.println("Failed to load MS Access driver");
}
catch (SQLException ex) {
System.out.println("SQL Exception: " + ex.getMessage());
ex.printStackTrace();
}
finally {
// 6) Close the connection
if( conn != null ) {
try {
conn.close();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
}


The above code packages the class file (ReadingAccessDB.class) in jdbc folder. now when I run this file, I usually move the "carpets.mdb" file one level above the jdbc folder. for example if the class file is in C:\jdbc\ReadingAccessDB.class, I put the "carpets.mdb" in C:\

When running the class file I use "java jdbc.ReadingAccessDB" and it runs fine. My problem is now how can archive this application with its database file and make it executable so that when wherever I move the JAR file it should run.

Alqtn

Originally posted by Ernest Friedman-Hill:


The initial is OK, but the last name is not a pronounceable word.

Anyway, as to your question: getters should return the most useful form of the information, which in this case is the price as a number (but more about that below...) Returning the price as a formatted string is inconvenient for any code that needs to add a list of prices, for example. Formatting for presentation is up to some other class.

About toString(): unless a class is documented otherwise, you can assume that toString() returns something useful for debugging/development only. It's fine, though, to make it return anything you want, as long as you document what it returns.

Now, finally, about using a float to represent money: don't do it. floats are not exact, and in a real application, at least, people tend to be very picky about monetary calculations being exact. Even being off by a penny is unacceptable (did you see the movie "Office Space?") Instead, you should use either an int to hold the number of pennies, a BigDecimal, or (best of all, in many cases) a Money class, either one you write or an existing one you obtain from a third party.



-------------------------------------------------

Thanks Enrest for the valuable information. Regarding my last name it is not English that is why it is not pronouceable.
18 years ago
Jesper, the naming policy says "You can even use initials for the first name if you like." So what is wrong with the name I am using?
18 years ago
So does this apply to the toSting( ) method? Should toString( ) return plain data without formatting or the story is different here?
18 years ago

Originally posted by Ilja Preuss:


I think there are better solutions to this, such as the Builder pattern, or a Collecting Parameter. Code doesn't *have* to be complex, it's always a choice we make.



---------------------------

The whole point I am trying to reach is when you design an application that reads values from the user (through a form or whatever means you can design), the validity checking of those values should be made at which level? Should they be checked at the driver class level and then when everything is OK call the setters with proper data? In this case I think you might not need to have any validity checking in the setter methods, since they receive valid data any way. Remeber you have developed everythinh.


However, if you want to rely on the setter method to reject invalid data, then I believe exception mechanism is a good choice.

My question again, which approach is used most and why?
18 years ago
Hello Everybody,

Back to basics. A getter method return a value for an instance variable. For example the following getter method returns a price

public float getPrice( ) {
return price;
}

Now if I wish this get method to return a formated price, is it suitable here to have the getter method return the formated value or it is better to do the formating at the place where the price has been received. As an example let us say that I want to format the price to UK locale

public String getPrice( ) {
Locale ukLocale = new Locale("en", "uk");
NumberFormat nfmt = NumberFormat.getCurrencyInstance( );

return nfmt.format( price );
}

This getter method is returning the price (which is actually of a float type) as a formated String. Is this good here or not? I am looking for best practices here (not it depends on the application answer).
18 years ago

Originally posted by Layne Lund:
IllegalArgumentException is a runtime exception. Generally, runtime exceptions indicate a programmer error and not a system error. This means that such an exception should NOT occur in a production environment. Persumably, the exception is thrown to facilitate testing so that such programming errors can be easily found and fixed.

I think in most cases this would be an acceptable solution as long as the whole system is thoroughly tested to ensure that such exceptions are not thrown after the program has been distributed for production. I also agree with Ilja that the way to handle this type of situation might not be entirely universal and depends on the exact situation. Don't get stuck into one solution so much that you are not flexible enough to modify it when there are reasons to do so.

Layne




Layne,

When reading a value from a text field, a runtime error might happen and it is not a programmer error. Such a exception can happen a production environment. What I mean is that not all run time errors represent progrmmaer error.

Alqtn
18 years ago
Michael,

So you suggest throwing an exception for setters. What about if we make it a standard. Meaning all classes developed use exception handling instead of the C-Style as you mentioned. When you say exception handling is expensive? In what factors might this approach affect the application.

Alqtn

Originally posted by Michael Duffy:


Now clients don't have to check a value to see if the set succeeds, and they'll be told if the operation fails.

Exceptions can be expensive, and there's an argument that says they shouldn't be used for normal program flow. But you've documented the proper use of your class in javadocs, so any user who wants to use your class will be fully-informed as to its proper use. I think it's a better alternative.

18 years ago
Here is an example:

public class A {
private int bonus;


public boolean setBonus(int bonus) {
if( bonus > 0 ) {
this.bonus = bonus;
return true;
}

return false;
}
}


public class B {
public static void main(String [ ] args) {
A a = new A();

int bonus = read from the user the bonus value;

if( a.setBonus( bonus ) == false )
System.out.println("invalid bonus value);
}
}
18 years ago
I am talking in general. Many others might use different approaches to this, but what I want to know which is better in general to depend on exceptional hadling or return a value from the method to indicate its success or fail.

If you use a simple return value from a setters then you have to use if statements for the setters. This is anoying at the driver level.

Alqtn.
18 years ago
Hello everyone,

I want to know how to inform the user a setter method about the success or failure of setting a value for an instance variable. For example:

public boolean setFace(int face) {
if( face >= 0 ) {
this.face = face;
return true;
}

return false;
}

is it better to use the boolean return type for the above setter method or it is better to use a throws clause?

Thanks

Alqtn
18 years ago