Bob Young

Ranch Hand
+ Follow
since Dec 23, 2000
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 Bob Young

Thanks for replying Sainudheen Mydeen. I was able to get this going by using a preparedstatement, just as you suggested. I know that not all databases are able to use preparedstatement, in particular MySql. I was hoping that somebody could suggest a way to make this work without using a preparedstatement.
However I am about to become bald from pulling out my hair trying to understand this database stuff. Can somebody explain why the following is happening. I am using Access as the database.
This code from a book almost runs but leaves out the last row of data.

The data for insert values 123, name1 and 987, name2 are added to the database but 555, name3 is not! Now I would not mind this happening with my checking account on the debit side, but it would definitely not be good for the credit side. Now if I try much the same code with a servlet, it does not work at all.

Why oh why will this almost work as in the first code example and utterly fail on the second? The only real difference that I see is the servlet
I could use some help on the following. I am reading up on servlets and JDBC via Java Servlet Programming by Jason Hunter. I have looked at this so long I just can't see my error, but there must be something wrong! I am trying to makeup a simple servlet example using a html form to pick up a parameter from the user and pass it on to the servlet. The servlet should perform an update on the database. When I look at the database nothing is being added. The database is an Access DB and it is hooked up correctly. I can use another app to look at the database and perform a select * from Table1 and see that nothing is added. I am using Tomcat 4.0.1. I have tried doPost and doGet neither worked. I have tried text and int for the parameter. The database is one table called of all things Table1 with the first column SSNum as Number and the next three columns as Parameter1, Parameter2, Parameter3 all as text. Got any ideas? Thanks.


(Marilyn added code tags and disabled smilies)
[ September 29, 2003: Message edited by: Marilyn de Queiroz ]
Thanks Wayne for the reply. I did not get a chance to post an earlier reply that I solve the problem. All I needed to do was go in and remove the DNS for the access database and reenter it. I have no idea why it did not work the first time as the path was exactly the same. The data base is only in one location and the spelling is/was correct. I did play with it and you can get the error if you misspell the URL.
I am trying to run a book example from Beginning Java 2 on the JDBC chapter and I am having trouble getting the example to run. I am using the downloaded database and example. I set up the Data Source Name in the ODBC Microsoft Access setup (Access 2000, on win 98), and the classpath contains the rt.jar. The database is ok, I can open it and look at the tables. What
needs to be included or changed for me to get this to work? Thanks.

import java.sql.*;
public class TestTheConnection
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String sourceURL = "jdbc dbc:technical_library";
Driver theDriver = DriverManager.getDriver(sourceURL);
int verMajor = theDriver.getMajorVersion();
float verComplete = Float.parseFloat(verMajor + "" + theDriver.getMinorVersion());
System.out.println("sourceURL version: " + verComplete);

Connection databaseConnection =
DriverManager.getConnection(sourceURL);
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe);
}
catch(SQLException sqle)
{
System.out.println("SQLException: " + sqle.getMessage());
sqle.printStackTrace();

}
}
}
sourceURL version: 21.0
SQLException: [Microsoft][ODBC Microsoft Access Driver] '(unknown)' is not a valid path.
Make sure that the path name is spelled correctly and that you are connected to the server on
which the file resides.
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] '(unknown)' is not a valid path.
Make sure that the path name is spelled correctly and that you are connected to the server on which
the file resides.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6031)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6188)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:2458)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:320)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:163)
at java.sql.DriverManager.getConnection(DriverManager.java:517)
at java.sql.DriverManager.getConnection(DriverManager.java:199)
at TestTheConnection.main(TestTheConnection.java:22)
I buzzed out to the JLS looking for the answer. The only thing I found is the example that Alton Hernandez is showing. Based on the hint that you gave this must not be what you are looking for. I am afraid you have forgotten about the question, what is the answer?
Oliver, the code from Dan's site that you listed works just fine. The first line is assigning null to an int[] handle called i (Please be aware that this is an array). The next line takes i and assignes it to a Cloneable handle called c (all arrays implement Cloneable, so this is a valid upcast assignment). Then the c handle is cast back to the original int[]. The casting rule for an interface to array is that the newType must implement the oldType interface, which all arrays do, i.e. Cloneable. The wording of you error has me guessing that you cast with an int and not and int[], which is easy to do.
The floating point numbers have the following form:
s = sign
e = exponent
m = mantissa
The bit layout of a Java float is:
s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm(23 m's)
The mantissa of of a float which occupies only 23 bits, has 24 bits of presion. The most significant mantissa bit is predictable and not included. Now on to your question. The special values have these forms:
+ infinity: 0 11111111 00000000000000000000000
- infinity: 1 11111111 00000000000000000000000
NaN 0 11111111 10000000000000000000000
(please note that NaN has a one bit at the most significant possition)
Now that you have the bit representation you will be able to easily understand his question.
Is there a doctor in the house? I need my eyes checked! I wanted this question to be a polymorphism question so bad that my mind just made it one. Thanks for clearing the trail grim from my eyes.
Well pardners, I need some help with Dan's Chapter 6, exam 1, question 8.
class A {
void m1(A a) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
}
class D {
public static void main(String[] args) {
A c1 = new C();
A c2 = new C();
A c3 = new C();
C c4 = new C();
c1.m1(c4);
c2.m1(c4);
c3.m1(c4);
}
}
What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above

My thinking for the code line: c1.m1(c4);
I know that the true class is of type C and I need to match the method from it and not use the method from the reference type. I therefore answered that the code would print CCC. With anticipation of success, I headed out to verify that I got the correct answer. But Nooooooooo (insert sound clip for shootout at the OK coral) the answer is not CCC but AAA.
I went back to my notes and text books and all had examples of polymorphism similar to the following:
class A {
void m1() {System.out.print("A");}
}
class B extends A {
void m1() {System.out.print("B");}
}
class C extends B {
void m1() {System.out.print("C");}
}
class D {
public static void main(String[] args) {
A c1 = new C();
A c2 = new C();
A c3 = new C();
C c4 = new C();
c1.m1();
c2.m1();
c3.m1();
}
}
This is just Dan's example with each method taking no arguments. This compiles and will print CCC.

Now good citizens and sheriffs of Java Ranch (heck I will even take an answer from the villian Dan himself :-) ) I need some clarification on polymorphism. I looked at Dan's remarks for the question and I can not understand how it works (I've included his remarks below). Comparing the two examples, how is the "normal" example of polymorphism different than Dan's question?
I will sit at the bar drinking the unbearable swill they serve here and await your answers. Thanks.
Dan's remarks to question 8:
Class A has only one implementation of method m1. Class B overloads method m1 with a second implementation. Class C overloads method m1 with a third implementation. Even though c1, c2 and c3 are all instances of class C, the reference type is always A so the overload methods declared in the subclasses are not accessible. For that reason, the implementation of method m1 declared in class A is always invoked.
class Test {
static char c = '\u0041'; // unicode, prints A
// char c = 65 decimal, prints A
// char c = 0101 octal, prints A
// char c = '\101' ?, prints A
public static void main (String[] args) {
System.out.println(c);
}
}

I have not seen a character defined as '/101'. What is it? It acts like octal, but does not have the leading 0 and it is in single quote. If I use and octal number, I do not need the single quote. 0101 (octal) is 65 decimal.
I need some more help with this one. I have a serious case of flawed logic! Let me try to explain what is going on and somebody tell me where I have the wrong concept.
As I see it:
When i = 0, flow continues to loop #3, which eventually encounters the continue inner statement. At this point the program flow leaves loop #3 and flows to the inner label. When i = 1, flow eventually get to loop #3 again, however the for loop has started fresh(ie k = 0).
Now when i = 7, flow is interrupted in loop #2, via the continue outer statement. At this point the program flow leaves loop #2 and flows to the outer label. When program flow moves back through loop #2 the loop counter does not start at i = 0! This is where I am confused. The two do not seem to be behaving the same. In both cases the current loop has been interrupted to a label outside the current loop. Yet one loop (loop #3) gets a new counter while the other (loop #2) gets the next increment. Why? What do I have wrong?
This example is from Bruce Eckel, Thinking in Java p. 138.

The output is:
i= 0
k = 0
k = 1
k = 2
k = 3
continue inner
i= 1
k = 0
k = 1
k = 2
k = 3
continue inner
i= 2
continue
i= 3
break
i= 4
k = 0
k = 1
k = 2
k = 3
continue inner
i= 5
k = 0
k = 1
k = 2
k = 3
continue inner
i= 6
k = 0
k = 1
k = 2
k = 3
continue inner
i= 7
continue outer
i= 8
break outer
My confusion is why is there a difference in behavior between
loop #2 and loop #3 when a labeled continue is used?
After leaving loop #3 via the continue inner statement, the loop #3 always starts anew whenever it is revisited (as can be seen with k= 0...3). However this same behavior is not seen in loop #2 when program flow leaves the loop via the continue outer statement. The loop does not start anew the next time. Why?
[ Jess added UBB [CODE] tags, adjusted white space and disabled smilies ]
[ August 29, 2002: Message edited by: Jessica Sant ]
The quick answer is:
Conversion from type int to type float can result in the loss of precision, because values of type float are not precise to nine significant digits.
See the JLS for more information on this, section 5.1.2. web page
I was just looking over some notes and I decided to run this example:
class Testit3 extends Private {

void methodA() {
System.out.println("Inside child");
}

public static void main (String args[]) {
Private p= new Testit3();
p.methodA();


}
}
class Private {

private double methodA() {
return 25.0d;

}
}
I got this compiler error:
Testit3.java:9: methodA() has private access in Private
p.methodA();
which I was not expecting nor due I clearly understand the wording. I understand this cast - ((Testit3)p).methodA(); - is needed to make the example work.

I was expecting a could not resolve error as the private methodA() is not picked up by the child. You get this error when you comment out or delete private methodA() in class Private. Thanks in advance.
I am working through Jason Hunter, Java Servlet Programming and I have a question about a comment made in the text about an example. The chapter is about database connectivity and example 9-8 showcases a connection pool. Here is a cut and paste of part of the example:
synchronized (connections) {
while(cons.hasMoreElements()) {
con = (Connection)cons.nextElement();

Boolean b = (Boolean)connections.get(con);
if (b == Boolean.FALSE) {
// So we found an unused connection.
// Test its integrity with a quick setAutoCommit(true) call.
// For production use, more testing should be performed,
// such as executing a simple query.
try {
con.setAutoCommit(true);
}
catch(SQLException e) {
// Problem with the connection, replace it.
connections.remove(con);
con = getNewConnection();
}
// Update the Hashtable to show this one's taken
connections.put(con, Boolean.TRUE);
// Return the connection
return con;
}
}
In the text, the author states "For deployment, you probably want something that does a better job of maintaining the quality of the pool and does more verification of integtity than a simple call to setAutoCommit()." What quality is he refering to? Why wouldn't a call to setAutoCommit() be good enough? If you have a connection to the database, then you have a connection. What can go wrong on this? I have zero real experience with database connections. On my PC, it always connects, so I don't have anything to relate these statements to. Any insight would be appreciated.