2014-03-13 2 views
0

오라클에서 이미지를 검색하고 자바 프레임에 디스플레이하는 방법. 나에게 샘플 프로그램을 보내주십시오. 나는 Oracle 10G 익스프레스 에디션을 사용하고 있습니다.오라클에서 이미지를 검색하고 자바 프레임에 디스플레이하는 방법

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

안녕하세요 이것은 내 gui에 사진을 가져와야하는 프로그램입니다 – 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

관련 문제