我有一个包含每个人照片的数据库。我有一个课程,显示人们的照片。当类首先运行时,它会在本地存储foto,然后另一个方法接受它并显示它。问题是该课程第一次正常工作!每个其他名称选择显示第一张图片。请帮我确定一下这发生在哪里!
这是我的班级:
public class Profile extends JFrame {
int x;
JLabel label;
Image img;
ImageIcon pic;
JPanel panel;
private JTextPane namepanel = new JTextPane();
private JLabel namelabel = new JLabel();
private JLabel lastnamelabel = new JLabel();
private JTextPane lastnamepanel = new JTextPane();
ResultSet resultSet;
private JButton add = new JButton();
private JButton remove = new JButton();
public Profile() {
try {
//getData(x);
//showImage();
//jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getData(int p_id) throws Exception {
Login login = new Login();
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
String username = login.getUsername();
String password = login.getPassword();
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
System.out.println("At the profile class id = " + p_id);
String sql = "SELECT foto, name, surname FROM criminals WHERE id = " + p_id;
System.out.println(sql);
PreparedStatement stmt = conn.prepareStatement(sql);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
namepanel.setText(resultSet.getString(2));
lastnamepanel.setText(resultSet.getString(3));
File image = new File("java.jpg");
FileOutputStream fos = new FileOutputStream(image);
byte[] buffer = new byte[256];
// Get the binary stream of our BLOB data
InputStream is = resultSet.getBinaryStream(1);
int bytes = 0;
while ((bytes = is.read(buffer)) > 0) {
fos.write(buffer, 0, bytes);
}
showImage();
fos.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
public void showImage() throws Exception {
this.getContentPane().removeAll();
img = null;
img = new ImageIcon("java.jpg").getImage();
pic = null;
pic = new ImageIcon(img);
label = new JLabel("", pic, JLabel.CENTER);
panel = new JPanel(new BorderLayout());
panel.setBounds(new Rectangle(0, 0, 340, 310));
namepanel.setBounds(new Rectangle(340, 30, 295, 35));
namelabel.setText("Name");
namelabel.setBounds(new Rectangle(340, 0, 295, 30));
lastnamelabel.setText("Surname");
lastnamelabel.setBounds(new Rectangle(340, 65, 295, 30));
lastnamepanel.setBounds(new Rectangle(340, 105, 295, 35));
add.setText("add");
add.setBounds(new Rectangle(440, 175, 255, 40));
remove.setText("remove");
remove.setBounds(new Rectangle(440, 240, 255, 35));
panel.add(label, null);
panel.add(label, BorderLayout.CENTER);
this.getContentPane().setLayout(null);
this.setSize(new Dimension(1148, 336));
this.getContentPane().add(remove, null);
this.getContentPane().add(add, null);
this.getContentPane().add(lastnamepanel, null);
this.getContentPane().add(lastnamelabel, null);
this.getContentPane().add(namelabel, null);
this.getContentPane().add(namepanel, null);
this.getContentPane().add(panel, null);
}
答案 0 :(得分:0)
您使用的方式是不使用预备声明。用这种方式
String sql = "SELECT foto, name, surname FROM criminals WHERE id = ?" ;
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1,p_id);
答案 1 :(得分:0)
我找到了解决方案!在这里,万一其他人搜索它!
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, password);
System.out.println("At the profile class id = " + p_id);
String sql = "SELECT foto, name, surname FROM criminals WHERE id = " + p_id;
System.out.println(sql);
PreparedStatement stmt = conn.prepareStatement(sql);
resultSet = stmt.executeQuery();
byte[] bytes = null;
while (resultSet.next()) {
namepanel.setText(resultSet.getString(2));
lastnamepanel.setText(resultSet.getString(3));
bytes = resultSet.getBytes(1);
}
resultSet.close();
stmt.close();
conn.close();
if (bytes != null) {
JFrame f = new JFrame();
image = f.getToolkit().createImage(bytes);
}
} catch (Exception e) {
}
jbInit();
}
public void jbInit() throws Exception {
pic = new ImageIcon(image);
label = new JLabel("", pic, JLabel.CENTER);
}