将图像Blob插入Sqlite Java

将图像Blob插入Sqlite Java,java,image,sqlite,Java,Image,Sqlite,如何在java netbeans中使用jfilechooser将图像插入到我的表sqlite中 我有两个JButton,第一个用于从pc浏览图像,第二个用于在表中插入信息 public void añadirUser() { String user; String pass; String pass2; String nombre; String apellidos; String telefono; String email;

如何在java netbeans中使用jfilechooser将图像插入到我的表sqlite中

我有两个JButton,第一个用于从pc浏览图像,第二个用于在表中插入信息

public void añadirUser() {

    String user;
    String pass;
    String pass2;
    String nombre;
    String apellidos;
    String telefono;
    String email;

    user = jTextField2.getText();
    pass = jPasswordField1.getText();
    pass2 = jPasswordField2.getText();
    nombre = jTextField1.getText();
    apellidos = jTextField5.getText();
    telefono = jTextField8.getText();
    email = jTextField9.getText();

    if (user.isEmpty()) {
        jLabel9.setText("User obligatorio");
    }

    else if (pass.isEmpty()) {
        jLabel9.setText("Contraseña obligatoria");
    } else if (pass2.isEmpty()) {
        jLabel9.setText("Contraseña obligatoria");
    } else if (pass2 == null ? pass != null : !pass2.equals(pass)) {
        jLabel9.setText("La contraseña no coincide");
    }

    else if (nombre.isEmpty()) {
        jLabel9.setText("Nombre obligatorio");
    }

    else if (telefono.isEmpty()) {
        jLabel9.setText("Telefono obligatorio");
    } else if (email.isEmpty()) {
        jLabel9.setText("email obligatorio");
    } else {
        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:"
                    + this.db);
            System.out.println("Conectado a la base de datos SQLite [ "
                    + this.db + "]");
        } catch (Exception e) {
            System.out
                    .println("No es posible conectar con la base de datos"
                            + this.db + "");
        }

        String q = "INSERT INTO Usuario(USER,PASS,NOMBRE,APELLIDOS,TELEFONO,EMAIL,FOTO) VALUES('" + user + "','"+ pass+ "','"+ nombre+ "','"+ apellidos + "'"
                + ",'" + telefono+ "','"+ email + "','" + image_user + "')";
        try {
            PreparedStatement pstm = connection.prepareStatement(q);
            pstm.execute();
            pstm.close();
            JOptionPane.showMessageDialog(rootPane,
                    "Enhorabuena has introducido un nuevo usuario");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane,
                    "No se ha podido insertar el nuevo User");
        }
    }
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    filname = f.getAbsolutePath();
    jTextField7.setText(filname);
    try {
        File image = new File(filname);
        FileInputStream fis = new FileInputStream(image);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        image_user = bos.toByteArray();
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

// The button i use to insert
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    añadirUser();
}              

多谢各位

获取路径后,将文件读入字节数组并将其插入数据库。。。请描述问题的具体内容。谢谢您的回答。问题是当我查看表格时,我发现如下内容:[B@336528c6。换句话说,它不会像blob一样保存。我从未在Java中做过blob,但它可以表示为字节数组,这将“toString”表示为[B@xxxxxxxx"。谢谢您的回答。也许,我想通过查询在jlabel中打印图像,但它不适用于以下图像[B@xxxxxxx],但它可以处理我使用slite manager手动添加的blob格式的图像。@user3325719您解决过这个问题吗?处理类似的问题,并且有相同的问题,其中filename=image\u user(ByteArray)
 String sql = "INSERT INTO student (id, image) values (?, ?)";
    String homeDir = System.getProperty("user.home"); 

    try{ 
        Class.forName("org.sqlite.JDBC"); // "com.mysql.jdbc.Driver" for mysql
         conn = DriverManager.getConnection("jdbc:sqlite:"+homeDir+"\\database.db");
        PreparedStatement pstmt = conn.prepareStatement(sql);
            {

        // set parameters
        pstmt.setString(1, id);
        pstmt.setBytes(2, filename);

        pstmt.execute();
        System.out.println("Stored the file in the BLOB column.");


}}   catch (Exception ex) {  Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);}