List of usage examples for java.sql ResultSet getLong
long getLong(String columnLabel) throws SQLException;
ResultSet
object as a long
in the Java programming language. From source file:Main.java
public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/booltest", "booltest", "booltest"); conn.prepareStatement("create table booltest (id bigint, truefalse varchar(10));").execute(); PreparedStatement stmt = conn.prepareStatement("insert into booltest (id, truefalse) values (?, ?);"); stmt.setLong(1, (long) 123); stmt.setBoolean(2, true);//from w ww .j a v a2s. c o m stmt.execute(); stmt.setLong(1, (long) 456); stmt.setBoolean(2, false); stmt.execute(); ResultSet rs = conn.createStatement().executeQuery("select id, truefalse from booltest"); while (rs.next()) { System.out.println(rs.getLong(1) + " => " + rs.getBoolean(2)); } }
From source file:ImageStringToBlob.java
public static void main(String[] args) { Connection conn = null;/* w w w. jav a 2 s. co m*/ if (args.length != 1) { System.out.println("Missing argument: full path to <oscar.properties>"); return; } try { FileInputStream fin = new FileInputStream(args[0]); Properties prop = new Properties(); prop.load(fin); String driver = prop.getProperty("db_driver"); String uri = prop.getProperty("db_uri"); String db = prop.getProperty("db_name"); String username = prop.getProperty("db_username"); String password = prop.getProperty("db_password"); Class.forName(driver); conn = DriverManager.getConnection(uri + db, username, password); conn.setAutoCommit(true); // no transactions /* * select all records ids with image_data not null and contents is null * for each id fetch record * migrate data from image_data to contents */ String sql = "select image_id from client_image where image_data is not null and contents is null"; PreparedStatement pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); List<Long> ids = new ArrayList<Long>(); while (rs.next()) { ids.add(rs.getLong("image_id")); } rs.close(); sql = "select image_data from client_image where image_id = ?"; pst = conn.prepareStatement(sql); System.out.println("Migrating image data for " + ids.size() + " images..."); for (Long id : ids) { pst.setLong(1, id); ResultSet imagesRS = pst.executeQuery(); while (imagesRS.next()) { String dataString = imagesRS.getString("image_data"); Blob dataBlob = fromStringToBlob(dataString); if (writeBlobToDb(conn, id, dataBlob) == 1) { System.out.println("Image data migrated for image_id: " + id); } } imagesRS.close(); } System.out.println("Migration completed."); } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
From source file:friendsandfollowers.DBFollowersIDs.java
public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, FileNotFoundException, UnsupportedEncodingException { // Check arguments that passed in if ((args == null) || (args.length == 0)) { System.err.println("2 Parameters are required plus one optional " + "parameter to launch a Job."); System.err.println("First: String 'OUTPUT: /output/path/'"); System.err.println("Second: (int) Number of ids to fetch. " + "Provide number which increment by 5000 " + "(5000, 10000, 15000 etc) " + "or -1 to fetch all ids."); System.err.println("Third (optional): 'screen_name / user_id_str'"); System.err.println("If 3rd argument not provided then provide" + " Twitter users through database."); System.exit(-1);/*from w w w .j a v a 2 s . c o m*/ } MysqlDB DB = new MysqlDB(); AppOAuth AppOAuths = new AppOAuth(); Misc helpers = new Misc(); String endpoint = "/followers/ids"; String OutputDirPath = null; try { OutputDirPath = StringEscapeUtils.escapeJava(args[0]); } catch (Exception e) { System.err.println("Argument" + args[0] + " must be an String."); System.exit(-1); } int IDS_TO_FETCH_INT = -1; try { IDS_TO_FETCH_INT = Integer.parseInt(args[1]); } catch (NumberFormatException e) { System.err.println("Argument" + args[1] + " must be an integer."); System.exit(-1); } int IDS_TO_FETCH = 0; if (IDS_TO_FETCH_INT > 5000) { float IDS_TO_FETCH_F = (float) IDS_TO_FETCH_INT / 5000; IDS_TO_FETCH = (int) Math.ceil(IDS_TO_FETCH_F); } else if ((IDS_TO_FETCH_INT <= 5000) && (IDS_TO_FETCH_INT > 0)) { IDS_TO_FETCH = 1; } String targetedUser = ""; if (args.length == 3) { try { targetedUser = StringEscapeUtils.escapeJava(args[2]); } catch (Exception e) { System.err.println("Argument" + args[2] + " must be an String."); System.exit(-1); } } try { TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint); Twitter twitter = tf.getInstance(); int RemainingCalls = AppOAuths.RemainingCalls; int RemainingCallsCounter = 0; System.out.println("First Time Remianing Calls: " + RemainingCalls); String Screen_name = AppOAuths.screen_name; System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name); IDs ids; System.out.println("Listing followers ids."); // if targetedUser not provided by argument, then look into database. if (StringUtils.isEmpty(targetedUser)) { String selectQuery = "SELECT * FROM `followers_parent` WHERE " + "`targeteduser` != '' AND " + "`nextcursor` != '0' AND " + "`nextcursor` != '2'"; ResultSet results = DB.selectQ(selectQuery); int numRows = DB.numRows(results); if (numRows < 1) { System.err.println("No User in database to get followersIDS"); System.exit(-1); } OUTERMOST: while (results.next()) { int followers_parent_id = results.getInt("id"); targetedUser = results.getString("targeteduser"); long cursor = results.getLong("nextcursor"); System.out.println("Targeted User: " + targetedUser); int idsLoopCounter = 0; int totalIDs = 0; // put idsJSON in a file PrintWriter writer = new PrintWriter(OutputDirPath + "/" + targetedUser, "UTF-8"); // call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); do { ids = null; try { if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); ids = twitter.getFollowersIDs(LongValueTargetedUser, cursor); } else { ids = twitter.getFollowersIDs(targetedUser, cursor); } } catch (TwitterException te) { // do not throw if user has protected tweets, // or if they deleted their account if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED || te.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Followers Get Exception: " + te.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println( "New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } // update cursor in "followers_parent" String fieldValues = "`nextcursor` = 2"; String where = "id = " + followers_parent_id; DB.Update("`followers_parent`", fieldValues, where); // If error then switch to next user continue OUTERMOST; } if (ids.getIDs().length > 0) { idsLoopCounter++; totalIDs += ids.getIDs().length; System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length); JSONObject responseDetailsJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); for (long id : ids.getIDs()) { jsonArray.put(id); } Object idsJSON = responseDetailsJson.put("ids", jsonArray); writer.println(idsJSON); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth " + "Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } if (IDS_TO_FETCH_INT != -1) { if (idsLoopCounter == IDS_TO_FETCH) { break; } } } while ((cursor = ids.getNextCursor()) != 0); writer.close(); System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs); System.out.println(); // update cursor in "followers_parent" String fieldValues = "`nextcursor` = " + cursor; String where = "id = " + followers_parent_id; DB.Update("`followers_parent`", fieldValues, where); } // loop through every result found in db } else { // Second Argument Set, so we are here. System.out.println("screen_name / user_id_str passed by argument"); int idsLoopCounter = 0; int totalIDs = 0; // put idsJSON in a file PrintWriter writer = new PrintWriter( OutputDirPath + "/" + targetedUser + "_ids_" + helpers.getUnixTimeStamp(), "UTF-8"); // call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); long cursor = -1; do { ids = null; try { if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); ids = twitter.getFollowersIDs(LongValueTargetedUser, cursor); } else { ids = twitter.getFollowersIDs(targetedUser, cursor); } } catch (TwitterException te) { // do not throw if user has protected tweets, or if they deleted their account if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED || te.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Followers Get Exception: " + te.getMessage()); } System.exit(-1); } if (ids.getIDs().length > 0) { idsLoopCounter++; totalIDs += ids.getIDs().length; System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length); JSONObject responseDetailsJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); for (long id : ids.getIDs()) { jsonArray.put(id); } Object idsJSON = responseDetailsJson.put("ids", jsonArray); writer.println(idsJSON); } // If rate limit reach then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } if (IDS_TO_FETCH_INT != -1) { if (idsLoopCounter == IDS_TO_FETCH) { break; } } } while ((cursor = ids.getNextCursor()) != 0); writer.close(); System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs); System.out.println(); } } catch (TwitterException te) { // te.printStackTrace(); System.err.println("Failed to get followers' ids: " + te.getMessage()); System.exit(-1); } System.out.println("!!!! DONE !!!!"); }
From source file:friendsandfollowers.DBFriendsIDs.java
public static void main(String[] args) throws ClassNotFoundException, SQLException, JSONException, FileNotFoundException, UnsupportedEncodingException { // Check arguments that passed in if ((args == null) || (args.length == 0)) { System.err.println("2 Parameters are required plus one optional " + "parameter to launch a Job."); System.err.println("First: String 'OUTPUT: /output/path/'"); System.err.println("Second: (int) Number of ids to fetch. " + "Provide number which increment by 5000 " + "(5000, 10000, 15000 etc) " + "or -1 to fetch all ids."); System.err.println("Third (optional): 'screen_name / user_id_str'"); System.err.println("If 3rd argument not provided then provide" + " Twitter users through database."); System.exit(-1);/* w w w . j av a 2 s . c o m*/ } MysqlDB DB = new MysqlDB(); AppOAuth AppOAuths = new AppOAuth(); Misc helpers = new Misc(); String endpoint = "/friends/ids"; String OutputDirPath = null; try { OutputDirPath = StringEscapeUtils.escapeJava(args[0]); } catch (Exception e) { System.err.println("Argument" + args[0] + " must be an String."); System.exit(-1); } int IDS_TO_FETCH_INT = -1; try { IDS_TO_FETCH_INT = Integer.parseInt(args[1]); } catch (NumberFormatException e) { System.err.println("Argument" + args[1] + " must be an integer."); System.exit(-1); } int IDS_TO_FETCH = 0; if (IDS_TO_FETCH_INT > 5000) { float IDS_TO_FETCH_F = (float) IDS_TO_FETCH_INT / 5000; IDS_TO_FETCH = (int) Math.ceil(IDS_TO_FETCH_F); } else if ((IDS_TO_FETCH_INT <= 5000) && (IDS_TO_FETCH_INT > 0)) { IDS_TO_FETCH = 1; } String targetedUser = ""; if (args.length == 3) { try { targetedUser = StringEscapeUtils.escapeJava(args[2]); } catch (Exception e) { System.err.println("Argument" + args[2] + " must be an String."); System.exit(-1); } } try { TwitterFactory tf = AppOAuths.loadOAuthUser(endpoint); Twitter twitter = tf.getInstance(); int RemainingCalls = AppOAuths.RemainingCalls; int RemainingCallsCounter = 0; System.out.println("First Time Remianing Calls: " + RemainingCalls); String Screen_name = AppOAuths.screen_name; System.out.println("First Time Loaded OAuth Screen_name: " + Screen_name); IDs ids; System.out.println("Listing friends ids."); // if targetedUser not provided by argument, then look into database. if (StringUtils.isEmpty(targetedUser)) { String selectQuery = "SELECT * FROM `followings_parent` WHERE " + "`targeteduser` != '' AND " + "`nextcursor` != '0' AND " + "`nextcursor` != '2'"; ResultSet results = DB.selectQ(selectQuery); int numRows = DB.numRows(results); if (numRows < 1) { System.err.println("No User in database to get friendsIDS"); System.exit(-1); } OUTERMOST: while (results.next()) { int following_parent_id = results.getInt("id"); targetedUser = results.getString("targeteduser"); long cursor = results.getLong("nextcursor"); System.out.println("Targeted User: " + targetedUser); int idsLoopCounter = 0; int totalIDs = 0; // put idsJSON in a file PrintWriter writer = new PrintWriter(OutputDirPath + "/" + targetedUser, "UTF-8"); // call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); do { ids = null; try { if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); ids = twitter.getFriendsIDs(LongValueTargetedUser, cursor); } else { ids = twitter.getFriendsIDs(targetedUser, cursor); } } catch (TwitterException te) { // do not throw if user has protected tweets, // or if they deleted their account if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED || te.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Friends Get Exception: " + te.getMessage()); } // If rate limit reached then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println( "New User Loaded OAuth" + " Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } // update cursor in "followings_parent" String fieldValues = "`nextcursor` = 2"; String where = "id = " + following_parent_id; DB.Update("`followings_parent`", fieldValues, where); // If error then switch to next user continue OUTERMOST; } if (ids.getIDs().length > 0) { idsLoopCounter++; totalIDs += ids.getIDs().length; System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length); JSONObject responseDetailsJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); for (long id : ids.getIDs()) { jsonArray.put(id); } Object idsJSON = responseDetailsJson.put("ids", jsonArray); writer.println(idsJSON); } // If rate limit reached then switch Auth user. RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth " + "Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } if (IDS_TO_FETCH_INT != -1) { if (idsLoopCounter == IDS_TO_FETCH) { break; } } } while ((cursor = ids.getNextCursor()) != 0); writer.close(); System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs); System.out.println(); // update cursor in "followings_parent" String fieldValues = "`nextcursor` = " + cursor; String where = "id = " + following_parent_id; DB.Update("`followings_parent`", fieldValues, where); } // loop through every result found in db } else { // Second Argument Sets, so we are here. System.out.println("screen_name / user_id_str " + "passed by argument"); int idsLoopCounter = 0; int totalIDs = 0; // put idsJSON in a file PrintWriter writer = new PrintWriter( OutputDirPath + "/" + targetedUser + "_ids_" + helpers.getUnixTimeStamp(), "UTF-8"); // call different functions for screen_name and id_str Boolean chckedNumaric = helpers.isNumeric(targetedUser); long cursor = -1; do { ids = null; try { if (chckedNumaric) { long LongValueTargetedUser = Long.valueOf(targetedUser).longValue(); ids = twitter.getFriendsIDs(LongValueTargetedUser, cursor); } else { ids = twitter.getFriendsIDs(targetedUser, cursor); } } catch (TwitterException te) { // do not throw if user has protected tweets, // or if they deleted their account if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED || te.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println(targetedUser + " is protected or account is deleted"); } else { System.out.println("Friends Get Exception: " + te.getMessage()); } System.exit(-1); } if (ids.getIDs().length > 0) { idsLoopCounter++; totalIDs += ids.getIDs().length; System.out.println(idsLoopCounter + ": IDS length: " + ids.getIDs().length); JSONObject responseDetailsJson = new JSONObject(); JSONArray jsonArray = new JSONArray(); for (long id : ids.getIDs()) { jsonArray.put(id); } Object idsJSON = responseDetailsJson.put("ids", jsonArray); writer.println(idsJSON); } // If rate limit reach then switch Auth user RemainingCallsCounter++; if (RemainingCallsCounter >= RemainingCalls) { // load auth user tf = AppOAuths.loadOAuthUser(endpoint); twitter = tf.getInstance(); System.out.println("New User Loaded OAuth Screen_name: " + AppOAuths.screen_name); RemainingCalls = AppOAuths.RemainingCalls; RemainingCallsCounter = 0; System.out.println("New Remianing Calls: " + RemainingCalls); } if (IDS_TO_FETCH_INT != -1) { if (idsLoopCounter == IDS_TO_FETCH) { break; } } } while ((cursor = ids.getNextCursor()) != 0); writer.close(); System.out.println("Total ids dumped of " + targetedUser + " are: " + totalIDs); System.out.println(); } } catch (TwitterException te) { // te.printStackTrace(); System.err.println("Failed to get friends' ids: " + te.getMessage()); System.exit(-1); } System.out.println("!!!! DONE !!!!"); }
From source file:com.intelius.iap4.TigerLineHit.java
public static void main(String[] args) { String _tigerDs = "jdbc:h2:/home/sxu/playground/tiger"; ResultSet rs = null; PreparedStatement ps = null;//www .j a v a 2 s .c o m List<TigerLineHit> ret = new ArrayList<TigerLineHit>(); try { // if (_tigerDs instanceof JdbcDataSource) { // JdbcDataSource ds = (JdbcDataSource) _tigerDs; // conn = ds.getPooledConnection().getConnection(); // }else{ // conn = _tigerDs.getConnection(); // } //try address "540 westerly parkway, state college, pa 16801" Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection(_tigerDs, "sa", ""); ps = conn.prepareStatement(generateSelectQuery("PA")); int i = 1; String streetNum = "540"; String zip = "16801"; ps.setString(i++, "Westerly"); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, streetNum); ps.setString(i++, zip); ps.setString(i++, zip); rs = ps.executeQuery(); while (rs.next()) { TigerLineHit hit = new TigerLineHit(); hit.streetNum = streetNum; hit.tlid = rs.getLong("tlid"); hit.frAddL = rs.getString("fraddl"); hit.frAddR = rs.getString("fraddr"); hit.toAddL = rs.getString("toaddl"); hit.toAddR = rs.getString("toaddr"); hit.zipL = rs.getString("zipL"); hit.zipR = rs.getString("zipR"); hit.toLat = rs.getFloat("tolat"); hit.toLon = rs.getFloat("tolong"); hit.frLat = rs.getFloat("frlat"); hit.frLon = rs.getFloat("tolong"); hit.lat1 = rs.getFloat("lat1"); hit.lat2 = rs.getFloat("lat2"); hit.lat3 = rs.getFloat("lat3"); hit.lat4 = rs.getFloat("lat4"); hit.lat5 = rs.getFloat("lat5"); hit.lat6 = rs.getFloat("lat6"); hit.lat7 = rs.getFloat("lat7"); hit.lat8 = rs.getFloat("lat8"); hit.lat9 = rs.getFloat("lat9"); hit.lat10 = rs.getFloat("lat10"); hit.lon1 = rs.getFloat("long1"); hit.lon2 = rs.getFloat("long2"); hit.lon3 = rs.getFloat("long3"); hit.lon4 = rs.getFloat("long4"); hit.lon5 = rs.getFloat("long5"); hit.lon6 = rs.getFloat("long6"); hit.lon7 = rs.getFloat("long7"); hit.lon8 = rs.getFloat("long8"); hit.lon9 = rs.getFloat("long9"); hit.lon10 = rs.getFloat("long10"); hit.fedirp = rs.getString("fedirp"); hit.fetype = rs.getString("fetype"); hit.fedirs = rs.getString("fedirs"); ret.add(hit); // System.out.println(ret.toString()); // } } catch (Exception e) { e.printStackTrace(); } finally { //DbUtils.closeQuietly(conn); DbUtils.closeQuietly(rs); DbUtils.closeQuietly(ps); } //return ret; }
From source file:org.spc.ofp.data.Repository.java
public static Long readLong(final ResultSet rs, final String columnName) throws SQLException { final long value = rs.getLong(columnName); return rs.wasNull() ? null : Long.valueOf(value); }
From source file:at.alladin.rmbt.db.dao.TestStatDao.java
/** * //from www . j av a 2 s . com * @param rs * @return * @throws SQLException * @throws JSONException */ private static TestStat instantiateItem(ResultSet rs) throws SQLException, JSONException { TestStat result = new TestStat(); result.setTestUid(rs.getLong("test_uid")); result.setCpuUsage(new JSONObject(rs.getString("cpu_usage"))); result.setMemUsage(new JSONObject(rs.getString("mem_usage"))); return result; }
From source file:net.sf.sprockets.sql.Statements.java
/** * Execute the query, get the long value in the first row and column of the result set, and * close the statement./*from www. j a v a 2s . c o m*/ * * @param stmt * must already have parameters set * @return {@link Long#MIN_VALUE} if the result set is empty */ public static long firstLong(PreparedStatement stmt) throws SQLException { ResultSet rs = stmt.executeQuery(); long l = rs.next() ? rs.getLong(1) : Long.MIN_VALUE; stmt.close(); return l; }
From source file:net.sf.sprockets.sql.Statements.java
/** * Execute the insert statement, get the first generated key as a long, and close the statement. * // w ww . jav a 2 s. co m * @param stmt * must have been created with {@link Statement#RETURN_GENERATED_KEYS} and already * have parameters set * @return 0 if the statement did not generate any keys */ public static long firstLongKey(PreparedStatement stmt) throws SQLException { stmt.execute(); ResultSet rs = stmt.getGeneratedKeys(); long l = rs.next() ? rs.getLong(1) : 0L; stmt.close(); return l; }
From source file:com.espertech.esperio.db.core.DBUtil.java
/** * Returns the object value for a given column and type. * @param rs result set/*from w ww . ja va2 s . com*/ * @param index column index * @param valueType value type * @return object value * @throws java.sql.SQLException if the column could not be read */ public static Object getValue(ResultSet rs, int index, int valueType) throws SQLException { if (valueType == Types.INTEGER) { return rs.getInt(index); } else if (valueType == Types.BIGINT) { return rs.getLong(index); } else if (valueType == Types.BLOB) { Blob blob = rs.getBlob(index); return getBlobValue(blob); } return rs.getObject(index); }