2016-04-09 29 views
0

Merhaba Java kullanarak normal pencere dizinlerinde bulunan dosyaların URL'lerini saklayan bir uygulama yapıyorum, dosyalar C-Drive'da bulunuyor. Program, elde edilen dosyanın yolunu almak için fileChooser kullanır, bu yol() bir dizeye dökülür ve java.sql.PreparedStatement.setURL (fileChooser.getPath(). ToString()) kullanılarak bir sql veritabanında depolanır;C: 'den bir url protokolünü: - Dosya:

İşte sorun: urev veritabanını java.sql.ResultSet.getUrl (String columnName) kullanarak kurtarmaya çalıştığımda. Protokol bulunamadı istisnası alıyorum. Ben görüntüyü kurtarmak ve belirtilen dizinden gibi profil resmi görüntülemek için kullanabileceğiniz bir gerçek url içine veritabanından elde edilen dize işlemek için nasıl hiçbir fikrim yok:

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

benim kodudur

+0

Url sütunu türü nedir? Verileri neden bir "String" yerine "URL" olarak kaydediyorsunuz? – Titus

cevap

1

Değeri, DB iç SQL DATALINK oluşturan PreparedStatement.setURL() ile kaydetmeyin. Normal bir dize olarak saklayın: url bir URL nesne ise url bir dize veya

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

olduğunu

pstmt.setString(2, url) 

eğer. Geri okunurken, s gibi bir değişkende String değerini okuyup yeni bir URL nesnesi oluşturun:

URL u = new URL(s) 
+0

url dizgisi veritabanına geldiği zaman bozuldu, (/) 'in yanlış olduğu ancak yöntemlerin iyi çalıştığı .. –