Java PreparedStatement.execute()
Syntax
PreparedStatement.execute() has the following syntax.
boolean execute() throws SQLException
Example
In the following code shows how to use PreparedStatement.execute() method.
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
/*from w w w. j a v a2 s . com*/
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 BLOB);");
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");
File file = new File("blob.txt");
FileInputStream fis = new FileInputStream(file);
pstmt.setBinaryStream(1, fis, (int) file.length());
pstmt.execute();
fis.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", "");
}
}