将Image Blob插入Sqlite Java

时间:2014-03-12 19:06:54

标签: java image sqlite

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

我有两个jbuttons,第一个用于从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();
}              

非常感谢!

1 个答案:

答案 0 :(得分:0)

 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);}