List of usage examples for java.sql PreparedStatement setCharacterStream
void setCharacterStream(int parameterIndex, java.io.Reader reader) throws SQLException;
Reader
object. From source file:Main.java
public static void main(String[] args) throws Exception { Connection con = DriverManager.getConnection("jdbc:h2:mem:"); Statement s = con.createStatement(); s.execute("CREATE TABLE Table1 (Column1 CLOB)"); InputStream is = new FileInputStream("data.txt"); Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1); PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)"); ps.setCharacterStream(1, rdr); ps.executeUpdate();/*from w ww .j ava 2 s . c om*/ ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1"); int rowNumber = 0; while (rs.next()) { String str = rs.getString("Column1"); System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length())); } rs.close(); con.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (Id int, b CLOB);"); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)"); File file = new File("c:/Java_Dev/data.txt"); FileReader reader = new FileReader(file); pstmt.setCharacterStream(1, reader); pstmt.execute();//from w w w . j a va2 s . co m ResultSet resultSet = pstmt.executeQuery("select b from survey "); File data = new File("C:\\a.txt"); Reader dataReader = resultSet.getCharacterStream(1); FileWriter writer = new FileWriter(data); char[] buffer = new char[1]; while (dataReader.read(buffer) > 0) { writer.write(buffer); } writer.close(); reader.close(); st.close(); conn.close(); }
From source file:com.feedzai.commons.sql.abstraction.engine.impl.H2Engine.java
@Override protected int entityToPreparedStatement(final DbEntity entity, final PreparedStatement ps, final EntityEntry entry, final boolean useAutoInc) throws DatabaseEngineException { int i = 1;//from w w w . ja va 2s.c om for (DbColumn column : entity.getColumns()) { if ((column.isAutoInc() && useAutoInc)) { continue; } try { final Object val; if (column.isDefaultValueSet() && !entry.containsKey(column.getName())) { val = column.getDefaultValue().getConstant(); } else { val = entry.get(column.getName()); } switch (column.getDbColumnType()) { case BLOB: ps.setBytes(i, objectToArray(val)); break; case CLOB: if (val == null) { ps.setNull(i, Types.CLOB); break; } if (val instanceof String) { StringReader sr = new StringReader((String) val); ps.setCharacterStream(i, sr); } else { throw new DatabaseEngineException("Cannot convert " + val.getClass().getSimpleName() + " to String. CLOB columns only accept Strings."); } break; default: ps.setObject(i, val); } } catch (Exception ex) { throw new DatabaseEngineException("Error while mapping variables to database", ex); } i++; } return i - 1; }
From source file:no.polaric.aprsdb.MyDBSession.java
public long addFileObject(InputStream data) throws java.sql.SQLException { PreparedStatement stmt = getCon() .prepareStatement(" INSERT INTO \"FileObject\" (data)" + " VALUES (?) RETURNING id"); stmt.setCharacterStream(1, new InputStreamReader(data)); ResultSet rs = stmt.executeQuery(); rs.next();//from www.ja v a 2 s.co m return rs.getLong("id"); }
From source file:nz.co.gregs.dbvolution.actions.DBUpdateLargeObjects.java
private void setUsingCharacterStream(DBDefinition defn, DBRow row, final String col, final DBLargeObject largeObject, DBDatabase db, DBStatement statement) throws SQLException { String sqlString = defn.beginUpdateLine() + defn.formatTableName(row) + defn.beginSetClause() + defn.formatColumnName(col) + defn.getEqualsComparator() + defn.getPreparedVariableSymbol() + defn.beginWhereClause() + defn.formatColumnName(row.getPrimaryKeyColumnName()) + defn.getEqualsComparator() + row.getPrimaryKey().toSQLString(db) + defn.endSQLStatement(); // db.printSQLIfRequested(sqlString); log.info(sqlString);// ww w . j ava 2 s. c o m PreparedStatement prep = statement.getConnection().prepareStatement(sqlString); try { prep.setCharacterStream(1, new InputStreamReader(largeObject.getInputStream())); prep.execute(); } finally { prep.close(); } }
From source file:org.batoo.jpa.jdbc.dbutils.QueryRunner.java
/** * Fill the <code>PreparedStatement</code> replacement parameters with the given objects. * //from w w w . j a v a 2 s . c o m * @param statement * PreparedStatement to fill * @param params * Query replacement parameters; <code>null</code> is a valid value to pass in. * @throws SQLException * if a database access error occurs */ private void fillStatement(PreparedStatement statement, Object... params) throws SQLException { // use local variable for performance boolean pmdKnownBroken = this.pmdKnownBroken; ParameterMetaData pmd = this.pmd; final boolean hasLob = this.hasLob; if (pmdKnownBroken) { ((PreparedStatementProxy) statement).setParamCount(params.length); } else { ((PreparedStatementProxy) statement).setParamCount(-1); } // if the jdbc adaptor wants to modify the parameters we let it do it its own way final JdbcAdaptor jdbcAdaptor = this.jdbcAdaptor; if ((jdbcAdaptor != null) && jdbcAdaptor.modifiesParameters()) { pmd = this.pmd = statement.getParameterMetaData(); jdbcAdaptor.modifyParameters(pmd, params); } for (int i = 0; i < params.length; i++) { final Object param = params[i]; if ((param != null) && (param != Void.TYPE)) { if (hasLob && (param instanceof Clob)) { if (this.jdbcAdaptor instanceof OracleAdaptor) { statement.setCharacterStream(i + 1, ((Clob) param).getCharacterStream()); } else { statement.setClob(i + 1, (Clob) param); } } else if (hasLob && (param instanceof Blob)) { if (this.jdbcAdaptor instanceof OracleAdaptor) { statement.setBinaryStream(i + 1, ((Blob) param).getBinaryStream()); } else { statement.setBlob(i + 1, (Blob) param); } } else { statement.setObject(i + 1, param); } } else { if (!pmdKnownBroken && (pmd == null)) { pmd = this.pmd = statement.getParameterMetaData(); } // VARCHAR works with many drivers regardless of the actual column type. // Oddly, NULL and OTHER don't work with Oracle's drivers. int sqlType = Types.VARCHAR; if (!pmdKnownBroken) { try { sqlType = pmd.getParameterType(i + 1); } catch (final SQLException e) { pmdKnownBroken = this.pmdKnownBroken = true; } } if (param != Void.TYPE) { statement.setNull(i + 1, sqlType); } } } }