Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How i assign Java Resultset to a Array?

 
Skm Liyanage
Greenhorn
Posts: 23
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use this way. but this code segment does not work.
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) You can't create a new Object[] and then cast it to a String[]. You will receive a ClassCastException because an Object[] is not a String[] (but a String[] is an Object[]). Just create a new String[] instead.

2) You executed the query but never traverse the ResultSet. Depending on the maximum number of results you expect (1 or more than 1), you need an if statement or loop:
Note that if you use a loop you should use a List<String[]> to store your data. Remember to create the new String[] inside the loop, otherwise you will have only one single String[] object and change that for each record.

3) Although array indexing is 0-based, JDBC is 1-based. You need to account for that:
 
Skm Liyanage
Greenhorn
Posts: 23
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You very much dear. i got it.
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic