You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package org.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://localhost:5432/cstatm-mte", "postgres", "gitops123");
} catch (Exception e) {
System.err.println("Load org.postgresql.Driver FailedCheck 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