[英]Using Eclipse SWT Image, JAVA and SQLite to insert, store and retrieve Images
我正在編寫一個基本的Java應用程序,允許用戶將有關個人的詳細信息插入到SQLite數據庫中。 我正在使用Eclipse SWT作為GUI。
Eclipse SWT定義了一個用於在GUI中顯示Images的Image( org.eclipse.swt.graphics.Image
)。
我試圖允許用戶瀏覽文件系統,選擇一個圖像,然后將該圖像插入數據庫。 我還希望能夠從數據庫中檢索該圖像並將其顯示在GUI中。
一切都很簡單,但對於我的生活,我無法讓它工作! 我也搜索了很多,似乎無法找到解決方案。
我正在使用Eclipse IDE for Java Developers(3.6), sqlite-jdbc-3.7.2.jar
和JDK 1.6.0_22。
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import java.sql.*;
/***********************************************************************/
/*** Tests reading and writing SWT Images from an SQLite Database ***/
/***********************************************************************/
public class ImageTest {
Shell shell;
//Variables to store the current values when editing
private Canvas personPhoto;
private Image personImage;
private int personID = 1;
private double photoWidth = 100;
private double photoHeight = 100;
//Database connection and statement variables
private static Connection connection = null;
private static Statement statement = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
public ImageTest(Shell parent, Connection passedConnection) {
shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
shell.setLayout(new GridLayout());
connection = passedConnection;
}
private void createControlButtons() {
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
GridLayout layout = new GridLayout();
layout.numColumns = 2;
composite.setLayout(layout);
Button okButton = new Button(composite, SWT.PUSH);
okButton.setText("OK");
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if(personID > 0){
try {
PreparedStatement ps = connection.prepareStatement("UPDATE person SET photo = ? " +
"WHERE person_id = ?");
ps.setBytes(1, personImage.getImageData().data);
ps.setInt(2, personID);
ps.executeUpdate();
ps.close();
} catch (SQLException err) {
err.printStackTrace();
}
} else {
try {
PreparedStatement ps = connection.prepareStatement("INSERT INTO person (photo) VALUES (?)");
ps.setBytes(1, personImage.getImageData().data);
ps.executeUpdate();
ps.close();
} catch (SQLException err) {
err.printStackTrace();
}
}
shell.close();
}
});
Button cancelButton = new Button(composite, SWT.PUSH);
cancelButton.setText("Cancel");
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
shell.setDefaultButton(okButton);
}
private void createTextWidgets(final Display display) {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
new Label(shell, SWT.NONE).setText("Photo:");
personPhoto = new Canvas(shell, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
gridData.widthHint = (int)photoWidth;
gridData.heightHint = (int)photoHeight;
gridData.verticalSpan = 5;
gridData.horizontalSpan = 2;
personPhoto.setLayoutData(gridData);
personPhoto.redraw();
personPhoto.addPaintListener(new PaintListener() {
public void paintControl(final PaintEvent event) {
if (personImage != null) {
event.gc.drawImage(personImage, 0, 0);
}
}
});
//Skip a Column
new Label(shell, SWT.NONE);
Button browse = new Button(shell, SWT.PUSH);
browse.setText("Browse...");
gridData = new GridData(GridData.FILL, GridData.CENTER, true, false);
gridData.horizontalIndent = 5;
browse.setLayoutData(gridData);
browse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
String fileName = new FileDialog(shell).open();
if (fileName != null) {
personImage = new Image(display, fileName);
personPhoto.redraw();
}
}
});
Button delete = new Button(shell, SWT.PUSH);
delete.setText("Delete");
gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
gridData.horizontalIndent = 5;
delete.setLayoutData(gridData);
delete.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (personImage != null) {
personImage.dispose();
personImage = null;
personPhoto.redraw();
}
}
});
//Skip a Column
new Label(shell, SWT.NONE);
//Skip two Rows
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
}
public void open() {
Display display = shell.getDisplay();
//To avoid null pointer exceptions
personImage = new Image(display,"user.png");
try{
PreparedStatement ps = connection.prepareStatement("SELECT photo FROM person WHERE person_id = ?");
ps.setInt(1, personID);
rs = ps.executeQuery();
while (rs.next()) {
//dispose of the current image
personImage.dispose();
personImage = new Image(display, (int) photoWidth, (int) photoHeight);
//Retrieve the photo for this person
personImage.getImageData().data = rs.getBytes("photo");
}
ps.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
createTextWidgets(display);
createControlButtons();
shell.pack();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
}
我現在已經簡化了代碼,但我仍然無法正確地從數據庫中提取字節數組並將其顯示為SWT圖像。 有人有什么想法嗎? 任何幫助將非常感激!
吉文
/* Imports */
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import java.sql.*;
/***********************************************************************/
/*** Tests reading and writing SWT Images from an SQLite Database ***/
/***********************************************************************/
public class ImageTest {
Shell shell;
//Variables to store the current values when editing
private Canvas personPhoto;
private Image personImage;
private int personID = 1;
private double photoWidth = 100;
private double photoHeight = 100;
//Database connection and statement variables
private static Connection connection = null;
private static Statement statement = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
public ImageTest(Shell parent, Connection passedConnection) {
shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
shell.setLayout(new GridLayout());
connection = passedConnection;
}
private void createControlButtons() {
Composite composite = new Composite(shell, SWT.NONE);
composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
GridLayout layout = new GridLayout();
layout.numColumns = 2;
composite.setLayout(layout);
Button okButton = new Button(composite, SWT.PUSH);
okButton.setText("OK");
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if(personID > 0){
try {
PreparedStatement ps = connection.prepareStatement("UPDATE person SET photo = ? " +
"WHERE person_id = ?");
ps.setBytes(1, personImage.getImageData().data);
ps.setInt(2, personID);
ps.executeUpdate();
ps.close();
} catch (SQLException err) {
err.printStackTrace();
}
} else {
try {
PreparedStatement ps = connection.prepareStatement("INSERT INTO person (photo) VALUES (?)");
ps.setBytes(1, personImage.getImageData().data);
ps.executeUpdate();
ps.close();
} catch (SQLException err) {
err.printStackTrace();
}
}
shell.close();
}
});
Button cancelButton = new Button(composite, SWT.PUSH);
cancelButton.setText("Cancel");
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
shell.setDefaultButton(okButton);
}
private void createTextWidgets(final Display display) {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
new Label(shell, SWT.NONE).setText("Photo:");
personPhoto = new Canvas(shell, SWT.BORDER);
GridData gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
gridData.widthHint = (int)photoWidth;
gridData.heightHint = (int)photoHeight;
gridData.verticalSpan = 5;
gridData.horizontalSpan = 2;
personPhoto.setLayoutData(gridData);
personPhoto.redraw();
personPhoto.addPaintListener(new PaintListener() {
public void paintControl(final PaintEvent event) {
if (personImage != null) {
event.gc.drawImage(personImage, 0, 0);
}
}
});
//Skip a Column
new Label(shell, SWT.NONE);
Button browse = new Button(shell, SWT.PUSH);
browse.setText("Browse...");
gridData = new GridData(GridData.FILL, GridData.CENTER, true, false);
gridData.horizontalIndent = 5;
browse.setLayoutData(gridData);
browse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
String fileName = new FileDialog(shell).open();
if (fileName != null) {
personImage = new Image(display, fileName);
personPhoto.redraw();
}
}
});
Button delete = new Button(shell, SWT.PUSH);
delete.setText("Delete");
gridData = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
gridData.horizontalIndent = 5;
delete.setLayoutData(gridData);
delete.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (personImage != null) {
personImage.dispose();
personImage = null;
personPhoto.redraw();
}
}
});
//Skip a Column
new Label(shell, SWT.NONE);
//Skip two Rows
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
}
public void open() {
Display display = shell.getDisplay();
//To avoid null pointer exceptions
personImage = new Image(display,"user.png");
try{
PreparedStatement ps = connection.prepareStatement("SELECT photo FROM person WHERE person_id = ?");
ps.setInt(1, personID);
rs = ps.executeQuery();
while (rs.next()) {
//dispose of the current image
personImage.dispose();
personImage = new Image(display, (int) photoWidth, (int) photoHeight);
//Retrieve the photo for this person
personImage.getImageData().data = rs.getBytes("photo");
}
ps.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
createTextWidgets(display);
createControlButtons();
shell.pack();
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
}
如果是我,我不會將圖像保存到數據庫中。 首先是由於這樣的問題,其次是因為它會使數據庫變得非常大。 在數據庫中包含圖像路徑可能更容易,然后從路徑加載該圖像。 類似地,插入只會插入到圖像文件的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.