我正在尝试创建桌面应用程序(scenebuilder),我想从DATABASE(SQLITE)检索Image的路径并将其附加到按钮以在ImageView
中打开它但我在做的时候遇到了问题这一点。
注意:我正在尝试学习Java
我的代码
@FXML
private ImageView imageView;
@FXML
void btn10 (ActionEvent event) {
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:sogeclair.sqlite");
c.setAutoCommit(false);
System.out.println("Opened database successfully");
stmt = c.createStatement();
String sql = "SELECT * FROM image WHERE rowid = 1" ;
System.out.println(sql);
ResultSet rs = stmt.executeQuery( sql );
while ( rs.next() ) {
String path=rs.getString("imaging");
imageView.setImage(new Image(path));
System.out.println();
}
rs.close();
stmt.close();
c.close();
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Operation done successfully");
}
我在结果中得到了这个
Opened database successfully
SELECT * FROM image WHERE rowid = 1
java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
Deleting directory C:\Users\Lenovo\Documents\NetBeansProjects\sogece\dist\run538077077
jfxsa-run:
BUILD SUCCESSFUL (total time: 10 seconds)
和 MyDatabase
欢迎所有评论:请帮助我:D
答案 0 :(得分:0)
这是我编写的程序中的代码,该程序从sqlite中检索图像blob。您可以从here下载并运行该程序。它正在运行,但它是一个不完整的程序。
在你的代码中,一旦你使用我的代码中的想法获得图像,那么只需使用该图像来设置你的ImageView。
String sqlTCNote = "SELECT * FROM Company;";
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:contactFX.db");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(sqlTCNote))
{
while(rset.next())
{
Company c1 = new Company();
c1.setName(rset.getString("company_name"));
c1.setShortName(rset.getString("short_name"));
c1.setStreet(rset.getString("street"));
c1.setCity(rset.getString("city"));
c1.setState(rset.getString("state"));
c1.setZip(rset.getString("zip"));
c1.setPhoneNumber(rset.getString("phone_number"));
c1.setTollFreeNumber(rset.getString("toll_free_number"));
c1.setEmailAddress(rset.getString("email_address"));
c1.setMissionStatement(rset.getString("mission_statement"));
c1.setPersonalNote(rset.getString("personal_note"));
c1.setWebSiteAddress(rset.getString("website_address"));
//This is the part you are interested in!
InputStream input = rset.getBinaryStream("image");
InputStreamReader inputReader = new InputStreamReader(input);
if(inputReader.ready())
{
File tempFile = new File("tempFile.jpg");
FileOutputStream fos = new FileOutputStream(tempFile);
byte[] buffer = new byte[1024];
while(input.read(buffer) > 0){
fos.write(buffer);
}
Image image = new Image(tempFile.toURI().toURL().toString());
c1.setImage(image);
}
for(int i = 0; i < 10; i++)
{
c1.addEmployee(new Employee());
}
companyData.add(c1);
}
}
catch(SQLException ex)
{
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}