Use PreparedStatement.setAsciiStream() : Preparedstatement « Database « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Database » Preparedstatement 
20.13.6.Use PreparedStatement.setAsciiStream()Previous/Next
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
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[] argsthrows 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 = (intfile.length();
    InputStream stream = (InputStreamnew FileInputStream(file);

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

    // 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();
  }

  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";

    return DriverManager.getConnection(url, "sa""");
  }
}
20.13.Preparedstatement
20.13.1.Working with the Preparedstatement
20.13.2.Create a PreparedStatement object with two parameter markers
20.13.3.Check for a SQL Warning Using PreparedStatement
20.13.4.Create a Table Using PreparedStatement
20.13.5.Set the Number of Rows to Prefetch Using PreparedStatement
20.13.6.Use PreparedStatement.setAsciiStream()
20.13.7.Use PreparedStatement.setBigDecimal()
20.13.8.Use PreparedStatement.setBinaryStream()
20.13.9.Use PreparedStatement.setBoolean()
20.13.10.Use PreparedStatement's setByte(), setShort(), setInt(), and setLong()
20.13.11.Use PreparedStatement.setBytes()
20.13.12.Use PreparedStatement.setCharacterStream()
20.13.13.Set NULL
20.13.14.DELETE data in a table
20.13.15.Modify data in a table
20.13.16.Prepared Statement With Batch Update
20.13.17.Select Records Using Prepared Statement
20.13.18.Inserting Records using the Prepared Statement
20.13.19.Count Records using the Prepared Statement
20.13.20.Deleting Records using the Prepared Statement
20.13.21.Using the Prepared Statement Twice
20.13.22.Set string,ingeger,double and float example by using the Prepared Statement
20.13.23.Set byte, short and long data types by using the Prepared Statement
20.13.24.Prepared Statement Set Big Decimal
20.13.25.Set Date by using the Prepared Statement
20.13.26.Set Time by using the Prepared Statement
20.13.27.Set Timestamp by using the Prepared Statement
20.13.28.Rows affected when updating data in database table
20.13.29.Use PreparedStatement.setURL()
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.