0

I want to upload a path of an image to mysql database but whenever i tried to upload it, it always upload the path without '/'. example i have a file in src/upload/nameofimage.jpg, in the database it's srcuploadnameofimage.jpg

here is the upload button that i have

try {
            String newpath = "src/upload";
            File directory = new File(newpath);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            File fileawal = null;
            File fileakhir = null;
            String ext = this.filename.substring(filename.lastIndexOf('.')+1);
            fileawal = new File(filename);
            System.out.println(newpath);
            fileakhir = new File(newpath+"/"+Nik.getText()+"."+ext);
            System.out.println(fileakhir);
            stat = koneksi.createStatement();
            stat.executeUpdate("insert into user values ('" + Nik.getText() + "','" + Nama.getText() + "','" + fileakhir.toString() + "')");
            System.out.println(fileakhir.toString());
            JOptionPane.showMessageDialog(null, "Akun Sudah Terdaftar, Silahkan Kembali Login");
            Files.copy(fileawal.toPath(), fileakhir.toPath());
            Login log = new Login();
            log.setVisible(true);
            this.setVisible(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }

and this is the image picker button that i have

try {
                JFileChooser chooser = new JFileChooser();
                chooser.showOpenDialog(null);
                File f = chooser.getSelectedFile();
                ImageIcon icon = new ImageIcon(f.toString());
                Image img = icon.getImage().getScaledInstance(Foto.getWidth(), Foto.getHeight(), Image.SCALE_DEFAULT);
                ImageIcon ic = new ImageIcon(img);
                Foto.setIcon(ic);
                filename = f.getAbsolutePath();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }

I have tried using .toString(), .getPath(), .getAbsolutePath(), but none of them include '/' in the database

Lenza
  • 3
  • 1
  • 1
    Note sure if it's related to your issue, but I strongly recommend you use a `PreparedStatement` and set the parameters instead of building a SQL statement via string concatenation. – Slaw Feb 15 '23 at 02:12
  • ok i will try to use PreparedStatement, may i ask why i should do that? – Lenza Feb 15 '23 at 02:20
  • https://stackoverflow.com/questions/3271249/difference-between-statement-and-preparedstatement – tgdavies Feb 15 '23 at 02:25
  • try and use: `fileakhir.getAbsolutePath().replaceAll("\\\\", "/")` instead of: `fileakhir.toString()`. – DevilsHnd - 退職した Feb 15 '23 at 03:05
  • You can store a string like "src/upload/nameofimage.jpg" (or "src\upload\nameofimage.jpg") in the MySQL database table column of type VARCHAR. If you have the file object defined as `File` or `Path`, just use the `fileobject.toString()` to get its string value and insert into the table. This should work fine. – prasad_ Feb 15 '23 at 03:12
  • "_may i ask why i should do that?_" -- The Q&A linked by tgdavies should answer your question. Probably the most important reason to use `PreparedStatement` is it prevents [SQL injection attacks](https://en.wikipedia.org/wiki/SQL_injection) by properly escaping special characters. – Slaw Feb 15 '23 at 04:46

0 Answers0