db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jørgen Løland <Jorgen.Lol...@Sun.COM>
Subject Re: Newbie Blues : Unable to connect to derby database using JDBC
Date Fri, 16 Nov 2007 10:02:38 GMT
One more thing...

Your query

"SELECT firstname, lastname FROM USERS WHERE uid = 1001

will never return more than one row since uid is primary key. Hence, the 
SimpleApp1 code will throw an exception on line 73 even if you change 
the query as suggested in the previous comment.

I wonder if you are misinterpreting the "row" and "column" terms in 
ResultSets? Assume the following information is in your table:

uid	firstname	lastname
---	---------	--------
1000	Sam		Gamgee
1001	Legolas		Greenleaf
1002 	Peregrin	Took

Your query would return 1 row with two columns:
"Legolas" and "Greenleaf"

To print this information, you should do:

rs.next(); // Current row is now 1, which is Legolas Greenleaf
System.out.println("This is person # 1001");
System.out.println("Firstname: "+rs.getString(1));
System.out.println("Lastname: "+rs.getString(2));

Which would print:

This is person # 1001
Firstname: Legolas
Lastname: Greenleaf

If you instead had a query

"SELECT uid, firstname, lastname FROM USERS"

you would get a ResultSet with three rows and three columns, and you 
could print them like this:

rs.next(); // now on row 1, which is Sam Gamgee
System.out.println("This is person # "+rs.getInt(1)); //column 1 is uid
System.out.println("Firstname: "+rs.getString(2));
System.out.println("Lastname: "+rs.getString(3));

rs.next(); // now on row 2, which is Legolas Greenleaf
...

or better:
while (rs.next()) {
	System.out.println("This is person # "+rs.getInt(1));
	System.out.println("Firstname: "+rs.getString(2));
	System.out.println("Lastname: "+rs.getString(3));
}

Hope this helped.

--
Jørgen Løland

Aneez Backer wrote:
> Hi
> 
> I have attached the Java source file. The error is as follows when I run the program:
> 
> ------------------------------
> 
> SimpleApp starting in embedded mode.
> Loaded the appropriate driver.
> Connected to 24k
> exception thrown:
> java.sql.SQLDataException: Invalid character string format for type int.
>         at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unkn
> own Source)
>         at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source
> )
>         at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException
> (Unknown Source)
>         at org.apache.derby.impl.jdbc.EmbedResultSet.noStateChangeException(Unkn
> own Source)
>         at org.apache.derby.impl.jdbc.EmbedResultSet.getInt(Unknown Source)
>         at SampleApp1.go(SampleApp1.java:66)
>         at SampleApp1.main(SampleApp1.java:23)
> Caused by: java.sql.SQLException: Invalid character string format for type int.
>         at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknow
> n Source)
>         at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransport
> AcrossDRDA(Unknown Source)
>         ... 7 more
> Caused by: ERROR 22018: Invalid character string format for type int.
>         at org.apache.derby.iapi.error.StandardException.newException(Unknown So
> urce)
>         at org.apache.derby.iapi.types.SQLChar.getInt(Unknown Source)
>         ... 3 more
> SampleApp finished
> 
> ------------------------------------------------
> 
> When I run the same query through the 'ij' tool on the console, it works fine. 
> 
> The create table statement is as follows:
> 
> CREATE TABLE USERS(
> uid BIGINT PRIMARY KEY,
> email VARCHAR(50) UNIQUE NOT NULL,
> passwd VARCHAR(15) NOT NULL,
> firstname VARCHAR(20),
> lastname VARCHAR(20),
> date_of_birth DATE,
> place VARCHAR(50),
> aboutme VARCHAR(500),
> myblog VARCHAR(50)
> );
> 
> Am using db-derby-10.3.1.4-bin. So , the version is 10.3
> 
> 
> Looking forward for comments
> Thanks
> -Aneez

Mime
View raw message