How to upload file to MySQL database in java application GUI?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ding
    New Member
    • Mar 2007
    • 14

    How to upload file to MySQL database in java application GUI?

    I am facing problem to writting file to MySQL database in java application. Can show me a example code? Thanks!
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by ding
    I am facing problem to writting file to MySQL database in java application. Can show me a example code? Thanks!
    Have a look at this.

    What part are you having trouble with?

    Comment

    • ding
      New Member
      • Mar 2007
      • 14

      #3
      Originally posted by sicarie
      Have a look at this.

      What part are you having trouble with?

      I trouble in part reading file from FileChooser.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        What trouble are you having with FileChoosers? Is there an error message, are you not getting the right directory....?

        Comment

        • ding
          New Member
          • Mar 2007
          • 14

          #5
          Originally posted by sicarie
          What trouble are you having with FileChoosers? Is there an error message, are you not getting the right directory....?
          I create two function. one is for looking file function while another is upload file function. I have error when sending filename from looking file function to upload file function.

          The error code is:

          incompatible types
          found : java.io.File
          required: java.lang.Strin g
          String fileName = actionFindfile( );
          ^
          1 error

          Tool completed with exit code 1

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by ding
            I create two function. one is for looking file function while another is upload file function. I have error when sending filename from looking file function to upload file function.

            The error code is:

            incompatible types
            found : java.io.File
            required: java.lang.Strin g
            String fileName = actionFindfile( );
            ^
            1 error

            Tool completed with exit code 1
            Ok, so it's saying that actionFindfile( ) is not returning a String. Can you post the declaration of actionFindfile( )?

            Comment

            • ding
              New Member
              • Mar 2007
              • 14

              #7
              Originally posted by sicarie
              Ok, so it's saying that actionFindfile( ) is not returning a String. Can you post the declaration of actionFindfile( )?
              The declaration of actionFindfile is:

              public File actionFindfile( ) {

              JFileChooser fileChooser = new JFileChooser();
              fileChooser.set FileSelectionMo de( JFileChooser.FI LES_AND_DIRECTO RIES);

              int result = fileChooser.sho wOpenDialog(thi s);
              if(result == JFileChooser.CA NCEL_OPTION)
              System.exit(1);

              File fileName = fileChooser.get SelectedFile();

              if ((fileName == null) || (fileName.getNa me().equals("") ))
              {
              JOptionPane.sho wMessageDialog( this, "Invalid File Name", "Invalid File Name", JOptionPane.ERR OR_MESSAGE);
              System.exit(1);
              }
              return fileName;

              }

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by ding
                The declaration of actionFindfile is:

                public File actionFindfile( ) {

                JFileChooser fileChooser = new JFileChooser();
                fileChooser.set FileSelectionMo de( JFileChooser.FI LES_AND_DIRECTO RIES);

                int result = fileChooser.sho wOpenDialog(thi s);
                if(result == JFileChooser.CA NCEL_OPTION)
                System.exit(1);

                File fileName = fileChooser.get SelectedFile();

                if ((fileName == null) || (fileName.getNa me().equals("") ))
                {
                JOptionPane.sho wMessageDialog( this, "Invalid File Name", "Invalid File Name", JOptionPane.ERR OR_MESSAGE);
                System.exit(1);
                }
                return fileName;

                }
                And there's your issue. You are returning a File, and trying to save it in a String. What do you want to do here? Turn the filename into a string, or just return the file and use it later?

                Comment

                • ding
                  New Member
                  • Mar 2007
                  • 14

                  #9
                  Originally posted by sicarie
                  And there's your issue. You are returning a File, and trying to save it in a String. What do you want to do here? Turn the filename into a string, or just return the file and use it later?
                  i wan to return the value of the file and filename. And both will be calling in function upload file so tat file can be upload.

                  Comment

                  • sicarie
                    Recognized Expert Specialist
                    • Nov 2006
                    • 4677

                    #10
                    Originally posted by ding
                    i wan to return the value of the file and filename. And both will be calling in function upload file so tat file can be upload.
                    So to get your code working, you can change your declaration to File (instead of String). That will remove that code, and then you can use the getFileName function you use in the actionFindfile to get the name - I'm pretty sure that returns a string.

                    Comment

                    • ding
                      New Member
                      • Mar 2007
                      • 14

                      #11
                      Originally posted by sicarie
                      So to get your code working, you can change your declaration to File (instead of String). That will remove that code, and then you can use the getFileName function you use in the actionFindfile to get the name - I'm pretty sure that returns a string.
                      So how to get the value of file in function upload file?

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        Originally posted by ding
                        So how to get the value of file in function upload file?
                        Not sure what you are asking. As long as you declare it as File, you can call that same function to get the filename, and then pass that variable you declared.

                        Comment

                        • ding
                          New Member
                          • Mar 2007
                          • 14

                          #13
                          Originally posted by sicarie
                          Not sure what you are asking. As long as you declare it as File, you can call that same function to get the filename, and then pass that variable you declared.

                          I have error in function upload file
                          The code is :

                          private void actionAdd() {

                          Class.forName(" com.mysql.jdbc. Driver");
                          Connection con=DriverManag er.getConnectio n("jdbc:mysql ://localhost/im","root","123 4");

                          File fileName = actionFindfile( );
                          PreparedStateme nt ps = con.prepareStat ement("insert into assignment values('henry', '"+fileName.toS tring() + "',?)");
                          int fileLength = (int) fileName.length ();
                          InputStream streamedFile = new FileInputStream (fileName);
                          ps.setBinaryStr eam(1, streamedFile, fileLength);
                          ps.executeUpdat e();
                          ps.close();
                          streamedFile.cl ose();

                          }

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by ding
                            I have error in function upload file
                            The code is :

                            private void actionAdd() {

                            Class.forName(" com.mysql.jdbc. Driver");
                            Connection con=DriverManag er.getConnectio n("jdbc:mysql ://localhost/im","root","123 4");

                            File fileName = actionFindfile( );
                            PreparedStateme nt ps = con.prepareStat ement("insert into assignment values('henry', '"+fileName.toS tring() + "',?)");
                            int fileLength = (int) fileName.length ();
                            InputStream streamedFile = new FileInputStream (fileName);
                            ps.setBinaryStr eam(1, streamedFile, fileLength);
                            ps.executeUpdat e();
                            ps.close();
                            streamedFile.cl ose();

                            }
                            Ummmm, I wouldn't recommend connecting as root, but besides that... did you associate the driver with the project, either in the classpath, or attach it as a library?

                            Comment

                            • ding
                              New Member
                              • Mar 2007
                              • 14

                              #15
                              Originally posted by sicarie
                              Ummmm, I wouldn't recommend connecting as root, but besides that... did you associate the driver with the project, either in the classpath, or attach it as a library?
                              i already install JDBC driver and already successful connecting to database through other program.

                              Comment

                              Working...