DB中有一张带图像的表格。我想,一个图像将在图像视图中显示,但是出了点问题,我有SQLException。
SELECT student.name
FROM student
WHERE student.ID in (SELECT advisor.s_ID
FROM advisor
, instructor
WHERE advisor.i_ID = instructor.ID
and instructor.dept_name = 'Comp.Sci')
答案 0 :(得分:0)
来自ResultSet
的文件(我的重点):
ResultSet
对象维护一个指向其当前数据行的光标。 最初光标位于第一行之前。下一个方法将光标移动到下一行
因此,您需要调用next()
将光标指向每一行。假设表中最多有一行,或者您只对第一行感兴趣,那么可以
ConnectionHelper ch=new ConnectionHelper();
ch.Connect();
String q="SELECT IMG FROM img_ins";
Statement st=ch.con.createStatement();
ResultSet rs = st.executeQuery(q);
if (rs.next()) {
InputStream is= rs.getBinaryStream("IMG");
// instead of the next 9 lines, you could just do
// javafx.scene.image.Image image1 = new Image(is);
OutputStream os=new FileOutputStream(new File("img.jpg"));
byte [] content= new byte[1024];
int size=0;
while ((size=is.read(content))!=-1){
os.write(content, 0, size);
}
os.close();
is.close();
javafx.scene.image.Image image1=new Image("file:img.jpg", image.getFitWidth(), image.getFitHeight(), true, true);
image.setImage(image1);
image.setPreserveRatio(true);
}
但请注意OP下面的评论。
答案 1 :(得分:0)
以下是我编写的一段代码中的示例。这是一个sqlite db
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.setID(Integer.toString(rset.getInt("company_id")));
c1.setName(rset.getString("company_name"));
.
.
.
c1.setWebSiteAddress(rset.getString("website_address"));
//This part is important to you.
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);//right here is where you want to set your imageView with the image.
}
companyData.add(c1);
}
}
catch(SQLException | IOException ex)
{
Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
}