Results 1 to 18 of 18
  1. #1
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default how to save pdf file to mysql database

    Java Code:
    public class BlagajnaVnosUpdater {
        
        String conString ="jdbc:mysql://192.168.0.1:3306/database";
        String username ="1";
        String passward ="1";
        
        FileInputStream input = null;
        
        public boolean add(String datum,String vnesel,String prihodek,String strosek,String status,String opis,String path){
              
            
            
            try{
      
                
                
            input = new FileInputStream(path);
      
    
        
             
            String sql="INSERT INTO `blagajnavnos` (`datum`, `vnesel`, `prihodek`, `strosek` , `status`, `opis`, `datoteka`) VALUES('"+datum+"','"+vnesel+"','"+prihodek+"','"+strosek+"','"+status+"','"+opis+"','"+input+"')";
            
               
                Connection con= (Connection) DriverManager.getConnection(conString, username, passward);
                
                Statement s =(Statement) con.prepareStatement(sql);
                
                s.execute(sql);
                
                return true;
            }catch(Exception e){
                e.printStackTrace();
            }
            return false;
            
        }
    When I run code I get only this in blob ----->java.io.FileInputStream@407abcbd

    Can someone help me out?

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: how to save pdf file to mysql database

    That is correct. You have added input.toString() to the statement.

    Please switch to using PreparedStatements. Concatenating an SQL string is so horribly sensitive for quote hell and security attacks.

    If you wanted to add the CONTENT of the FILE indicated by the PATH, there is some work to be done first. Java does not automagically read stuff from disk. Fortunately for you, there is a method called Files.readAllBytes that accepts a Path object from Paths.get().
    Java Code:
    byte[] bytes = Files.readAllBytes(Paths.get(input));
    You now have a byte[] to fill any blob you might have in your table using PreparedStatement.setBytes().

    You can also use PreparedStatement.setBinaryStream() for this.
    Last edited by SurfMan; 11-18-2016 at 12:23 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: how to save pdf file to mysql database

    Thanks for quick answer SurfMan.

    Can you please switch my code to PreparedStatements because I don't know how to do it properly.

    Thanks in advance

  4. #4
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: how to save pdf file to mysql database

    Quote Originally Posted by lpt2007 View Post
    Thanks for quick answer SurfMan.

    Can you please switch my code to PreparedStatements because I don't know how to do it properly.

    Thanks in advance
    https://docs.oracle.com/javase/tutor.../prepared.html

    I'll answer questions if you show me your effort.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: how to save pdf file to mysql database

    I figured out :)

    Java Code:
    public class BlagajnaVnosUpdater {
        
        
        public boolean add(String datum,String vnesel,String prihodek,String strosek,String status,String opis,String path){
              
            try{
                Connection con = (Connection) DriverManager.getConnection
          ("jdbc:mysql://192.168.0.1:3306/database","1","1");
                PreparedStatement updateemp = con.prepareStatement
                ("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)");
                
               
                updateemp.setString(1,datum);
                updateemp.setString(2,vnesel);
                updateemp.setString(3,prihodek);
                updateemp.setString(4,strosek);
                updateemp.setString(5,status);
                updateemp.setString(6,opis);
                byte[] bytes = Files.readAllBytes(Paths.get(path));
                updateemp.setBytes(7,bytes);
                updateemp.executeUpdate();
                
              
                return true;
                
            }catch(Exception e){
                e.printStackTrace();
            }
    Thanks for help.

  6. #6
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: how to save pdf file to mysql database

    Quote Originally Posted by lpt2007 View Post
    I figured out :)

    Thanks for help.
    That is good looking code. Well done. Remember to close the PreparedStatement and the Connection when you're done with it. In Java 8 you can do it with a try-with-resources block, see example below. Note how the Connection and PreparedStatement are created between the parenthesis of try (...) . This means it will auto-close them when the block is done.
    Java Code:
      try ( Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.0.1:3306/database","1","1");
             PreparedStatement updateemp = con.prepareStatement("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)") ) {
    
                updateemp.setString(1,datum);
                updateemp.setString(2,vnesel);
                updateemp.setString(3,prihodek);
                updateemp.setString(4,strosek);
                updateemp.setString(5,status);
                updateemp.setString(6,opis);
                byte[] bytes = Files.readAllBytes(Paths.get(path));
                updateemp.setBytes(7,bytes);
                updateemp.executeUpdate();
    
            }catch(Exception e){
                e.printStackTrace();
            }
    Last edited by SurfMan; 11-18-2016 at 04:20 PM. Reason: typo
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: how to save pdf file to mysql database

    If I not select file I get an error. How to skip without error If I won't select a file?

  8. #8
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: how to save pdf file to mysql database

    if there is no file, you probably need to insert null in the database:
    Java Code:
    try ( Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.0.1:3306/database","1","1");
           PreparedStatement updateemp = con.prepareStatement("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)") ) {
     
              updateemp.setString(1,datum);
              updateemp.setString(2,vnesel);
              updateemp.setString(3,prihodek);
              updateemp.setString(4,strosek);
              updateemp.setString(5,status);
              updateemp.setString(6,opis);
              if( path != null) {
                byte[] bytes = Files.readAllBytes(Paths.get(path));
                updateemp.setBytes(7,bytes);
              }
              else {
                 updateemp.setNull(7, Types.BLOB);
              }
              updateemp.executeUpdate();
     
          }catch(Exception e){
              e.printStackTrace();
          }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: how to save pdf file to mysql database

    Quote Originally Posted by SurfMan View Post
    if there is no file, you probably need to insert null in the database:
    Java Code:
    try ( Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.0.1:3306/database","1","1");
           PreparedStatement updateemp = con.prepareStatement("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)") ) {
     
              updateemp.setString(1,datum);
              updateemp.setString(2,vnesel);
              updateemp.setString(3,prihodek);
              updateemp.setString(4,strosek);
              updateemp.setString(5,status);
              updateemp.setString(6,opis);
              if( path != null) {
                byte[] bytes = Files.readAllBytes(Paths.get(path));
                updateemp.setBytes(7,bytes);
              }
              else {
                 updateemp.setNull(7, Types.BLOB);
              }
              updateemp.executeUpdate();
     
          }catch(Exception e){
              e.printStackTrace();
          }
    This code not working error "java.nio.file.AccessDeniedException:"

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: how to save pdf file to mysql database

    Did you look up what causes that exception?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: how to save pdf file to mysql database

    Quote Originally Posted by Tolls View Post
    Did you look up what causes that exception?
    Line 11 causes that exception.
    Java Code:
    byte[] bytes = Files.readAllBytes(Paths.get(path));

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

    Default Re: how to save pdf file to mysql database

    I meant, did you look up what that exception means.
    In the API docs.
    It's pretty clear.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: how to save pdf file to mysql database

    I only wanna add null to "datoteka" if "path" is not choosen.

  14. #14
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: how to save pdf file to mysql database

    So apparently the check path != null returns true. What about the value of path? Is that perhaps an empty String? Sprinkle print statements in there, or better yet, use a debugger to step through the code.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  15. #15
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: how to save pdf file to mysql database

    Quote Originally Posted by SurfMan View Post
    So apparently the check path != null returns true. What about the value of path? Is that perhaps an empty String? Sprinkle print statements in there, or better yet, use a debugger to step through the code.
    I already do some tests. Error only occurs when "path" is empty or not correct.

  16. #16
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: how to save pdf file to mysql database

    Quote Originally Posted by lpt2007 View Post
    I already do some tests. Error only occurs when "path" is empty or not correct.
    So there is your cause. Now you need to prevent this code from running when path is empty or not correct.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  17. #17
    lpt2007 is offline Member
    Join Date
    Nov 2016
    Posts
    31
    Rep Power
    0

    Default Re: how to save pdf file to mysql database

    I don't now why but I added "String test" whitch have value 0 when path = null and 1 when file is attached.

    Java Code:
    String test = null;    
                 if(jTextField1.getText().equals("")) {
                     jTextField1.setText("brez");
                     test = "1";
                  }else {  
                     test = "0";
                  }
    Java Code:
    public class BlagajnaVnosUpdater {
        
        public boolean add(String datum,String vnesel,String prihodek,String strosek,String status,String opis,String path,String test){
              
            try ( Connection con = (Connection) MySQLConnect.getConnection();
                   
           PreparedStatement UpdBlagajna = con.prepareStatement("insert into blagajnavnos (datum,vnesel,prihodek,strosek,status,opis,datoteka) values (?,?,?,?,?,?,?)") ) {
              UpdBlagajna.setString(1,datum);
              UpdBlagajna.setString(2,vnesel);
              UpdBlagajna.setString(3,prihodek);
              UpdBlagajna.setString(4,strosek);
              UpdBlagajna.setString(5,status);
              UpdBlagajna.setString(6,opis);
              System.out.println(test);
              System.out.println(path);
              if( test != "1" ) {
                byte[] bytes = Files.readAllBytes(Paths.get(path));
                UpdBlagajna.setBytes(7,bytes);
                }
                else {
                UpdBlagajna.setNull(7, Types.BLOB);
                }
                UpdBlagajna.executeUpdate();
     
              return true;
              
          }catch(Exception e){
              e.printStackTrace();
          }
    
       
            return false;
            
        }

  18. #18
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: how to save pdf file to mysql database

    You should compare Strings with equals and not ==: test != "1" would be !test.equals("1")

    Funny how you did it right in line 3 of the first snippet.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Cannot save data in mysql database
    By nayeem in forum New To Java
    Replies: 4
    Last Post: 11-09-2016, 03:52 PM
  2. (in Java mysql to excel) where i have specify the path to save the file
    By raj.mscking@gmail.com in forum New To Java
    Replies: 3
    Last Post: 06-04-2012, 10:23 AM
  3. Upload file in jsp and save it to MYSQL
    By juhiswt in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 05-03-2012, 07:38 AM
  4. Replies: 3
    Last Post: 03-12-2012, 02:03 PM
  5. save file to database
    By katerinaaa in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 12:15 PM

Posting Permissions

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