将 url 的协议从 C: 更改为文件:

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

您好,我正在构建一个应用程序,该应用程序使用 Java 存储 url 个在常用 windows 目录中找到的文件,这些文件位于 C 驱动器中。该程序使用 fileChooser 获取文件的路径,然后使用 java.sql.PreparedStatement.setURL(fileChooser.getPath().toString() 将 path() 转换为字符串并存储在 sql 数据库中);

问题出在这里:当我尝试使用 java.sql.ResultSet.getUrl(String columnName) 从数据库 table 恢复 url 时。我收到一个未找到协议的异常。我不知道如何将从数据库中获取的字符串转换为实际的 url,我可以用它来恢复图像并显示为指定目录中的配置文件图像:

这是我的代码:

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

不要使用 PreparedStatement.setURL() 存储值,即创建数据库内部 SQL DATALINK。将其存储为普通字符串:

pstmt.setString(2, url)

如果 url 是字符串或

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

如果 url 是一个 URL 对象。回读时,只需读取 s 等变量中的字符串值,然后从中创建一个新的 URL 对象:

URL u = new URL(s)