2016-04-09 15 views
1

私は、BLOBおよびCLOBデータ型によるパフォーマンスの低下が原因で、ユーザーのプロファイルイメージをmysqlデータベースに格納するアプリケーションを作成していますが、イメージのURLをデータベーステーブルにvarchar値として格納します。MySQLデータベースに保存されたURLを回復できません

問題は次のとおりです。ResultSet.getURL()メソッドまたはresultSet.getString()を使用してURLを取得しようとすると、問題が発生します。私はMalformedURLException- "プロトコル:C認識されません"を取得します。どうすればこの問題を解決できますか。

ここは私のコードです。

研究と実験を通して
try { 
     sqlConnectionObject = DriverManager.getConnection(jdbcUrl,"root",""); 
    } catch (SQLException ex) { 
     Logger.getLogger(Androbot.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    fileChooser = new JFileChooser(); 
    FileFilter fileFilter = new FileNameExtensionFilter("pic", "jpg"); 
    fileChooser.addChoosableFileFilter(fileFilter); 

    obtainURLFromDBButton.addActionListener(
      new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      try { 
       JOptionPane.showMessageDialog(null, obtainImageUrl("userName")); 
       File file = new File(obtainImageUrl("userName").toString()); //the obtain image url 
                      //function is called here 
                      //and passed to File 
                      //file is used to creat image 
       Image img = ImageIO.read(file); 
       thisButton.setIcon(new ImageIcon(img)); 
      } catch (FileNotFoundException e) { 
       JOptionPane.showMessageDialog(null, "ERROR" + e.getMessage()); 
      } catch (IOException e) { 
       JOptionPane.showMessageDialog(null, "ERROR" + e.getMessage()); 
      } 
     } 
    } 
    ); 

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

      } else if (ret == JFileChooser.CANCEL_OPTION) { 
       int opt = JOptionPane.showConfirmDialog(null, "Apopprove cancel?", "", JOptionPane.OK_CANCEL_OPTION); 
       if (opt == JOptionPane.CANCEL_OPTION) { 
        fileChooser.showDialog(null, "Add Profile Picture"); 
       } 
      } else if (ret == JFileChooser.ERROR_OPTION) { 
       JOptionPane.showMessageDialog(null, "An ERROR occured try again later"); 

      } 
     } 
    } 
    ); 

//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; 
} 
//insert image url in the db 
public void addPictureToDB(File file) { 
    imageUrl = file.getAbsolutePath(); 
    try (Connection connect = DriverManager.getConnection(jdbcUrl, "root", "")) { 
     Statement mysqlStmt = connect.createStatement(); 
     String INSERT_QUERY = "INSERT INTO picture VALUES('" 
       + imageUrl + "','" 
       + "BETTY KIPPO');"; 
     mysqlStmt.execute(INSERT_QUERY); 
     JOptionPane.showMessageDialog(null, "Image Added Succefully"); 
    } catch (SQLException e) { 
     JOptionPane.showMessageDialog(null, "ERROR" + e.getMessage()); 
    } 
} 
+0

エラーメッセージは、ファイルの場所をローカルファイルの絶対パス(例: 'C:\ folder \ ...')として保存していることを示唆しています。もしそうなら、おそらくそれらをURLと考えるべきではなく、それらをVARCHARとしてデータベースに格納し、それらをString値として扱うだけです。 –

+0

...また、コード内の動的SQLを置き換えるには、 'PreparedStatement'オブジェクトと*パラメータ化されたクエリ*を使うべきです。 –

答えて

0

、私の調査結果は、私が

JFileChooser fileChooser = new JFileChooser(); 
fileChooser.getSelectedFile().getPath().toString(); 

その方法を使用すると、データベースに送信されたパス文字列が有効なURLであることであり、使用しています私はそのためのプロトコルを変更する必要はありませんonRetrieve。

+0

ファイル選択ツールを使用すると、実際のファイルパスが文字列形式で表示されます –

関連する問題