List of usage examples for java.sql ResultSet getObject
Object getObject(String columnLabel) throws SQLException;
Gets the value of the designated column in the current row of this ResultSet
object as an Object
in the Java programming language.
From source file:Main.java
public static void main(String[] args) throws Exception { ResultSet rs = null; Connection conn = null;/*from ww w. j a v a 2 s .co m*/ PreparedStatement pstmt = null; PreparedStatement pstmt2 = null; conn = getOracleConnection(); String[] columnNames = { "id", "name", "content", "date_created" }; Object[] inputValues = new Object[columnNames.length]; inputValues[0] = new java.math.BigDecimal(100); inputValues[1] = new String("String Value"); inputValues[2] = new String("This is my resume."); inputValues[3] = new Timestamp((new java.util.Date()).getTime()); // prepare blob object from an existing binary column String insert = "insert into resume (id, name, content, date_created ) values(?, ?, ?, ?)"; pstmt = conn.prepareStatement(insert); pstmt.setObject(1, inputValues[0]); pstmt.setObject(2, inputValues[1]); pstmt.setObject(3, inputValues[2]); pstmt.setObject(4, inputValues[3]); pstmt.executeUpdate(); String query = "select id, name, content, date_created from resume where id=?"; pstmt2 = conn.prepareStatement(query); pstmt2.setObject(1, inputValues[0]); rs = pstmt2.executeQuery(); Object[] outputValues = new Object[columnNames.length]; if (rs.next()) { for (int i = 0; i < columnNames.length; i++) { outputValues[i] = rs.getObject(i + 1); } } System.out.println("id=" + ((java.math.BigDecimal) outputValues[0]).toString()); System.out.println("name=" + ((String) outputValues[1])); System.out.println("content=" + ((Clob) outputValues[2])); System.out.println("date_created=" + ((java.sql.Date) outputValues[3]).toString()); rs.close(); pstmt.close(); pstmt2.close(); conn.close(); }
From source file:Main.java
public static Object readJavaObject(Connection conn, long id) throws Exception { PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL); pstmt.setLong(1, id);/*w w w .j ava2 s . c om*/ ResultSet rs = pstmt.executeQuery(); rs.next(); Object object = rs.getObject("object_value"); String className = object.getClass().getName(); rs.close(); pstmt.close(); return object; }
From source file:Main.java
public static Object readJavaObject(Connection conn, long id) throws Exception { PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL); pstmt.setLong(1, id);//from w w w. j a va 2 s . co m ResultSet rs = pstmt.executeQuery(); rs.next(); Object object = rs.getObject(1); String className = object.getClass().getName(); rs.close(); pstmt.close(); return object; }
From source file:SerializeJavaObjects_MySQL.java
public static Object readJavaObject(Connection conn, long id) throws Exception { PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL); pstmt.setLong(1, id);//from ww w . j a v a 2 s .c o m ResultSet rs = pstmt.executeQuery(); rs.next(); Object object = rs.getObject(1); String className = object.getClass().getName(); rs.close(); pstmt.close(); System.out.println("readJavaObject: done de-serializing: " + className); return object; }
From source file:com.pinterest.deployservice.db.SingleResultSetHandlerFactory.java
public static <T> ResultSetHandler<T> newObjectHandler() { return new ResultSetHandler<T>() { @Override/*ww w . j a v a 2 s . c o m*/ public T handle(ResultSet resultSet) throws SQLException { if (resultSet.next()) { return (T) resultSet.getObject(1); } return null; } }; }
From source file:fr.eo.util.dumper.JSONDumper.java
private static Object getFieldValue(RequestDefinitionBean request, int pos, ResultSet rs, String fieldName) throws SQLException { String str = String.valueOf(rs.getObject(fieldName)).trim(); if (str.endsWith(".0")) { str = str.substring(0, str.length() - 2); }//from w w w .java 2 s. co m if (str.equals("null") || str.isEmpty()) { return null; } if (request.compressedStrPos.contains(pos)) { str = getCompressedString(str); } else if (request.numberPos.contains(pos)) { Number number = rs.getDouble(fieldName); if (number.longValue() == number.doubleValue()) { return number.longValue(); } return number.doubleValue(); } else if (request.fieldStrPos.contains(pos)) { str = formatString(str); } return str; }
From source file:com.pinterest.deployservice.db.SingleResultSetHandlerFactory.java
public static <T> ResultSetHandler<List<T>> newListObjectHandler() { return new ResultSetHandler<List<T>>() { @Override/*from w w w . j a va 2 s.c om*/ public List<T> handle(ResultSet resultSet) throws SQLException { List<T> ret = new ArrayList<T>(); while (resultSet.next()) { ret.add((T) resultSet.getObject(1)); } return ret; } }; }
From source file:com.nortal.petit.core.util.SqlUtil.java
public static Long getLong(ResultSet rs, String columnLabel) throws SQLException { return getLong(rs.getObject(columnLabel)); }
From source file:jdbc.JdbcUtils.java
/** * Retrieve a JDBC column value from a ResultSet, using the most appropriate * value type. The returned value should be a detached value object, not * having any ties to the active ResultSet: in particular, it should not be * a Blob or Clob object but rather a byte array respectively String * representation.//from w w w . ja v a 2 s.c o m * <p> * Uses the <code>getObject(index)</code> method, but includes additional * "hacks" to get around Oracle 10g returning a non-standard object for its * TIMESTAMP datatype and a <code>java.sql.Date</code> for DATE columns * leaving out the time portion: These columns will explicitly be extracted * as standard <code>java.sql.Timestamp</code> object. * * @param rs * is the ResultSet holding the data * @param index * is the column index * @return the value object * @see java.sql.Blob * @see java.sql.Clob * @see java.sql.Timestamp * @see oracle.sql.TIMESTAMP */ public static Object getResultSetValue(ResultSet rs, int index) throws SQLException { Object obj = rs.getObject(index); if (obj instanceof Blob) { obj = rs.getBytes(index); } else if (obj instanceof Clob) { obj = rs.getString(index); } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) { obj = rs.getTimestamp(index); } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) { String metaDataClassName = rs.getMetaData().getColumnClassName(index); if ("java.sql.Timestamp".equals(metaDataClassName) || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) { obj = rs.getTimestamp(index); } else { obj = rs.getDate(index); } } else if (obj != null && obj instanceof Date) { if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) { obj = rs.getTimestamp(index); } } return obj; }
From source file:cit360.sandbox.BackEndMenu.java
public final static void connect() { Connection conn = null;/*from www .j a v a2s .c om*/ try { conn = DriverManager.getConnection("jdbc:mysql://localhost/cit361-sandbox?" + "user=root&password="); // Do something with the Connection } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } if (null != conn) { System.out.println("Connected to database!"); } else { System.out.println("Failed to make connection!"); } try { Statement stmt = conn.createStatement(); String query = "select * from movies ;"; //movies is the table name ResultSet rs = stmt.executeQuery(query); while (rs.next()) { String name = rs.getObject(2).toString(); String Start_Time = rs.getObject(3).toString(); System.out.println(name + ": " + Start_Time); //movies table has name and price columns } } catch (SQLException e) { for (Throwable ex : e) { System.err.println("Error occurred " + ex); } System.out.println("Error in fetching data"); } }