将图像存储在netbeans中的数据库中

时间:2014-02-01 16:42:09

标签: swing oracle10g awt netbeans-7 netbeans-platform

我正在尝试将图像与string一起存储到db中。我使用下面给出的代码

 File image = new File("c:/a1.jpg");
 String d=jTextField1.getText();
FileInputStream  fis 
 try {
        psmnt = con.prepareStatement("insert into c(name,pic) "+ "values(?,?)");
         psmnt.setString(1,d);
        fis = new FileInputStream(image);
         psmnt.setBinaryStream(2,fis,(int) (image.length()));
        psmnt.executeUpdate();
     }
catch().....

我尝试了很多。但仍然收到错误“请求未实现或不合理的转换”。任何人都可以帮忙解决这个问题吗?pic被声明为blob数据类型。谢谢。

1 个答案:

答案 0 :(得分:0)

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {



      try {  
         String sid="orcl";
         String username="user";
         String password= "pass";
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
         Connection connection = DriverManager.getConnection(  
                "jdbc:oracle:thin:@localhost:1521:"+sid, username, password);  

        //Statement statement = connection.createStatement();  
           File image = new File("c:/a1.jpeg");
           String d= "the pics";
           FileInputStream  fis ;
           fis = new FileInputStream(image);

          String query = " insert into c(name,pic)values(?,?)";
          PreparedStatement preparedStmt = connection.prepareStatement(query);
           //System.out.println("Data is inserted:");
           preparedStmt.setString(1, d);
           preparedStmt.setBinaryStream(2,fis,(int) (image.length()));

            // execute the preparedstatement
           preparedStmt.execute();
           connection.close(); 
   } catch (Exception e) {  
        e.printStackTrace();  
     }  
   System.out.println("Data is inserted:");

}

我想你在项目中添加了oracle jar ojdbc14.jar