Search code examples
javamysqlurljavax.imageiodata-linking

Changing the protocol of a url from C: to File:


Hi I am building an application that stores urls of files found in the usual windows directories using Java,files are located in the C-Drive. The program uses fileChooser to get the path of the file obtained then that path() is cast to a string and stored in an sql database using java.sql.PreparedStatement.setURL(fileChooser.getPath().toString());

Here is the problem: When I try to recover the urls from the database table using java.sql.ResultSet.getUrl(String columnName). I get a protocol not found exception. I have no idea of how to manipulate the string obtained from the database into an actual url that I can use to recover the image and display as profile image from the directory specified:

Here is my code:

public void addURLRow(String description, String url)
        throws SQLException {

    PreparedStatement pstmt = null;

    try {
        pstmt = this.sqlConnectionObject.prepareStatement(
                "INSERT INTO data_repository"
                + "(document_name,url) VALUES (?,?)");

        pstmt.setString(1, description);
        pstmt.setURL(2, new URL(url));
        pstmt.execute();
    } catch (SQLException sqlex) {
        JOptionPane.showMessageDialog(null,sqlex);
        // JDBCTutorialUtilities.printSQLException(sqlex);
    } catch (Exception ex) {
        System.out.println("Unexpected exception");
        JOptionPane.showMessageDialog(null,"ERROR"+ex.getMessage());
       // ex.printStackTrace();
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
    }
}

//Obtain picture from the database
public Object obtainImageUrl(String userName) throws MalformedURLException {
    try (Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/picturedb", "root", "")) {
        Statement mysqlStm = connect.createStatement();
        String SELECT_QUERY = "SELECT * FROM picture WHERE userName IN ('" + userName + "');";
        ResultSet cursor = mysqlStm.executeQuery(SELECT_QUERY);
        while (cursor.next()) {
            imageUrl = cursor.getObject("picUrl");
        }
        URL url = new URL(imageUrl.toString());
        // File imageFile = new File(imageUrl);
        try {
            Image img = ImageIO.read(url);

        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "ERROR" + ex.getMessage());
        }

    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, "ERROR" + e.getMessage());
    }
    return imageUrl;
}

//Allows user to set profile
    addImagButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            int ret = fileChooser.showDialog(null, "Add Profile Picture");
            if (ret == JFileChooser.APPROVE_OPTION) {
                // fileChooser.getSelectedFile().getPath();
                try {
                    // file = new File("C:\\Users\\user\\Pictures\\EmmaBeib\\12553040_133350150376029_4407158756206009973_n.jpg");
                    String fileUrl = fileChooser.getSelectedFile().getPath();
                    JOptionPane.showMessageDialog(null,"URL = "+fileUrl);
                    addURLRow("userName", fileUrl);
                    image = ImageIO.read(fileChooser.getSelectedFile());

                    addImagButton.setIcon(new ImageIcon(image));
                    addPictureToDB(fileChooser.getSelectedFile());
                } catch (FileNotFoundException e) {
                    System.err.println(e);
                } catch (IOException e) {
                    System.out.println(e);
                }catch(SQLException e){
                    JOptionPane.showMessageDialog(null,"ERROR"+e.getMessage());
                }

Solution

  • Don't store the value with PreparedStatement.setURL(), that is creating a DB-internal SQL DATALINK. Store it as a normal String:

    pstmt.setString(2, url)
    

    if urlis a String or

    pstmt.setString(2, url.toExternalForm())
    

    if url is an URL object. When reading back just read the String value in a variable like s and create a new URL object from that:

    URL u = new URL(s)