Error In Getint Sql Returned
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or resultset getint posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss java resultset size Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only check if resultset is empty takes a minute: Sign up Check an int variable is null or is empty from the Database up vote 6 down vote favorite 1 I am having a variable which is: nocustomers = rs.getInt("CUST_TRAN_COUNT"); I would like to perform if resultset getrow it is null or not. I tried if (nocustomers ==null) It showed an error. How do I solve this? My new modified code is: try{ query=" select * from SS_summary where txn_date = to_date ('"+asatdate+"','YYYY-MM-DD') "; st = conn.createStatement(); rs = st.executeQuery(query); if (rs.next()) { nocustomers = rs.getInt("CUST_TRAN_COUNT"); nocheques =rs.getInt("CHEQ_DELIVERED_MAIN"); } if (rs.wasNull()) { out.println("NO DATA FOUND"); } %> java jdbc share|improve this question edited Sep 14 '11 at 11:07 Buhake Sindi 56.1k15112178 asked Sep 14 '11 at 6:41 maas 31113
Preparedstatement
you can't check primitive datatypes for null (= null pointer), only pointers to objects. what is rs ? you have to check your ORM / SQL adapter for such a functionality –Marian Theisen Sep 14 '11 at 6:43 Whats the error and whats the datatype of this variable. –Shahzeb Sep 14 '11 at 6:43 Related: stackoverflow.com/questions/2254435/can-an-int-be-null-in-java –Joachim Sauer Sep 14 '11 at 6:50 @maas, you don't need to do a rs.wasNull() as rs.getInt() will return a 0 if data was null by default. Rather say, if (nocustomers == 0) { out.println("NO DATA FOUND"); }. –Buhake Sindi Sep 14 '11 at 11:09 I'll have cancer because of looking at that example code :( –topr Oct 1 '14 at 0:13 add a comment| 5 Answers 5 active oldest votes up vote 6 down vote int can't be null, because it's a primitive type. For the same reason ResultSet.getInt() can't return null. You will need to call ResultSet.wasNull() right after your getInt() call to check if the value was null. Note that since int can't be null, nocustomer will have a value even if the database value is null. getInt() is defined to return 0 when the DB value is null, so nocustomers will be 0. share|improve this answer answered Sep 14 '11 at 6:48 Joachim Sauer 184k35393501 resultSet.wasNull() did not work and showed an error "java.sql.SQLException: No data read " &nda
Method java.sql Interface ResultSet All Superinterfaces: AutoCloseable, Wrapper All Known Subinterfaces: CachedRowSet, FilteredRowSet, JdbcRowSet, JoinRowSet, RowSet, SyncResolver, WebRowSet public interface ResultSet extends Wrapper, AutoCloseable A table of data representing a database result set, which is usually generated
Java Arraylist
by executing a statement that queries the database. A 6 object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The 5 method moves the cursor to the next row, and because it returns 4 when there are no more rows in the 3 object, it can be used in a 2 loop to iterate through the http://stackoverflow.com/questions/7412250/check-an-int-variable-is-null-or-is-empty-from-the-database result set. A default 1 object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce 0 objects that are scrollable and/or updatable. The following code fragment, in which 9 is a valid 8 object, illustrates how to make a result set that is https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html scrollable and insensitive to updates by others, and that is updatable. See 7 fields for other options. Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2"); // rs will be scrollable, will not show changes made by others, // and will be updatable The 6 interface provides getter methods ( 5, 4, and so on) for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once. For the getter methods, a JDBC driver attempts to convert the underlying data to the Java type specified in the getter method and returns a suitable Java value. The JDBC specification has a table showing the allowable mappings from SQL types to Java types that can be used by the 3 getter methods. Column names used as input to getter methods are case insensitive. When a getter method is called with a column name and several colum
java.io.IOException java.sql.Connection java.sql.Timestamp java.util.logging.Level java.sql.DatabaseMetaData java.sql.SQLException Frequently Used Methods next ( ) close ( ) getString ( ) getInt ( ) http://www.programcreek.com/java-api-examples/java.sql.ResultSet getLong ( ) getMetaData ( ) wasNull ( ) getObject ( ) https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/api/java.sql.ResultSet.html getTimestamp ( ) getBoolean ( ) getDouble ( ) getBytes ( ) TYPE_FORWARD_ONLY getDate ( ) getBigDecimal ( ) first ( ) getFloat ( ) getShort ( ) CONCUR_READ_ONLY getRow ( ) View more ... last ( ) updateString ( ) getBlob ( ) getBinaryStream ( error in ) beforeFirst ( ) getTime ( ) TYPE_SCROLL_INSENSITIVE updateRow ( ) insertRow ( ) HOLD_CURSORS_OVER_COMMIT moveToInsertRow ( ) FETCH_FORWARD CONCUR_UPDATABLE getByte ( ) getClob ( ) TYPE_SCROLL_SENSITIVE absolute ( ) CLOSE_CURSORS_AT_COMMIT getArray ( ) updateInt ( ) getCharacterStream ( ) getStatement ( ) findColumn ( ) updateBinaryStream ( ) updateNClob ( ) isClosed ( ) updateNCharacterStream ( ) error in getint previous ( ) isAfterLast ( ) updateNString ( ) setFetchSize ( ) updateNull ( ) isBeforeFirst ( ) updateLong ( ) updateClob ( ) updateCharacterStream ( ) relative ( ) updateDate ( ) afterLast ( ) updateBlob ( ) getType ( ) updateShort ( ) updateAsciiStream ( ) isLast ( ) updateObject ( ) unwrap ( ) getNString ( ) updateFloat ( ) getFetchSize ( ) clearWarnings ( ) isFirst ( ) updateBytes ( ) getConcurrency ( ) deleteRow ( ) getAsciiStream ( ) getSQLXML ( ) FETCH_UNKNOWN FETCH_REVERSE updateDouble ( ) getWarnings ( ) updateTimestamp ( ) setFetchDirection ( ) updateBoolean ( ) getNClob ( ) getRef ( ) Java Code Examples for java.sql.ResultSet The following are top voted examples for showing how to use java.sql.ResultSet. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples. → Go to Official Java Doc + Save this class to your library Example 1 Project
Within a row its column values can be accessed in any order. A ResultSet maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The 'next' method moves the cursor to the next row. The getXXX methods retrieve column values for the current row. You can retrieve values either using the index number of the column, or by using the name of the column. In general using the column index will be more efficient. Columns are numbered from 1. For maximum portability, ResultSet columns within each row should be read in left-to-right order and each column should be read only once. For the getXXX methods, the JDBC driver attempts to convert the underlying data to the specified Java type and returns a suitable Java value. See the JDBC specification for allowable mappings from SQL types to Java types with the ResultSet.getXXX methods. Column names used as input to getXXX methods are case insensitive. When performing a getXXX using a column name, if several columns have the same name, then the value of the first matching column will be returned. The column name option is designed to be used when column names are used in the SQL query. For columns that are NOT explicitly named in the query, it is best to use column numbers. If column names were used there is no way for the programmer to guarantee that they actually refer to the intended columns. A ResultSet is automatically closed by the Statement that generated it when that Statement is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results. The number, types and properties of a ResultSet's columns are provided by the ResulSetMetaData object returned by the getMetaData method. See Also: executeQuery, getResultSet, ResultSetMetaData clearWarnings() After this call getWarnings returns null until a new warning is reported for this ResultSet. close() In some cases, it is desirable to immediately release a ResultSet's database and JDBC resources instead of waiting for this to happen when it is automatically closed; the close method provides this immediate release. findColumn(String) Map a Resultset column name to a ResultSet column index. getAsciiStream(int) A column value can be retrieved as a stream of ASCII characters and then read in chunks from the stream. getAsciiStream