2014-03-13 48 views
0

如何從oracle檢索圖像並在java框架中顯示。請給我一個示例程序 我使用的是Oracle 10G快速版。如何從oracle中檢索圖像並在java框架中顯示

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.sql.*; 

@SuppressWarnings("serial") 
public class Search extends JFrame implements ActionListener { 

//Initializing Components 
    private JLabel lb, lb1, lb2, lb3, lb4, lb5, lb6; 
    private JTextField tf1, tf2, tf3, tf4, tf5; 
    private byte s4; 
    private JButton btn; 
    private Connection con; 

    Search() {//Creating Constructor for initializing JFrame components 
     //Providing Title 
     super("Fetching Student Information"); 
     lb5 = new JLabel("Enter the Employee id:"); 
     lb5.setBounds(20, 20, 100, 20); 
     tf5 = new JTextField(20); 
     tf5.setBounds(130, 20, 200, 20); 
     btn = new JButton("Submit"); 
     btn.setBounds(50, 50, 100, 20); 
     btn.addActionListener(this); 
     lb = new JLabel("Fetching Employee Information From Database"); 
     lb.setBounds(30, 80, 450, 30); 
     lb.setForeground(Color.red); 
     lb.setFont(new Font("Serif", Font.BOLD, 20)); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     lb1 = new JLabel("EmployeeName:"); 
     lb1.setBounds(20, 120, 100, 20); 
     tf1 = new JTextField(50); 
     tf1.setBounds(130, 120, 200, 20); 
     lb2 = new JLabel("Gender:"); 
     lb2.setBounds(20, 150, 100, 20); 
     tf2 = new JTextField(100); 
     tf2.setBounds(130, 150, 200, 20); 
     lb3 = new JLabel("DOB:"); 
     lb3.setBounds(20, 180, 100, 20); 
     tf3 = new JTextField(50); 
     tf3.setBounds(130, 180, 200, 20); 
     lb4 = new JLabel("DOJ:"); 
     lb4.setBounds(20, 210, 100, 20); 
     tf4 = new JTextField(50); 
     tf4.setBounds(130, 210, 100, 20); 
     lb6 = new JLabel("Photo:"); 
     lb6.setBounds(550, 10, 100, 100); 
     setLayout(null); 
     setVisible(true); 
     //Add components to the JFrame 
     add(lb5); 
     add(tf5); 
     add(btn); 
     add(lb); 
     add(lb1); 
     add(tf1); 
     add(lb2); 
     add(tf2); 
     add(lb3); 
     add(tf3); 
     add(lb4); 
     add(tf4); 
     add(lb6); 
     //Set TextField Editable False 
     tf1.setEditable(false); 
     tf2.setEditable(false); 
     tf3.setEditable(false); 
     tf4.setEditable(false); 
    } 

    public void actionPerformed(ActionEvent e) { 
     //Create DataBase Coonection and Fetching Records 
     try { 
      String str = tf5.getText(); 
      String url = "jdbc:oracle:thin:@localhost:1521:XE"; 
      String u = "ems2"; 
      String p = "ems2"; 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      con = DriverManager.getConnection(url, u, p); 
      PreparedStatement st = con.prepareStatement("select * from employee where emp_id=?",); 
      st.setString(1, str); 
      //Excuting Query 
      ResultSet rs = st.executeQuery(); 
      if (rs.next()) { 
       String s = rs.getString("employeename"); 
       String s1 = rs.getString("dob"); 
       String s2 = rs.getString("gender"); 
       String s3 = rs.getString("doj"); 
       //Sets Records in TextFields. 
       tf1.setText(s); 
       tf2.setText(s2); 
       tf3.setText(s1); 
       tf4.setText(s3); 
      } else { 
       JOptionPane.showMessageDialog(null, "please check your employeeID"); 
      } 
      PreparedStatement ps1 = con.prepareStatement("select * from photo where photoid =?"); 
      ResultSet rs1 = ps1.executeQuery(); 
      while (rs.next()) {//now on 1st row 
       Blob b = rs.getBlob(2);//2 means 2nd column data 
       byte barr[] = b.getBytes(1, (int) b.length());//1 means first image 
      } 
      //Create Exception Handler 
     } catch (Exception ex) { 
      System.out.println(ex); 
     } 
    } 

//Running Constructor 
    public static void main(String args[]) { 
     new Search(); 
    } 
} 
+0

嗨,這是我的程序,我必須把照片在我的貴 – prasanth

+0

但問題是我已經將照片存儲在另一張名爲照片的表,所以我試圖在這個程序中使用準備好的語句兩次 – prasanth

+0

這更好。我正在刪除我的downvote。 –

回答

2

調用查詢來檢索圖像並使用類似的東西。

byte[] imgData = null; 
if (rs.next()) 
{ 
    Blob img = rs.getBlob(1); 
    imgData = img.getBytes(1,(int)img.length()); 
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(imgData)); 
    yourJLabelInstance.setIcon(new ImageIcon(image)); 
} 

yourJLabelInstance是創建並添加到您的框架JLabel

+0

當圖像數據大於'Integer.MAX_VALUE'時會發生什麼? – MadProgrammer

+0

你說得對。這不完美:-)這只是一個例子,而不是準備好工作代碼。實際上,這幾乎不是圖像的尺寸大於MAX int。 – StanislavL

+0

同意,只是pocking;) – MadProgrammer

相關問題