All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.audit.PostgresDBUtil Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.audit;

import org.bdware.doip.audit.writer.AuditLog;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PostgresDBUtil extends BasicDB{
    public PostgresDBUtil(String driverClass, String url, String user, String password) {
        super(driverClass, url, user, password);
    }

    public boolean add(String tableName, String req_hash, String req_msg, String resp_hash, String resp_msg) throws SQLException {
        try {
            Connection conn = getConn();
            Statement statement = conn.createStatement();
            String sql = "insert into ? (req_hash, req_msg, resp_hash, resp_msg ) values (?, ?, ?, ?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, formatTableName(tableName));
            ps.setString(2, req_hash);
            ps.setString(3, req_msg);
            ps.setString(4, resp_hash);
            ps.setString(5, resp_msg);
            statement.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean delete(String tableName, String req_hash, String resp_hash) throws SQLException {
        try {
            Connection conn = getConn();
            Statement statement = conn.createStatement();
            String sql = "delete from ? where req_hash=? and resp_hash=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, formatTableName(tableName));
            ps.setString(2, req_hash);
            ps.setString(3, resp_hash);
            statement.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean update(String tableName, String req_hash, String req_msg, String resp_hash, String resp_msg) throws SQLException {
        try {
            Connection conn = getConn();
            Statement statement = conn.createStatement();
            String sql = "update ? set req_msg=?, resp_msg=? where values req_hash=? and resp_hash=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, formatTableName(tableName));
            ps.setString(2, req_msg);
            ps.setString(3, resp_msg);
            ps.setString(4, req_hash);
            ps.setString(5, resp_hash);
            statement.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public List query(String tableName, String req_hash, String resp_hash) throws SQLException {
        try {
            Connection conn = getConn();
            Statement statement = conn.createStatement();
            String sql = "select * from ? where req_hash=? and resp_hash=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, formatTableName(tableName));
            ps.setString(2, req_hash);
            ps.setString(3, resp_hash);
            ResultSet rs = statement.executeQuery(sql);
            List ret = new ArrayList<>();
            while (rs.next()) {
                AuditLog auditLog = new AuditLog();
//                auditLog.req_hash = req_hash;
//                auditLog.resp_hash = resp_hash;
//                auditLog.req_msg = rs.getString("req_msg");
//                auditLog.resp_msg = rs.getString("resp_msg");
                ret.add(auditLog);
            }
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy