java gui设计

java 代码
import javax.swing.* ;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.io.*;
public class TestJFrame implements ActionListener
{
 JTextArea textAreaOutput;
 JButton button;
 Connection conn 
= null;
 String tbname;
 String pathname;
 String blobname;
 
public static void main (String[] args)
 
{
 TestJFrame ttf
=new TestJFrame();
 ttf.createUI();
 }

 
public void createUI(){
  JFrame jf
=new JFrame("SQLBLOB");
  Container c
=jf.getContentPane();
  c.setLayout(
new FlowLayout(FlowLayout.LEFT,30,30));
  
//定义图标
  ImageIcon ii=new ImageIcon("main.gif");
  Image image
=ii.getImage();
  JPanel panel 
=new JPanel();
  jf.setIconImage(image);
  textAreaOutput 
= new JTextArea("select * from tablename"610);
  textAreaOutput.setSelectedTextColor(Color.RED);
  textAreaOutput.setLineWrap(
true);        //激活自动换行功能 
  textAreaOutput.setWrapStyleWord(true);            // 激活断行不断字功能

  jf.setExtendedState(JFrame.ICONIFIED);
  
//读取配置文件
   try{
       Properties ps
=new Properties();
     FileInputStream fis
= new FileInputStream("tbname.properties");
       ps.load(fis);
     fis.close();
     tbname
=ps.getProperty("tablename");
     pathname
=ps.getProperty("pname");
     blobname
=ps.getProperty("blobname");
     System.out.println(tbname
+":"+pathname+":"+blobname);
     }
catch (IOException e) {
   System.out.println(e.getLocalizedMessage());
  }
    
  
//选择数据库类型
  ButtonGroup bg=new ButtonGroup();
  JRadioButton sqlbutton
=new JRadioButton("Sqlserver");
  JRadioButton orabutton
=new JRadioButton("oracle");
  bg.add(sqlbutton);
  bg.add(orabutton);
  button
=new JButton("导出");
  JButton wButton
=new JButton("写入");
  button.addActionListener(
this);
  wButton.addActionListener(
this);
  sqlbutton.addActionListener(
this);
  orabutton.addActionListener(
this);
  
//控制窗口初始化大小
  jf.setSize(500,200);
  
//jf.setLocation(400,200);
  c.add(textAreaOutput);
  c.add(sqlbutton);
  c.add(orabutton);
  c.add(button);
  c.add(wButton);
  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jf.setVisible(
true);
  }

 
public void actionPerformed(ActionEvent ev){
  String s 
=ev.getActionCommand();
  
if(s.equals("导出")){
  
this.submit();
  }

  
if(s.equals("Sqlserver")){
  
try{
  conn
=getsqlConnection();
  }

  
catch (ClassNotFoundException e) {
   System.out.println(e.getLocalizedMessage());
  }
 catch (SQLException e) {
   System.out.println(e.getLocalizedMessage());
  }
 catch (IOException e) {
   System.out.println(e.getLocalizedMessage());
  }
    
  }

  
if(s.equals("oracle")){
  
try{
  conn
=getConnection();
  }

  
catch (ClassNotFoundException e) {
   System.out.println(e.getLocalizedMessage());
  }
 catch (SQLException e) {
   System.out.println(e.getLocalizedMessage());
  }
 catch (IOException e) {
   System.out.println(e.getLocalizedMessage());
  }
    
  }

  
if(s.equals("写入")){
     String fname
=textAreaOutput.getText();
       
this.writeBlob(tbname,fname);
      }

 }

 
public void submit(){
  String sql
=textAreaOutput.getText();
  
//System.out.println(sql);
  this.readBlob(sql);
 }


 
public Connection getConnection() throws ClassNotFoundException,IOException,
    SQLException 
{
    Properties ps
=new Properties();
    FileInputStream fis
= new FileInputStream("oracle.properties");
    ps.load(fis);
    fis.close();
    String url
=ps.getProperty("db_url");
    String user
=ps.getProperty("username");
    String pwd
=ps.getProperty("password");
    String driver 
= "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver);
    
return DriverManager.getConnection(url, user, pwd);
 }

 
//加载sqlserver驱动mssqlserver.jar、msutil.jar和 msbase.jar
 public Connection getsqlConnection() throws ClassNotFoundException,IOException,
     SQLException 
{
     Properties ps
=new Properties();
     FileInputStream fis
= new FileInputStream("sqlserver.properties");
     ps.load(fis);
     fis.close();
     String url
=ps.getProperty("db_url");
     String user
=ps.getProperty("username");
     String pwd
=ps.getProperty("password");
     
//采用jtds
    
// String driver = "net.sourceforge.jtds.jdbc.Driver"
     String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
     Class.forName(driver);
     
return DriverManager.getConnection(url, user, pwd);
 }

  
private void readBlob(String sql ){
  
try {
   
//conn = getConnection();
   
//String sql = "select * from  "+tablename+" where "+columname+"=?";   
   
//PreparedStatement stmt =  conn.prepareStatement(sql);
   Statement stmt = conn.createStatement();
   
//stmt.setString(1,"d:\\身份证.jpg"); 
   System.out.println("你输入的sql语句为:"+sql);
   ResultSet rs 
= stmt.executeQuery(sql);
   
int tempLen = 0;
   
int amount = 0;
   
while (rs.next()){
   String colVal
=rs.getString(pathname);
   colVal
=colVal.replaceAll("\\\\","\\\\\\\\");
   colVal
=colVal.replaceAll(" ","");
   
//System.out.println(colVal);
   File file=new File(colVal);
   
byte[] buffer = new byte[1024];
   OutputStream out 
= new FileOutputStream(file);
   Blob blob 
= rs.getBlob(blobname);
   InputStream in 
= blob.getBinaryStream();
   
while ((tempLen = in.read(buffer)) != -1{
   out.write(buffer, 
0, tempLen);
   amount 
+= tempLen;
   
//System.out.println("已经读出并写:" + amount + " 字节");
   }
  
   System.out.println(colVal
+"写出完毕");
   out.flush();
   out.close();
   in.close();
   }

   rs.close();
   stmt.close();
   conn.close();
  }
 catch (SQLException e) {
   System.out.println(e.getLocalizedMessage());
  }
 catch (IOException e) {
   System.out.println(e.getLocalizedMessage());
  }
    catch (NullPointerException e) {
    System.out.println(
"无记录存在。核实再试");
    }
 
  
finally {
   
try {
    
if (conn != null)
     conn.close();
   }
 catch (SQLException e) {
    System.out.println(e.getLocalizedMessage());
   }

  }

 }

  
private void writeBlob(String table_name,String file_name) {
   
try {
   conn.setAutoCommit(
false);
   String sql 
= "insert into "+table_name+" ("+pathname+","+blobname+") values(?,?)";   
   PreparedStatement stmt 
=  conn.prepareStatement(sql);
   System.out.println(file_name);
   stmt.setString(
1,file_name);
   File file
=new File(file_name);
   FileInputStream fis
=new FileInputStream(file);
   stmt.setBinaryStream(
2,fis,fis.available());
   stmt.executeUpdate();
   conn.commit();
   stmt.close();
   fis.close();

  }
catch (SQLException e) {
   
try {
    conn.rollback();
   }
 catch (SQLException e1) {
    System.out.println(e1.getLocalizedMessage());
   }

   System.out.println(e.getLocalizedMessage());
  }
 catch (IOException e) {
   System.out.println(e.getLocalizedMessage());
  }
 finally {
   
try {
    
if (conn != null)
     conn.close();
   }
 catch (SQLException e) {
    System.out.println(e.getLocalizedMessage());
   }

  }

 }

}
tbname.properties文件
tablename=bloblib
#定义路径字段
pname=pathname
#定义图片blob字段
blobname=photo
oracle.properties文件
db_url=jdbc:sqlserver://127.0.0.1:1433;Database=testdb
#db_url=jdbc:jtds:sqlserver://127.0.0.1:1433/testdb
username=sa
password=sa
oracle.properties文件
db_url=jdbc:oracle:thin:@127.0.0.1:1521:tytx
#db_url=jdbc:jtds:sqlserver://127.0.0.1:1433/testdb
username=scott
password=oracle


posted on 2013-10-11 22:24 snowhill 阅读(154) 评论(0)  编辑 收藏 引用 所属分类: java


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

公告

又一年...........

留言簿(3)

随笔分类(13)

文章分类(131)

文章档案(124)

c++

java

linux

oracle

常用软件

其他

网络配置

系统安全

音乐

搜索

最新评论

阅读排行榜