package com.sap;
import java.io.;
import java.sql.;
/**
- Created by zzhang on 12/7/2018.
*/
public class OracleJdbc {
private static Connection conn = null;
private static String driver = “oracle.jdbc.driver.OracleDriver”;
private static String url = “jdbc:oracle:thin:@localhost:1521:orcl”;
private static String username = “outln”;
private static String password = “aE3407449”;
private static synchronized Connection getConn() throws SQLException {
if(conn == null){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
}
conn.setAutoCommit(false);
return conn;
}
public void insertTable() throws SQLException, IOException {
//String sql = “insert into test (id,blobvalue) values (’”+uuid+ “’,empty_blob())”;
String sql=“insert into INTELLIGENTINVOICEINVPDF values(103,‘zhangsan’,empty_blob())”;
conn=getConn();
Statement st = conn.createStatement();
st.executeUpdate(sql);
ResultSet rs = st.executeQuery(“select PDF from INTELLIGENTINVOICEINVPDF where id=103 for update”);
if (rs.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);
OutputStream outStream = blob.getBinaryOutputStream();
File file = new File("/201808.pdf");
InputStream fin = new FileInputStream(file);
//将输入流写到输出流
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ( (len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
//blob.putBytes(1,b);
}
//依次关闭(注意顺序)
fin.close();
outStream.flush();
outStream.close();
conn.commit();
conn.close();
}
}
public void queryBlob(String sql) throws SQLException, IOException {
ReadPDFInvoice pasePdf = new ReadPDFInvoice();
conn=getConn();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
if (rs.next()) {
java.sql.Blob blob = rs.getBlob(1);
InputStream ins = blob.getBinaryStream();
//用文件模拟输出流
File file = new File("…/output.PDF");
OutputStream fout = new FileOutputStream(file);
//下面将BLOB数据写入文件
byte[] b = new byte[1024];
int len = 0;
while ((len = ins.read(b)) != -1) {
fout.write(b, 0, len);
}
//依次关闭
fout.close();
ins.close();
conn.commit();
conn.close();
pasePdf.parsePdf(file);
file.delete();
}
}
public void query(String sql, boolean isSelect) throws SQLException{
PreparedStatement pstmt;
try {
pstmt = getConn().prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String name = rs.getString(“name”);
System.out.println(name);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void query(String sql) throws SQLException{
PreparedStatement pstmt;
pstmt = getConn().prepareStatement(sql);
pstmt.execute();
pstmt.close();
}
public void close(){
try {
getConn().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.sap;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
package com.sap;
import java.awt.Rectangle;
import java.io.*;
//import java.io.BufferedWriter;
//import java.io.FileWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripperByArea;
class ReadPDFInvoice {
ReadPDFInvoice()
{
}
public void parsePdf(File file) throws IOException
{
try {
PDDocument document = PDDocument.load(file);
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition( true );
// Rectangle rect = new Rectangle( 40, 280, 280, 50 );
Rectangle rect = new Rectangle( 10, 10, 600, 600 );
stripper.addRegion( “class1”, rect );
PDPage firstPage = document.getPage(0);
stripper.extractRegions( firstPage );
System.out.println( “Text in the area:” + rect );
String sOutput = stripper.getTextForRegion( “class1” );
System.out.println( sOutput );
} catch (Exception e) {
e.printStackTrace();
}
}
}