Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: java

  1. #1
    Junior Member
    Join Date
    Aug 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java

    good evening I have not received the videos in my mailbox, aufaite I have a serious problem in java that I can not solve for a week:
    I create a JFrame in which I save data including choosing a PDF file that will be saved in the mysql database then retrieve the same PDF and display it in a Panel with scrollable. Please help me please

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java

    If you have a java program you are having problems with, post the code (wrapped in code tags) so we can see it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java

    Quote Originally Posted by Norm View Post
    If you have a java program you are having problems with, post the code (wrapped in code tags) so we can see it.
    this is my CODE:

    public void insertFile() {

    filename = LabelNomFichier.getText();
    InputStream stream = new ByteArrayInputStream(filename.getBytes(StandardCha rsets.UTF_8));
    nom = txtNom.getText();

    try {

    String querySetLimit = "SET GLOBAL max_allowed_packet=904857600;"; // 10 MB
    Statement stSetLimit = cnx.createStatement();
    stSetLimit.execute(querySetLimit);

    String sql = "INSERT INTO person (first_name, photo) values (?, ?)";
    PreparedStatement statement = cnx.prepareStatement(sql);
    statement.setString(1, nom);
    statement.setBlob(2, stream);
    int row = statement.executeUpdate();
    if (row > 0) {
    System.out.println("A contact was inserted with photo image.");

    }
    // cnx.close();
    stream.close();
    } catch (SQLException ex) {
    ex.printStackTrace();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }

    private void getPDF() throws SQLException {
    String SQL = "SELECT DISTINCT photo FROM person WHERE person_id='" + Classe.idPersonn + "'";
    try {
    PreparedStatement ps = cnx.prepareStatement(SQL);
    ResultSet rs = ps.executeQuery();
    System.out.println("Following flies are downloaded from database..");
    while (rs.next()) {
    int fileId = rs.getInt("person_id");
    JOptionPane.showMessageDialog(this, Classe.idPersonn);
    // Blob fileName;
    // fileName = rs.getBlob("photo");
    // long fileSizeInKb = rs.getLong("file_size_in_kb");
    // String fileExtension = rs.getString("file_extension");
    // System.out.println("File Id:" + fileId);
    // System.out.println("File Name:" + fileName);
    // System.out.println("File Size In KB:" + fileSizeInKb);
    // System.out.println("File Extension:" + fileExtension);

    Blob blob = rs.getBlob("photo");
    InputStream inputStream = blob.getBinaryStream();
    JOptionPane.showMessageDialog(this,Classe.idPerson n);

    // ----
    // InputStream stream = new ByteArrayInputStream(blob.getBytes(StandardCharset s.UTF_8));
    // ----
    readFile((File) blob);
    System.out.println("--------------- AAAAAAA --------------------");
    Files.copy(inputStream, Paths.get(LabelPDF.getText() + fileId));
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public String readFile(File file) {
    StringBuffer fileBuffer = null;
    String fileString = null;
    String line = null;

    try {
    // String SQL = "SELECT DISTINCT photo FROM person WHERE person_id='" + Classe.idPersonn + "'";
    // PreparedStatement ps = cnx.prepareStatement(SQL);
    // ResultSet rs = ps.executeQuery();
    System.out.println("Affichage du PDF..");

    FileReader in = new FileReader(file);
    BufferedReader brd= new BufferedReader(in);
    fileBuffer=new StringBuffer();
    while ((line = brd.readLine()) !=null ) {
    fileBuffer.append(line).append(System.getProperty( "line.separator"));
    }
    in.close();
    fileString =fileBuffer.toString();
    } catch (IOException e) {
    return null;
    }
    return fileString;
    }

    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java

    What happens when you compile and execute the program?

    The code is missing the import statements, the class declaration and the main method for testing its execution.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: java

    One pitfall you're going to run into is the use of BufferedReader. That class is no good for reading binary files (such as PDF), it's only good for reading plain ASCII text files. So you'll want to use FileInputStream instead and read the bytes into a buffer array instead of reading line-by-line.

    As for displaying the PDF contents in a JPanel, Java doesn't have a built-in way of doing that. You'll probably want to look around for the right library for your purposes:

    https://stackoverflow.com/questions/...xisting-jpanel