PreparedStatement|类是预编译的Statement对象。当要执行多条语句时,且每条语句的结构相似时,可以利用此类
PreparedStatement show;
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,user,pass);
show = conn.prepareStatement(“select img_data from imgtable “+” where img_id = ?”) ;}
ResultSet rs = show.executeQuery() 在函数里面不用再输入查询的语句,直接就可以查询
其中你的问好进行传入功能
show.setInt(1,6); 前面的1为第几个问哈,6为传入的数值,还有其他的形式setXxx格式,用于?是什么类型
其中,当数据库要进行进行字节的操作时
需求:需要运用到PreparedStatement对象中的setBinaryStream(int paramenterIndex,InputStream x);该方法用于指定
参数传入二进制输入流,从而可以实现将Blob数据保存到数据库的功能
其中mediumblob对象可以存储16MB的内容 blob类型最多能存储64KB的内容
综上来连接数据库,当要查询图片的时候,利用上述代码输入id号即可查看图片
例如:show.setInt(1,1);编号为1的图片为
代码显示
try(
//执行查询
ResultSet rs = show.executeQuery()){
if(rs.next()) {
//取出blob列
Blob imgBlob = rs.getBlob(1);
//取出Blob列的数据
ImageIcon icon = new ImageIcon(imgBlob.getBytes(1L, (int)imgBlob.length()));
imageLabel.setIcon(icon);
System.out.println(“显示成功!”);
}
其中imageLabel
JLabel imageLabel = new JLabel();
panel.add(new JScrollPane(imageLabel) ,BorderLayout.CENTER);//为标签设置滚轮