Java PreparedStatement .setCharacterStream (int parameterIndex, Reader reader)
Syntax
PreparedStatement.setCharacterStream(int parameterIndex, Reader reader) has the following syntax.
void setCharacterStream(int parameterIndex, Reader reader) throws SQLException
Example
In the following code shows how to use PreparedStatement.setCharacterStream(int parameterIndex, Reader reader) method.
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
/* w w w . j a v a 2s .c o m*/
public class Main {
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();
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();
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}