Created
September 28, 2012 14:20
-
-
Save franzwong/3800147 to your computer and use it in GitHub Desktop.
Sample for persisting CLOB with pure JDBC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.clob001; | |
import java.math.BigDecimal; | |
import java.nio.file.FileSystems; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.sql.Clob; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
public class DocumentDAO { | |
private Connection conn; | |
public DocumentDAO(Connection conn) { | |
this.conn = conn; | |
} | |
public void persistDocument(String name, String filePath) throws Exception { | |
System.out.println("Name: "+ name); | |
System.out.println("File path: " + filePath); | |
BigDecimal documentId = generateDocumentId(); | |
insertRecord(documentId, name); | |
String documentContent = getDocumentContentFromFile(filePath); | |
persistContent(documentId, documentContent); | |
} | |
private BigDecimal generateDocumentId() throws Exception { | |
BigDecimal documentId = null; | |
String seqSql = "SELECT DOCUMENT_ID_SEQ.NEXTVAL FROM DUAL"; | |
try (Statement statement = conn.createStatement()) { | |
try (ResultSet resultSet = statement.executeQuery(seqSql)) { | |
resultSet.next(); | |
documentId = resultSet.getBigDecimal(1); | |
System.out.println("Document id: " + documentId); | |
} | |
} | |
return documentId; | |
} | |
private void insertRecord(BigDecimal documentId, String name) throws Exception { | |
String insertSql = "INSERT INTO DOCUMENT (ID, NAME, CONTENT) VALUES (?, ?, EMPTY_CLOB())"; | |
try (PreparedStatement q = conn.prepareStatement(insertSql)) { | |
q.setBigDecimal(1, documentId); | |
q.setString(2, name); | |
q.executeUpdate(); | |
} | |
} | |
private String getDocumentContentFromFile(String filePath) throws Exception { | |
Path path = FileSystems.getDefault().getPath(filePath); | |
byte[] bytes = Files.readAllBytes(path); | |
String documentContent = new String(bytes, "utf-8"); | |
System.out.println("*********************"); | |
System.out.println("Document content:"); | |
System.out.println(documentContent); | |
System.out.println("*********************"); | |
return documentContent; | |
} | |
private void persistContent(BigDecimal documentId, String documentContent) throws Exception { | |
String selectSql = "SELECT CONTENT FROM DOCUMENT WHERE ID = ?"; | |
try (PreparedStatement q = conn.prepareStatement(selectSql)) { | |
q.setBigDecimal(1, documentId); | |
try (ResultSet resultSet = q.executeQuery()) { | |
resultSet.next(); | |
Clob clob = resultSet.getClob(1); | |
clob.setString(1, documentContent); | |
String updateSql = "UPDATE DOCUMENT SET CONTENT = ? WHERE ID = ?"; | |
try (PreparedStatement q2 = conn.prepareStatement(updateSql)) { | |
q2.setClob(1, clob); | |
q2.setBigDecimal(2, documentId); | |
q2.executeUpdate(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment