How can you store content in various formats (pdf, html, txt) in the database, subd - MySQL, programming language - Java. Files must initially be stored in the database.
1 answer
Convert file (pdf, html, txt) into byte [] array and save to DB.
File pdfFile = new File("...test.pdf"); byte[] pdfData = new byte[(int) pdfFile.length()]; DataInputStream dis = new DataInputStream(new FileInputStream(pdfFile)); dis.readFully(pdfData); // read from file into byte[] array dis.close(); String myConnectionString = "jdbc:mysql:..."; String user = "user"; String password = "password"; dbConnection = DriverManager.getConnection(myConnectionString, user, password); PreparedStatement ps = dbConnection.prepareStatement( "INSERT INTO project (filename, pdf_file) VALUES (?,?)"); ps.setString(1, "test"); ps.setBytes(2, pdfData); // byte[] array ps.executeUpdate(); |