【发布时间】:2015-08-04 07:48:21
【问题描述】:
在 derby 数据库中使用文件选择器保存图像时遇到问题。我想构建一个文件选择器方法来从用户获取图像并将图像保存到数据库中。但现在图像不会保存在我的数据库中。 请帮我。 谢谢你
我的表格如下:
-- create table LABORATORY(
-- ID INT NOT NULL,
-- TESTERS_ID INT NOT NULL,
-- PATIENTS_ID INT,
-- AZMAYESH BLOB
-- PRIMARY KEY(ID));
还有我的班级代码:
public class Database {
//FileInputStream fis = null;
//PreparedStatement ps = null;
private Connection connection;
private ResultSet resultSet;
private Statement statement;
public Database() throws SQLException{/*nabayad public bashe*/
try{
String userName="reza";
String password="reza";
String hostURL="jdbc:derby://localhost:1527/reza";
connection=DriverManager.getConnection(hostURL,userName,password);
statement=connection.createStatement(TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch(SQLException err){
JOptionPane.showMessageDialog(null, err.getMessage());
}
}
public void azmayesh() throws SQLException, IOException {
int id=LastIdOf("LABORATORY");
String INSERT_PICTURE = "insert into LABORATORY(ID,TESTERS_ID,PATIENTS-ID,AZMAYESH) values (?,?,?,?)";
FileInputStream fis = null;
PreparedStatement ps = null;
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open text file");
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("image Files", "*.jpg")
);
File file = fileChooser.showOpenDialog(null);
fis = new FileInputStream(file);
ps = connection.prepareStatement(INSERT_PICTURE);
ps.setInt(1, id);
//ps.setInt(2, id);
//ps.setInt(3, id);
ps.setBinaryStream(4, fis, (int) file.length());
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Azmayesh Ersal shod.");
//connection.commit();
//ps.close();
//fis.close();
}
}
【问题讨论】:
-
您提到了
mysql,但您正在尝试连接到derby。 -
嗨,我的错,如何更改连接mysql的代码?
-
更新您的 hostUrl 以匹配您的本地主机数据库。前任。 jdbc:mysql://...
-
我使用Java netbeans默认数据库,是derby还是mysql?
-
with String userName="reza";字符串密码="reza";字符串 hostURL="jdbc:derby://localhost:1527/reza";我正在连接到数据库。
标签: java image javafx derby filechooser