我有一个问题。我正在使用数据库进行项目。当我在表格中插入新行时,我还会添加图片。我的问题是,将图片存储在数据库中的最佳方法是什么?我尝试使用不同的方式。
问题是在两种方式下,当我单击JTable进行切换时,图片切换速度都很慢。您能否提出有关正确方法的建议?我非常感谢您的帮助。 这是我的代码:
private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {
jButton1.setEnabled(true);
Database base = new Database();
model = (DefaultTableModel)jTable2.getModel();
int selectedRowIndex = jTable2.getSelectedRow();
//find picture in forlder and change jLabel2 for image
base.findImageDerictory(model.getValueAt(selectedRowIndex, 1).toString(), jLabel2 );
}
public void findImageDerictory(String name, JLabel jLabel2) {
//GET TYPE OF IMAGE
/*String[] path1 = name.split("\\.");
String ext = path1[path1.length - 1];
// your directory
File f = new File("/Computer/eclipce-projects/NewDataBase/resource");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(name) && name.endsWith(ext);
}
});*/
try {
data = getmyConnection();
st = data.createStatement();
//CallableStatement statement = null;
//Connection data = getmyConnection();
//statement = data.prepareCall("{call get_picture(?)}");
//statement.setString(1, name);
//statement.registerOutParameter(2, java.sql.Types.VARCHAR);
myResults = st.executeQuery("SELECT * FROM students_table WHERE Name = '"+name+"'");
if(myResults.next() ) {
byte[] img = myResults.getBytes("Picture");
ImageIcon firstIcon = new ImageIcon(img);
Image im = firstIcon.getImage();
Image my_image = im.getScaledInstance(jLabel2.getWidth(),
jLabel2.getHeight(), Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(my_image);
jLabel2.setIcon(newIcon);
} else {
JOptionPane.showMessageDialog(null, "No Data!!!");
}
} catch (Exception c) {
c.printStackTrace();
}
单击表格时出现问题。有时速度很慢,有时id无法从jtable中获取名称。你能帮我吗?