Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- File currentDir = new File("C:\Users\luccaskammer\Desktop\Imagens\"); // Define o diretorio a ser lido
- displayDirectoryContents(currentDir);
- }
- public static void displayDirectoryContents(File dir) {
- try {
- ImagensParaDBController contr = new ImagensParaDBController();
- File[] files = dir.listFiles();
- DeletePng del = new DeletePng();
- //ConvertData conv = new ConvertData();
- for (File file : files) {
- ImagensParaBD img = new ImagensParaBD();
- ImagensParaBD name = new ImagensParaBD();
- ImagensParaBD picture = new ImagensParaBD();
- if (file.isDirectory())
- {
- System.out.println("directory:" + file.getCanonicalPath());
- displayDirectoryContents(file);
- }
- else if(file.isFile()) {
- System.out.println(" file:" + file.getCanonicalPath());
- img.setName(del.deletePng(file.getName()));
- java.nio.file.Path filelocation = Paths.get(file.getCanonicalPath());
- img.setPicture(Files.readAllBytes(filelocation));
- contr.insertImageDB(img, picture, name);
- }
- else
- {
- System.out.println("invalid");
- }
- }
- contr.disconnect();
- }
- catch (IOException e) {
- }
- }
- }
- @Entity
- @Table(name = "planegeo")
- public class ImagensParaBD implements Serializable {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;
- //private String pathFolder;
- private String name;
- @Lob
- private byte[] picture;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public byte[] getPicture() {
- return picture;
- }
- public void setPicture(byte[] picture) {
- this.picture = picture;
- }
- }
- public class ImagensParaDBController {
- EntityManagerFactory mf = Persistence.createEntityManagerFactory("mySQLPU");
- EntityManager em = mf.createEntityManager();
- public void insertImageDB(ImagensParaBD img, ImagensParaBD name, ImagensParaBD picture) {
- em.getTransaction().begin();
- em.merge(img);
- em.getTransaction().commit();
- }
- public void disconnect() {
- em.close();
- }
- public void insertImageDB(ImagensParaBD img) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
- List<ImagensParaBD> all = em.createQuery("Select a From ImagensParaBD a where name=:name",
- ImagensParaBD.class)
- .setParameter("name", theNameTOFind)
- .getResultList();
Add Comment
Please, Sign In to add comment