2013-06-19 77 views
1

我正在使用netbeans和mySQL创建产品。我用了一个文件选择从用户那里获取的图像时,按钮上点击,例如:将图像插入到MySQL中

public void handle(ActionEvent event){ 
    FileChooser fileChooser = new FileChooser(); 

     //Set extension filter 
     FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG"); 
     FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG"); 
     fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG); 

     //Show open file dialog 
     File file = fileChooser.showOpenDialog(null); 

     try { 
      BufferedImage bufferedImage = ImageIO.read(file); 
      WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null); 
      myImageView.setImage(image); 
     } catch (IOException ex) { 
      Logger.getLogger(CreateProductUI.class.getName()).log(Level.SEVERE, null, ex); 
     } 
} 

我用下面的代码显示的图像:

Image image = panel.getMyImageView().getImage(); 

它,直到我设法工作正常将图像插入数据库。这是我的构造函数和create方法:

public Product(String name,String desc,double price, int quantity,String datestr,Image image){ 
    this.name = name; 
    this.desc = desc; 
    this.price = price; 
    this.quantity = quantity; 
    this.datestr = datestr; 
    this.image = image; 
} 

public boolean create(){ 
    boolean success = false; 
    DBController db = new DBController(); 
    String dbQuery; 
    db.getConnection();  

    dbQuery = "INSERT INTO sm_product(productName,productDescription,productPrice,productQuantity,dateOfCreation,productStatus,productImage) VALUES ('" + name + "', '" + desc + "', " + price + ", " + quantity + ",'" + datestr + "', 'Available', '" + image + "')"; 

    if (db.updateRequest(dbQuery) == 1){ 
     success = true; 
    } 
    db.terminate(); 
    return success; 
} 

然而,图像存储为“[email protected]”。我用相同的图片尝试过两次,但地址不同。我想知道我是否以错误的方式存储图像?我不确定我是否可以使用select SQL语句检索图像,因为我还没有尝试,但我认为它不起作用。

有人有更好的方法来解决它,因为存储在数据库中的图像是非常奇怪的,我认为这可能是错误的。

在此先感谢。

更新部分

public void getConnection(){ 
    String url = ""; 
    try { 
     //url = "jdbc:mysql://172.20.133.227/test"; 
     url = "jdbc:mysql://localhost/amkcc"; 
     con = DriverManager.getConnection(url, "root", "root"); 
     System.out.println("Successfully connected to " + url+ "."); 
    } 
    catch (java.sql.SQLException e) { 
     System.out.println("Connection failed ->"+ url); 
     System.out.println(e); 
    } 
} 
+0

存储你需要传递'byte []'作为参数的blob。你需要弄清楚如何从你现有的代码构建 – DevZer0

+0

什么是blob?也如何使用一个字节[] –

+0

如果你使用一点谷歌我相信你可以找到答案。 – DevZer0

回答

-1

如何使用JDBC MySQL中保存图像的一个例子:

File image = new File("C:/image.jpg"); 
psmnt = connection.prepareStatement 
    ("insert into save_image(name, city, image, Phone) "+ "values(?,?,?,?)"); 
psmnt.setString(1,"MyName"); 
psmnt.setString(2,"MyCity"); 
psmnt.setString(4,"123456"); 
FileInputStream fis = new FileInputStream(image); 
psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length())); 
int s = psmnt.executeUpdate(); 

这是你的方法应该如何看起来像:

public boolean create(){ 
boolean success = false; 
try{ 
DBController db = new DBController(); 
String dbQuery; 
db.getConnection();  

dbQuery = "INSERT INTO sm_product(productName,productDescription,productPrice,productQuantity,dateOfCreation,productStatus,productImage) VALUES (?,?,?,?, ?, 'Available', ?)"; 

PreparedStatement psmnt = db.getConnection().prepareStatement(dbQuery); 
psmnt.setString(1,name); 
psmnt.setString(2, desc); 
psmnt.setDouble(3, price); 
psmnt.setInt(4, quantity); 
psmnt.setString(5, dateStr); 

File imageFile = new File("test.png"); 
RenderedImage renderedImage = SwingFXUtils.fromFXImage(image, null); 
ImageIO.write(renderedImage, "png", imageFile); //Change extension appropriately 
FileInputStream fis = new FileInputStream(imageFile); 
psmnt.setBinaryStream(3, (InputStream)fis, (int)(imageFile.length())); 
int s = psmnt.executeUpdate(); 

//check the value of s and initialize success appropriately 

return success; 
}catch(Exception e) { e.printStackTrace(); } 
    return false; 

} 

'DBController`中的getConnection()方法应该是b e是这样的:

public Connection getConnection(){ 
      if(con != null) return con; 
    String url = ""; 
    try { 
     //url = "jdbc:mysql://172.20.133.227/test"; 
     url = "jdbc:mysql://localhost/amkcc"; 
     con = DriverManager.getConnection(url, "root", "root"); 
     System.out.println("Successfully connected to " + url+ "."); 
        return con; 
    } 
    catch (java.sql.SQLException e) { 
     System.out.println("Connection failed ->"+ url); 
     System.out.println(e); 
    } 
      return null; 
} 
+0

但我从用户输入获取文件,所以我不知道图像的确切位置。 –

+0

顺便说一下,这不是我的失望。我的选票还不够。这是由其他用户。你的连接课程是做什么的?谢谢 –

+0

的确如此,我没有意识到这一点。顺便说一下,'connection'对象应该从'DBController'类中检索,可能类似于'db.getConnection()' – Mubin