com.mntviews.upload.impl.PostgresUploadServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mnt-upload Show documentation
Show all versions of mnt-upload Show documentation
Data upload library for the different types of databases
The newest version!
package com.mntviews.upload.impl;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.mntviews.upload.UploadService;
import com.mntviews.upload.model.ConnectionData;
import com.mntviews.upload.model.RecipientData;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
public class PostgresUploadServiceImpl implements UploadService {
@Override
public void upload(ConnectionData connectionData, File file, String script) {
}
@Override
public void uploadFile(ConnectionData connectionData, File file, String script) {
ObjectMapper objectMapper = JsonMapper.builder()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.build();
try (FileInputStream fis = new FileInputStream(file)) {
RecipientData recipientData = objectMapper.readValue(script, RecipientData.class);
Properties props = new Properties();
props.setProperty("user", connectionData.getUserName());
props.setProperty("password", connectionData.getPassword());
Connection connection = DriverManager.getConnection(connectionData.getUrl(), props);
connection.setAutoCommit(false);
try (PreparedStatement ps = connection.prepareStatement("INSERT INTO " + recipientData.getSchemaName()
+ "." + recipientData.getTableName() + " (" + recipientData.getFieldName() + "," + recipientData.getSourceFileIdFieldName() + " ) VALUES (?, ?)")) {
ps.setBinaryStream(1, fis, file.length());
ps.setLong( 2, recipientData.getFileId());
ps.executeUpdate();
connection.commit();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy