oracle大文件插入

package com;

import java.sql.Blob;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.sql.*;
import oracle.sql.BLOB;
import javax.swing.JFileChooser;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.filechooser.FileFilter;

public class ConnOracle

{
public ConnOracle(){
MyFrame win = new MyFrame();
win.validate();
//win.pack();
win.setLocation(200,200);
win.setVisible(true);
}
public static void main(String[] args) {
new ConnOracle();
}
}

class MyFrame extends JFrame{
JPanel contentPane;
JButton bt_choose = new JButton();
JButton bt_exit = new JButton();
int result;
public MyFrame(){
this.setSize(new Dimension(200,80));
this.setTitle("WELCOME TO YOU");
bt_choose.addActionListener(new MyActionListener(this));
bt_choose.setText("选择文件");
bt_exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
} );
bt_exit.setText("退出");
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(bt_choose);
contentPane.add(bt_exit);
}
public void choose_file(){

String pathname =null;
String filename = null;
JFileChooser filechooser = new JFileChooser();
filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
JasperFileFilter jasperFileFilter = new JasperFileFilter("jasper");
filechooser.setFileFilter(jasperFileFilter);
result = filechooser.showOpenDialog(this);

if(result==JFileChooser.CANCEL_OPTION){
System.out.println("没选择文件!");
}else{

try{
File openfile = filechooser.getSelectedFile();
filename = openfile.getName();
pathname = openfile.getPath();

System.out.println("filename="+filename);
System.out.println("pathname="+pathname);
if(!(openfile.isFile())){
System.out.println("没有此文件!");
return ;
}
int index = filename.lastIndexOf('.');
if (index > 0 && index < filename.length() - 1) {
String extension = filename.substring(index + 1).toLowerCase();
if (!(extension.equals("jasper"))){
System.out.println("您选择的文件不正确!");
return ;
}
}
filename = filename.substring(0,index);
System.out.println("filename="+filename);
}catch(Exception e){
System.out.println("error="+e);
}

try{
int report_id= 0;
String report_name = filename;
String flag = null;

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.17:1521:gzscdb","klxuser","5583");

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from reports where report_name='"+report_name+"'");
if(rs.next()){
int i = JOptionPane.showConfirmDialog(this,"是否要更新!","window",JOptionPane.YES_NO_OPTION);
if(i == 1){
System.out.println("不更新!");
return ;
}
flag = "up";
}else{
flag = "insert" ;
}
rs.close();
stmt.close();

Statement stmt_max = conn.createStatement();
ResultSet rs_max = stmt_max.executeQuery("SELECT MAX(REPORT_ID) + 1 AS REPORT_ID FROM REPORTS");
if(rs_max.next()){
report_id = rs_max.getInt("report_id");
}
if(report_id==0){
report_id = 1;
}
System.out.println("report_id="+report_id);
rs_max.close();
stmt_max.close();

Statement stmt_end = conn.createStatement();
if(flag.equals("insert")){
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try{
System.out.println( "1");
stmt_end.executeUpdate("INSERT INTO REPORTS VALUES ('"+report_id+"', '"+report_name+"',EMPTY_BLOB())");
System.out.println( "2");
ResultSet rs_end = stmt_end.executeQuery("SELECT REPORT_DATA FROM REPORTS WHERE REPORT_ID='"+report_id+"' FOR UPDATE");
System.out.println( "3");
while (rs_end.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs_end.getBlob("REPORT_DATA");
File infile = new File(pathname );
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
conn.commit();
} catch (Exception ex) {
conn.rollback();
throw ex;
}
conn.setAutoCommit(defaultCommit);
System.out.println( "insert ok!");
}else if(flag.equals("up")){
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
stmt_end.executeUpdate("UPDATE REPORTS SET REPORT_DATA=EMPTY_BLOB() WHERE REPORT_NAME='"+report_name+"'");
ResultSet rs_end = stmt_end.executeQuery("SELECT REPORT_DATA FROM REPORTS WHERE REPORT_NAME='"+report_name+"' FOR UPDATE");
while (rs_end.next()) {
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs_end.getBlob("REPORT_DATA");
File infile = new File(pathname );
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
conn.commit();
} catch (Exception ex) {
conn.rollback();
throw ex;
}
conn.setAutoCommit(defaultCommit);
System.out.println( "update ok!");
}
stmt_end.close();
conn.close();
System.out.println( "all ok");
}catch (Exception e){
System.out.println("error+"+e);
}
}
}
protected void processWindowEvent(WindowEvent e){
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
}

class MyActionListener implements ActionListener{
MyFrame myframe;
public MyActionListener(MyFrame frame){
myframe = frame;
}
public void actionPerformed(ActionEvent e){
myframe.choose_file();
}
}

class JasperFileFilter extends FileFilter {
String ext; // 文件扩展名
JasperFileFilter(String ext) {
this.ext = ext;
}

public boolean accept(File file) {
if (file.isDirectory()) // 是目录,打开
return true;

String fileName = file.getName();
int index = fileName.lastIndexOf('.');
if (index > 0 && index < fileName.length() - 1) {
String extension = fileName.substring(index + 1).toLowerCase();
if (extension.equals(ext))
return true;
}
return false;
}
public String getDescription() { // 返回描述文件的说明字符串
if (ext.equals("jasper"))
return "jasper File (*.jasper)";
return "";
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值