Java ResultSet.getAsciiStream(int columnIndex)

Syntax

ResultSet.getAsciiStream(int columnIndex) has the following syntax.

InputStream getAsciiStream(int columnIndex)    throws SQLException

Example

In the following code shows how to use ResultSet.getAsciiStream(int columnIndex) method.


// ww  w  .j  a v a  2  s. com

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {

  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:yourdatabasename";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";

      FileInputStream fis = new FileInputStream("sometextfile.txt");

      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url, user, password);
      Statement createTable = connection.createStatement();
      createTable.executeUpdate("CREATE TABLE source_code (name char(20), source LONGTEXT)");
      String ins = "INSERT INTO source_code VALUES(?,?)";
      PreparedStatement statement = connection.prepareStatement(ins);

      statement.setString(1, "TryInputStream2");
      statement.setAsciiStream(2, fis, fis.available());

      int rowsUpdated = statement.executeUpdate();
      System.out.println("Rows affected: " + rowsUpdated);
      Statement getCode = connection.createStatement();
      ResultSet theCode = getCode.executeQuery("SELECT name,source FROM source_code");
      BufferedReader reader = null;
      String input = null;

      while (theCode.next()) {
        reader = new BufferedReader(new InputStreamReader(theCode.getAsciiStream(2)));
        while ((input = reader.readLine()) != null) {
          System.out.println(input);
        }
      }
      connection.close();
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}




















Home »
  Java Tutorial »
    java.sql »




DatabaseMetaData
ParameterMetaData
PreparedStatement
ResultSet
Timestamp