package com.atm.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DbUtil { protected static PreparedStatement ps = null; protected static ResultSet rs = null; protected static Connection conn = null; public static synchronized Connection getConn() { try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/seb", "postgres", "gitops123"); } catch (Exception e) { System.err.println("Load org.postgresql.Driver Failed,Check pgJDBC jar is not Exist!"); return null; } return conn; } public static void close() { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (SQLException e) { System.err.println("Close DB link Failed!"); return; } } public static PreparedStatement executePreparedStatement(String sql) { try { ps = getConn().prepareStatement(sql); } catch (Exception e) { System.err.println("Create PreparedStatement Failed!"); return null; } return ps; } public static ResultSet executeQuery(String sql) { try { ps = executePreparedStatement(sql); rs = ps.executeQuery(); } catch (SQLException e) { System.err.println("Execute Query Failed!"); return null; } return rs; } public static void executeUpdate(String sql) { try { ps = executePreparedStatement(sql); ps.executeUpdate(); } catch (SQLException e) { System.err.println("Execute Update Failed!"); } finally { try { if (ps != null) ps.close(); } catch (SQLException e) { System.err.println("Close Resources Failed!"); return; } } } }// end DbUtil