【问题标题】:Save image to Derby Database with Java使用 Java 将图像保存到 Derby 数据库
【发布时间】:2015-08-04 07:48:21
【问题描述】:

在 derby 数据库中使用文件选择器保存图像时遇到问题。我想构建一个文件选择器方法来从用户获取图像并将图像保存到数据库中。但现在图像不会保存在我的数据库中。 请帮我。 谢谢你

我的表格如下:

-- create table LABORATORY(
-- ID INT NOT NULL,
-- TESTERS_ID INT NOT NULL,
-- PATIENTS_ID INT,
-- AZMAYESH BLOB
-- PRIMARY KEY(ID));

还有我的班级代码:

public class Database {

//FileInputStream fis = null;
//PreparedStatement ps = null;
private Connection connection;
private ResultSet resultSet;
private Statement statement;

public Database() throws SQLException{/*nabayad public bashe*/
    try{
        String userName="reza";
        String password="reza";
        String hostURL="jdbc:derby://localhost:1527/reza";
        connection=DriverManager.getConnection(hostURL,userName,password);
        statement=connection.createStatement(TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

    }
    catch(SQLException err){
        JOptionPane.showMessageDialog(null, err.getMessage());
    }

}
public void azmayesh() throws SQLException, IOException {
    int id=LastIdOf("LABORATORY");
    String INSERT_PICTURE = "insert into LABORATORY(ID,TESTERS_ID,PATIENTS-ID,AZMAYESH) values (?,?,?,?)";
    FileInputStream fis = null;
    PreparedStatement ps = null;

        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open text file");
        fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("image Files", "*.jpg")
        );
        File file = fileChooser.showOpenDialog(null);
        fis = new FileInputStream(file);
        ps = connection.prepareStatement(INSERT_PICTURE);
        ps.setInt(1, id);
        //ps.setInt(2, id);
        //ps.setInt(3, id);
        ps.setBinaryStream(4, fis, (int) file.length());
        ps.executeUpdate();
        JOptionPane.showMessageDialog(null, "Azmayesh Ersal shod.");
        //connection.commit();
        //ps.close();
        //fis.close();
}

}

【问题讨论】:

  • 您提到了mysql,但您正在尝试连接到derby
  • 嗨,我的错,如何更改连接mysql的代码?
  • 更新您的 hostUrl 以匹配您的本地主机数据库。前任。 jdbc:mysql://...
  • 我使用Java netbeans默认数据库,是derby还是mysql?
  • with String userName="reza";字符串密码="reza";字符串 hostURL="jdbc:derby://localhost:1527/reza";我正在连接到数据库。

标签: java image javafx derby filechooser


【解决方案1】:

改变这个:

    String userName="reza";
    String password="reza";
    String hostURL="jdbc:derby://localhost:1527/reza";

到这里

    String userName="mysqlusername";
    String password="mysqlpassword";
    String hostURL="jdbc:mysql://localhost:3306/mysqldatabasename";

localhost 更改为您的主机名或 IP。 3306 是 MySQL 的默认端口。

注意:你的类路径中应该有 mysql-jdbc-connector。

【讨论】:

  • 我正在使用 Java netbeans 默认数据库,是 derby 还是 mysql?
  • 对不起,这是德比数据库。请你帮我解决这个问题。
【解决方案2】:
  if(request.getParameter("PhotoFiled")!= null && request.getParameter("PhotoFiled").length()> 0) 
  {
     FileInputStream Photograph_Stream = new FileInputStream(request.getParameter("PhotoFiled"));
     if(Photograph_Stream.available()>0)
     {
          ImgBn.Photograph = new byte[Photograph_Stream.available()] ;
          Photograph_Stream.read( ImgBn.Photograph, 0, Photograph_Stream.available()); 
     }
  }

【讨论】:

  • 感谢您的回答,能否将最终代码发给我?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 2019-06-27
相关资源
最近更新 更多