我是JavaDb / Derby的新手。我正在将项目从MySQL迁移到Derby(使用JavaFX制作的项目)。我在NetBeans下创建了一个Embedded Db。 GUI具有序列号字段(文本字段),名称字段(文本字段),ImageView和TableView(具有相应数据的列)。以下是与Derby连接的java代码。
public static Connection ConnectionDb() {
try{
String url = "jdbc:derby:Test_JavaDb/webdb;create=true";
String user = "root";
String password = "root";
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url, user, password);
if (conn != null) {
System.out.println("Connected to database #1");
}
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
return null;
}
//BELOW METHOD IS LINKED TO A BUTTON FOR LOADING TABLE
@FXML public void LoadTable(){
data.clear();
try{
conn = lrconn.ConnectionDb();
String sql = "select * from TEST";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
data.add(new TestPOJO(
rs.getString(1),
rs.getString(2),
rs.getString(3)));
Table.setItems(data);
}
pst.close();
rs.close();
}catch(Exception e1){
}
Table.setOnMouseClicked((MouseEvent me) -> {
try {
conn = lrconn.ConnectionDb();
TestPOJO user = (TestPOJO) Table.getSelectionModel().getSelectedItem();
String sql = "select * from TEST where SLNO =?";
pst = conn.prepareStatement(sql);
pst.setString(1, user.getSLNO());
rs = pst.executeQuery();
while (rs.next()) {
slnoField.setText(rs.getString(1));
nameField.setText(rs.getString(2));
try (InputStream is = rs.getBinaryStream(3)) {
OutputStream os = new FileOutputStream(new File("photo1.jpg"));
byte[] content = new byte[1024];
int size = 0;
while((size= is.read(content))!=-1){
os.write(content,0,size);
}
os.close();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
image1 = new Image("file:photo1.jpg");
imgvw.setImage(image1);
rs.close();
pst.close();
}catch (SQLException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
});
Table.setOnKeyReleased((KeyEvent e) -> {
if(e.getCode()== KeyCode.UP || e.getCode() == KeyCode.DOWN){
try {
TestPOJO user = (TestPOJO) Table.getSelectionModel().getSelectedItem();
String sql = "select * from TEST where SLNO =?";
pst = conn.prepareStatement(sql);
pst.setString(1, user.getSLNO());
rs = pst.executeQuery();
while (rs.next()) {
slnoField.setText(rs.getString(1));
nameField.setText(rs.getString(2));
try (InputStream is = rs.getBinaryStream(3)) {
OutputStream os = new FileOutputStream(new File("photo1.jpg"));
byte[] content = new byte[1024];
int size = 0;
while((size= is.read(content))!=-1){
os.write(content,0,size);
}os.close();
}
image1 = new Image("file:photo1.jpg");
imgvw.setImage(image1);
}}catch (FileNotFoundException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException | SQLException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
连接正常,因为我在执行代码时输出“连接到数据库#1”(单击LOAD TABLE BUTTON)。但GUI表不会使用嵌入式数据库中的数据进行更新。请帮忙。