簡體   English   中英

將圖像 Blob 插入 Sqlite Java

[英]Insert Image Blob into Sqlite Java

我如何在 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();
}              

非常感謝 !

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM