Example usage for java.sql ResultSet getBytes

List of usage examples for java.sql ResultSet getBytes

Introduction

In this page you can find the example usage for java.sql ResultSet getBytes.

Prototype

byte[] getBytes(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a byte array in the Java programming language.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("create table survey (id int, name BINARY );");
    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();
    pstmt.setBytes(1, binaryData);// w w w .j  a  v a2s .co  m
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(rs.getBytes("name").length + "   ");
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("create table survey (id int, name BINARY );");
    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();
    pstmt.setBytes(1, binaryData);//from  w  w  w . j a  v a  2s .  c om
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(rs.getBytes(2).length + "   ");
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY);");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setBytes(1, "asdfasdf".getBytes());
    pstmt.executeUpdate();/*from   www . ja  va 2 s.  co m*/

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getBytes(2));
    }

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY );");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    // create some binary data
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();

    // set value for the prepared statement
    pstmt.setBytes(1, binaryData);/*from   w ww .  j  a va2  s  .com*/

    // insert the data
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(rs.getBytes(2).length + "   ");
    }

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY);");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    File file = new File("yourFileName.txt");
    long fileLength = file.length();
    Reader fileReader = (Reader) new BufferedReader(new FileReader(file));

    pstmt.setCharacterStream(1, fileReader, (int) fileLength);

    int rowCount = pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getBytes(2));
    }//  w  w  w .j  a  va2  s.  c  om

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//  w  w  w.j ava 2s.c  o m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    String sql = "INSERT INTO mysql_all_table (col_binarystream) VALUES(?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    byte[] buffer = "some data".getBytes();
    pstmt.setBytes(1, buffer);
    pstmt.executeUpdate();
    pstmt.close();

    Statement stmt = connection.createStatement();
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM mysql_all_table");
    while (resultSet.next()) {
        byte[] bytes = resultSet.getBytes("col_binarystream");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY );");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    // create some binary data
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();

    // set value for the prepared statement
    pstmt.setBytes(1, binaryData);// w  ww  .j  a v  a  2 s.  c  o  m

    // insert the data
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(new String(rs.getBytes(2)));
    }

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY );");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    // prepare text stream
    File file = new File("yourFileName.txt");
    int fileLength = (int) file.length();
    InputStream stream = (InputStream) new FileInputStream(file);

    pstmt.setString(1, "001");
    pstmt.setAsciiStream(2, stream, fileLength);

    // insert the data
    pstmt.executeUpdate();/*from   www.j  a  va  2s. c  o  m*/

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(new String(rs.getBytes(2)));
    }

    rs.close();
    stmt.close();
    conn.close();
}

From source file:DemoDisplayBinaryDataFromDatabase.java

public static void main(String args[]) throws Exception {
    Connection conn = null;/*  w ww. ja v  a  2s.  c om*/
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT raw_column, long_raw_column FROM binary_table WHERE id = ?";
    try {
        conn = getConnection();
        Object[] results = new Object[2];
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        rs = pstmt.executeQuery();
        rs.next();
        // materialize binary data onto client
        results[0] = rs.getBytes("RAW_COLUMN");
        results[1] = rs.getBytes("LONG_RAW_COLUMN");
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:org.nuxeo.App.java

public static void main(String[] args) throws SQLException, IOException {

    Properties prop = readProperties();
    String user = prop.getProperty("user");
    String password = prop.getProperty("password");
    String connectionURL = prop.getProperty("url");
    String driver = prop.getProperty("driver");
    String query = prop.getProperty("query");

    log.info("Connect to:" + connectionURL + " from " + getHostName());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(baos);
    final ConsoleReporter reporter = new ConsoleReporter(printStream);

    Connection conn = null;//from   w ww.  j av  a 2s  .c o m
    PreparedStatement ps = null;
    ResultSet rs = null;
    TimerContext tc = null;
    int repeat = Integer.valueOf(System.getProperty(REPEAT_KEY, DEFAULT_REPEAT)).intValue();

    log.info("Submiting " + repeat + " queries: " + query);
    try {
        Class.forName(driver);
        tc = connTimer.time();
        conn = DriverManager.getConnection(connectionURL, user, password);
        tc.stop();
        ps = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        int paramCount = countOccurrences(query, '?');
        for (int i = 1; i <= paramCount; i++) {
            String key = "p" + i;
            String param = prop.getProperty(key);
            if (param == null) {
                break;
            }
            log.info(key + " = " + param);
            String type = "object";
            if (param.contains(":")) {
                type = param.split(":", 2)[0];
                param = param.split(":", 2)[1];
            }
            if (type.equalsIgnoreCase("object")) {
                ps.setObject(i, (Object) param);
            } else if (type.equalsIgnoreCase("string")) {
                ps.setString(i, param);
            } else if (type.equalsIgnoreCase("nstring")) {
                ps.setNString(i, param);
            } else {
                log.warn("Unknown type " + type + " use setObject");
                ps.setObject(i, (Object) param);
            }
        }

        int rows = 0;
        int bytes = 0;

        for (int i = 0; i < repeat; i++) {
            tc = execTimer.time();
            rs = ps.executeQuery();
            tc.stop();
            tc = fetchingTimer.time();
            ResultSetMetaData rsmd = rs.getMetaData();
            int cols = rsmd.getColumnCount();
            while (rs.next()) {
                rows++;
                for (int c = 1; c <= cols; c++) {
                    bytes += rs.getBytes(1).length;
                }
            }
            rs.close();
            tc.stop();
            // don't stress too much
            Thread.sleep((int) (Math.random() * 100));
        }
        log.info("Fetched rows: " + rows + ", total bytes: " + bytes + ", bytes/rows: "
                + ((float) bytes) / rows);

    } catch (SQLException e) {
        log.error(e.getMessage(), e);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
    reporter.run();
    try {
        String content = baos.toString("ISO-8859-1");
        log.info(content);
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    }

}