Page 1 of 2 12 LastLast
Results 1 to 20 of 30
Like Tree2Likes

Thread: Import image to embedded database and display into application?

  1. #1
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Import image to embedded database and display into application?

    Hello all.

    I have working whit nebeans 7, making embedded database. I have make tables and forms for application what i need. Now I have problem, actualy i dont know how to deal whit it, becouse i dont know what way to apply it.

    Problem: I have table whit and i leave a 2 blob colums for image, when i have been look for example or eaven explanation for that i got stuck. My goal is to make something like this A Swing Demo (The Java™ Tutorials > Graphical User Interfaces > A Brief Introduction to the Swing Package).

    When i build aplication i wana to have 2 buttons or like in that tutorial two image holder when i click on button or image to load in to database whit jFilechooser, and show at application.

    Second option is to make button for input a link to image on Hdd and show to application.

    I am not sure what to use, i am not expiriance what is better to doo image in to database or only link wher is image and then show to application.

    Suggestions and link to tutorials would be great. Other problem is i am working in Netbeans so there is all difrente then writing a code by myself.

    I would be greatfull for some advice and tips to achive this simple dilema.

    Thanks on your time and effort.

  2. #2
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Bump. Anyone something ? If is needed additional explanation I would be great to say it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Import image to embedded database and display into application?

    Taking the GUI out of it for a moment, do you know how to store and retrieve images (BLOBs) from your embedded database using JDBC?

  4. #4
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Yea, i read some tutorials, and i make some test table, on gui i make Jfilechooser and import some code to manage a uplaod a photo. for some of tuts like Store image into and load image from JavaDB using EclipseLink | MrTextminer’s Weblog or Insert an Image : Blob Clob*«*Database*«*Java Tutorial

    My question is about functionality for the image manipulation. A bit i am confused and lost whit image whit imbedded database. Some time i think is better to put in to DB or just to use extended folder on hdd for the image storige.

  5. #5
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Yea, i read some tutorials, and i make some test table, on gui i make Jfilechooser and import some code to manage a uplaod a photo. for some of tuts like Store image into and load image from JavaDB using EclipseLink | MrTextminer’s Weblog or Insert an Image : Blob Clob*«*Database*«*Java Tutorial

    My question is about functionality for the image manipulation. A bit i am confused and lost whit image whit imbedded database. Some time i think is better to put in to DB or just to use extended folder on hdd for the image storige.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Import image to embedded database and display into application?

    It's no different to working with files, once you get the stream.
    Maybe if you show us some code, say the select statement for retrieving an image up to the point you create a BufferedImage (or ImageIcon or whatever).

  7. #7
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Quote Originally Posted by Tolls View Post
    It's no different to working with files, once you get the stream.
    Maybe if you show us some code, say the select statement for retrieving an image up to the point you create a BufferedImage (or ImageIcon or whatever).
    First thanks on your time and effort. I will try to doo something at netbeans, going on insert image into database. So i will post code when i am get somehwer.

  8. #8
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    When i try to make anything in the netbeans source code all got wrong. I cant eaven make a Jfilechooser to implement whit connection code.

    Java Code:
      public void main(String[] argv) throws Exception {
        String url = "jdbc:derby:bobo;create=true";
        String dbName = "bobo";
        String userName = "app";
        String password = "app";
    
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection con = DriverManager.getConnection(url + dbName, userName, password);
        File imgfile = new File("images.jpg");
        FileInputStream fin = new FileInputStream(imgfile);
        PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
        pre.setInt(1, 5);
        pre.setString(2, "A");
        pre.setBinaryStream(3, fin, (int) imgfile.length());
        pre.executeUpdate();
        pre.close();
        con.close();
      }
    }
    This is what i try to implenet into Jfilechooser and all vent red when i try to doo that. Eaven shift+ctrl I wont work on that. Whit that comand project wont run becouse errors.

    I will try to get something how to get a jfilechoser to work whit upload of image. If anyone have some tip i would like to use it.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Import image to embedded database and display into application?

    If it "all goes red" then that implies errors...which Netbeans should tell you about.

  10. #10
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Yea i know. That is a problem. When i insert jFilechooser and only option is to put action listiner to show him, i dont get a option to insert code what to doo when he hit save. Whatever i put inside of listiner netbeans show all above code as notfunctional or errors.

    So i am thinking to make button when is pressed to call in a New Jfilechooser what i will make whit custom code. That is what i am aiming. I try lastnight all ways to approach whit inserting code. Apperently ther is a option for connecting jfilechooser whit database whit generated code "Gether and seter" option in netbeans source code.

    For now i will try to make new Jfilechooser.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Import image to embedded database and display into application?

    Are you using the GUI builder?
    If so then I can't help....I don't touch that thing if I can possibly help it.

  12. #12
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    I tryed gui and handcode. Anyway I make new javafile in project and i get code there and its working till upload to database. When i try to Use in project then is not working.

    when i run file he pop out nicly and i can select files, but not uploading, i am talking about javafile what i make whitin project.

    Code is like this I know i am missing something but still working on it

    Java Code:
    package imgz;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    /**
     *
     * @author Ogre
     */
    
        public class InsertImage {
                static JTextField text=new JTextField(20);
                static JButton button=new JButton("Save");
                static JPanel panel=new JPanel();
    
        public static void main(String[] args) {
                JFileChooser chooser = new JFileChooser();
                int returnVal = chooser.showOpenDialog(null);
                final File file ;
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
                            file = chooser.getSelectedFile();
                            final String name=file.getName();
                    System.out.println(name);
                    text.setText(name);
                    panel.add(text);
                    panel.add(button);
                    button.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                                try{
                        Connection con = null;
                        String url = "jdbc:derby:bobo;create=true";;
                        String db = "test";
                        String driver = "org.apache.derby.jdbc.EmbeddedDriver";
                        String user = "app";
                        String pass = "app";
                        FileInputStream fis;
                        Class.forName(driver);
                        con = DriverManager.getConnection(url+db, user, pass);
                        PreparedStatement st = con.prepareStatement("insert into PHOTO(file_name,file_data) values(?,?)");
                        fis = new FileInputStream(file);
                        st.setString(1,name);
                        st.setBinaryStream(2, (InputStream)fis, (int)(file.length()));
                        int s = st.executeUpdate();
                        JOptionPane.showMessageDialog(null,"image is successfully inserted.");
                }
    catch(Exception ex){}
            }
           });
        JFrame f=new JFrame("Insert Image");
            f.add(panel);
            f.setVisible(true);
            f.setSize(300,100);
          }
    }
    }
    I think that i wrong at prepareStatements, but

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Import image to embedded database and display into application?

    Java Code:
    catch(Exception ex){}
    You are eating exceptions, so when the code throws an exception you will never know about it.
    Add ex.printStackTrace() in there.

  14. #14
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Great that was nice, got error moustly .newEmbedSQLException(Unknown Source), but that is good now i know that file is going but no DB to accept it.


    Rest is moustly errors at generated code from Netbeans.


    java.sql.SQLException: JDBC attribute 'create' has an invalid value 'truetest', valid values are '{true|false}'.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.g etSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLExcepti on(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.newEmbedSQLExcepti on(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLExcep tion(Unknown Source)
    at org.apache.derby.jdbc.InternalDriver.checkEnumerat ion(Unknown Source)
    at org.apache.derby.jdbc.InternalDriver.checkBoolean( Unknown Source)
    at org.apache.derby.jdbc.InternalDriver.getAttributes (Unknown Source)
    at org.apache.derby.jdbc.InternalDriver.connect(Unkno wn Source)
    at org.apache.derby.jdbc.AutoloadedDriver.connect(Unk nown Source)
    at java.sql.DriverManager.getConnection(DriverManager .java:582)
    at java.sql.DriverManager.getConnection(DriverManager .java:185)
    at imgz.InsertImage$1.actionPerformed(InsertImage.jav a:54)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6288)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:605 3)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4651)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 81)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 81)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 613)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Caused by: java.sql.SQLException: JDBC attribute 'create' has an invalid value 'truetest', valid values are '{true|false}'.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.get SQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.w rapArgsForTransportAcrossDRDA(Unknown Source)
    ... 48 more
    Last edited by ogre; 01-04-2012 at 07:00 PM.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Import image to embedded database and display into application?

    There you go.
    Your semi colon was in the wrong place.

  16. #16
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Nah is not it. I think connection is problem or string driver, or way how i aprouch it. /edit, sry i was mean "When i remove semi colon is same errors" But i wasnt write here.

    I think i should use this model Connect to Java DB (Derby) with org.apache.derby.jdbc.EmbeddedDriver : Java DB Derby*«*Database*«*Java Tutorial
    Last edited by ogre; 01-04-2012 at 08:34 PM.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Import image to embedded database and display into application?

    No.
    Your semi-colon is in the wrong place:
    Java Code:
    String url = "jdbc:derby:bobo;create=true";;  <-- right here.
    String db = "test";
    That first semi colon should be inside the quotes.

  18. #18
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Quote Originally Posted by Tolls View Post
    No.
    Your semi-colon is in the wrong place:
    Java Code:
    String url = "jdbc:derby:bobo;create=true";;  <-- right here.
    String db = "test";
    That first semi colon should be inside the quotes.
    I have done that, and is same error. I have trying to solve this, and no resultats, i am thinking on other solutions.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Import image to embedded database and display into application?

    What is the current code then and what is the full exception?
    Because the code above is causing that exception...

  20. #20
    ogre is offline Member
    Join Date
    Jun 2011
    Posts
    17
    Rep Power
    0

    Default Re: Import image to embedded database and display into application?

    Code for imageupload

    Java Code:
    package img1;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    /**
     *
     * @author Ogre
     */
    public class insertImage {
                static JTextField text=new JTextField(20);
                static JButton button=new JButton("Save");
                static JPanel panel=new JPanel();
     
        public static void main(String[] args) {
                JFileChooser chooser = new JFileChooser();
                int returnVal = chooser.showOpenDialog(null);
                final File file ;
                        if(returnVal == JFileChooser.APPROVE_OPTION) {
                            file = chooser.getSelectedFile();
                            final String name=file.getName();
                    System.out.println(name);
                    text.setText(name);
                    panel.add(text);
                    panel.add(button);
                    button.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                                try{
                        Connection con = null;
                        String url = "jdbc:derby:bobo;create=true;";
                        String db = "test";
                        String driver = "org.apache.derby.jdbc.EmbeddedDriver";
                        String user = "app";
                        String pass = "app";
                        FileInputStream fis;
                        Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                        con = DriverManager.getConnection(url+db, user, pass);
                        PreparedStatement st = con.prepareStatement("insert into PHOTO(Image) values(?,?)");
                        fis = new FileInputStream(file);
                        st.setString(1,name);
                        st.setBinaryStream(2, (InputStream)fis, (int)(file.length()));
                        int s = st.executeUpdate();
                        JOptionPane.showMessageDialog(null,"image is successfully inserted.");
                }
    catch(Exception ex){
       ex.printStackTrace(); 
    }
            }
           });
        JFrame f=new JFrame("Insert Image");
            f.add(panel);
            f.setVisible(true);
            f.setSize(300,100);
          }
    }
    }
    And error code

    Java Code:
    java.sql.SQLException: The URL 'jdbc:derby:bobo;create=true;test' is not properly formed.
    	at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    	at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    	at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
    	at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    	at org.apache.derby.jdbc.InternalDriver.getAttributes(Unknown Source)
    	at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
    	at org.apache.derby.jdbc.AutoloadedDriver.connect(Unknown Source)
    	at java.sql.DriverManager.getConnection(DriverManager.java:582)
    	at java.sql.DriverManager.getConnection(DriverManager.java:185)
    	at img1.insertImage$1.actionPerformed(insertImage.java:53)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    	at java.awt.Component.processMouseEvent(Component.java:6288)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    	at java.awt.Component.processEvent(Component.java:6053)
    	at java.awt.Container.processEvent(Container.java:2041)
    	at java.awt.Component.dispatchEventImpl(Component.java:4651)
    	at java.awt.Container.dispatchEventImpl(Container.java:2099)
    	at java.awt.Component.dispatchEvent(Component.java:4481)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    	at java.awt.Container.dispatchEventImpl(Container.java:2085)
    	at java.awt.Window.dispatchEventImpl(Window.java:2478)
    	at java.awt.Component.dispatchEvent(Component.java:4481)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    	at java.awt.EventQueue.access$000(EventQueue.java:84)
    	at java.awt.EventQueue$1.run(EventQueue.java:602)
    	at java.awt.EventQueue$1.run(EventQueue.java:600)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    	at java.awt.EventQueue$2.run(EventQueue.java:616)
    	at java.awt.EventQueue$2.run(EventQueue.java:614)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Caused by: java.sql.SQLException: The URL 'jdbc:derby:bobo;create=true;test' is not properly formed.
    	at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    	at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
    Tryed to find solution to insert a Path of image to database varchar insted of blob and then display it. So Basicly just insert link to photo and display in jlabel.

    I dont think that netbeans is best idea for building a Embedded app.

Page 1 of 2 12 LastLast

Similar Threads

  1. Embedded Database with Multi-Platform support
    By StormyWaters in forum JDBC
    Replies: 12
    Last Post: 12-12-2011, 07:20 PM
  2. Replies: 1
    Last Post: 07-28-2011, 09:48 AM
  3. Embedded Database ?
    By Prajin in forum JDBC
    Replies: 8
    Last Post: 09-27-2010, 07:44 AM
  4. Replies: 1
    Last Post: 10-06-2008, 03:55 AM
  5. How to display a database-image in a page?
    By simon in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •