Blob type data insert
In this chapter you will learn:
Insert Blob data
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
/* j ava 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 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", "");
}
}
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » SQL Data Types