如何从oracle中检索图像并在java框架中显示
如何从oracle检索图像并在java框架中显示。请寄给我一个样本程序 我正在使用oracle 10G express edition如何从oracle中检索图像并在java框架中显示,java,sql,oracle,swing,Java,Sql,Oracle,Swing,如何从oracle检索图像并在java框架中显示。请寄给我一个样本程序 我正在使用oracle 10G express edition import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.sql.*; @SuppressWarnings("serial") public class Search extends JFrame implements ActionListener { //In
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();
}
}
调用一个查询来检索图像,并使用如下内容
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
。您好,这是我的程序,我必须将照片放在GUI中,但问题是我已将照片存储在另一个名为photo的表中,因此我尝试在该程序中使用准备好的语句两次。这样更好。我要取消我的反对票。你的问题太广泛了。图像如何存储在oracle数据库中?斑点?哪个表/字段?不要期望工作代码。请描述您遇到的具体问题。我想知道如何从同一个jdbc程序中执行不同表的多个select语句。非常感谢:)当图像数据大于整数.MAX_值时会发生什么情况?你是对的。它并不完美:-)它只是一个示例,而不是现成的代码。事实上,图像的大小很难超过MAX int。同意,只是麻点;)hi@StanislavL如何在选择位置执行联接query@prasanth我不明白这个问题:(