如何从oracle中检索图像并在java框架中显示。请寄给我一个示例程序 我使用的是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 :(得分: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
已创建并添加到您的框架中。