Questions tagged [resultset]

A result set is a set of rows from a database, which is usually generated by executing a statement that queries the database. A resultSet object maintains a cursor pointing to its current row of data.

A result set is a set of rows from a database, which is usually generated by executing a statement that queries the database. A resultSet object maintains a cursor pointing to its current row of data.

2543 questions
329
votes
13 answers

Checking for a null int value from a Java ResultSet

In Java I'm trying to test for a null value, from a ResultSet, where the column is being cast to a primitive int type. int iVal; ResultSet rs = magicallyAppearingStmt.executeQuery(query); if (rs.next()) { if (rs.getObject("ID_PARENT") != null &&…
ian_scho
  • 5,906
  • 9
  • 35
  • 51
317
votes
15 answers

How do I get the size of a java.sql.ResultSet?

Shouldn't this be a pretty straightforward operation? However, I see there's neither a size() nor length() method.
Jake
  • 15,007
  • 22
  • 70
  • 86
125
votes
14 answers

Most efficient conversion of ResultSet to JSON?

The following code converts a ResultSet to a JSON string using JSONArray and JSONObject. import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONException; import java.sql.SQLException; import java.sql.ResultSet; import…
Devin Dixon
  • 11,553
  • 24
  • 86
  • 167
99
votes
5 answers

How to get the number of columns from a JDBC ResultSet?

I am using CsvJdbc (it is a JDBC-driver for csv-files) to access a csv-file. I don't know how many columns the csv-file contains. How can I get the number of columns? Is there any JDBC-function for this? I can not find any methods for this in…
Jonas
  • 121,568
  • 97
  • 310
  • 388
92
votes
13 answers

How to get row count using ResultSet in Java?

I'm trying to create a simple method that receives a ResultSet as a parameter and returns an int that contains the row count of the ResultSet. Is this a valid way of doing this or not so much? int size = 0; try { while(rs.next()){ …
user818700
87
votes
6 answers

How can I determine if the column name exist in the ResultSet?

As the ResultSet contains the data returned from the dynamic SQL, if there are any method to determine if the ResultSet contains a particular column name? For example , if I run rs.getString("Column_ABC") but "Column_ABC" does not really exist, it…
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
79
votes
9 answers

How to check if a result set is empty?

I have a sql statement that returns no hits. For example, 'select * from TAB where 1 = 2'. I want to check how many rows are returned, cursor.execute(query_sql) rs = cursor.fetchall() Here I get already exception: "(0, 'No result set')" How can…
Tao Venzke
  • 893
  • 1
  • 6
  • 6
77
votes
12 answers

Can a JPA Query return results as a Java Map?

We are currently building a Map manually based on the two fields that are returned by a named JPA query because JPA 2.1 only provides a getResultList() method: @NamedQuery{name="myQuery",query="select c.name, c.number from Client…
Eddie
  • 9,696
  • 4
  • 45
  • 58
75
votes
14 answers

Get Number of Rows returned by ResultSet in Java

I have used a ResultSet that returns certain number of rows. My code is something like this: ResultSet res = getData(); if(!res.next()) { System.out.println("No Data Found"); } while(res.next()) { // code to display the data in the…
Kiran
  • 1,119
  • 1
  • 10
  • 19
74
votes
7 answers

Fastest way to write huge data in text file Java

I have to write huge data in text[csv] file. I used BufferedWriter to write the data and it took around 40 secs to write 174 mb of data. Is this the fastest speed java can offer? bufferedWriter = new BufferedWriter ( new FileWriter ( "fileName.csv"…
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
65
votes
13 answers

How to access mysql result set data with a foreach loop

I'm developing a php app that uses a database class to query MySQL. The class is here: http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/*note there are multiple bad practices demonstrated in the tutorial -- it should not be…
medk
  • 9,233
  • 18
  • 57
  • 79
63
votes
9 answers

Efficient way to Handle ResultSet in Java

I'm using a ResultSet in Java, and am not sure how to properly close it. I'm considering using the ResultSet to construct a HashMap and then closing the ResultSet after that. Is this HashMap technique efficient, or are there more efficient ways of…
Deepak
  • 6,684
  • 18
  • 69
  • 121
63
votes
10 answers

How to select an empty result set?

I'm using a stored procedure in MySQL, with a CASE statement. In the ELSE clause of the CASE ( equivalent to default: ) I want to select and return an empty result set, thus avoiding to throw an SQL error by not handling the ELSE case, and instead…
Petruza
  • 11,744
  • 25
  • 84
  • 136
60
votes
11 answers

Mapping a JDBC ResultSet to an object

I have a user class that has 16 attributes, things such as firstname, lastname, dob, username, password etc... These are all stored in a MySQL database and when I want to retrieve users I use a ResultSet. I want to map each of the columns back to…
Quanqai
  • 647
  • 1
  • 5
  • 10
55
votes
14 answers

ResultSet: Retrieving column values by index versus retrieving by label

When using JDBC, I often come across constructs like ResultSet rs = ps.executeQuery(); while (rs.next()) { int id = rs.getInt(1); // Some other actions } I asked myself (and authors of code too) why not to use labels for retrieving column…
Rorick
  • 8,857
  • 3
  • 32
  • 37
1
2 3
99 100