1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package dataaccess.jpa;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.postgresql.Driver;
public class JdbcConnection {
private static String host = "174.136.152.13:5432";
private static String base = "compagno_m1ps";
private static String user = "compagno_postgres";
private static String password = "xxxxxxxx";
private static String url = "jdbc:postgresql://" + host + "/" + base;
/* Declare private variables */
private static Connection con;
private static Driver PgsqlDriver;
public JdbcConnection() throws SQLException, ClassNotFoundException,
InstantiationException {
}
public synchronized static Connection getCon() throws SQLException,
ClassNotFoundException, IllegalAccessException,
InstantiationException {
if (con == null)
setCon();
return con;
}
public synchronized static void setCon() throws SQLException,
ClassNotFoundException, IllegalAccessException,
InstantiationException {
/* Set up JDBC */
if (con != null)
return;
PgsqlDriver = (Driver) Class.forName("org.postgresql.Driver")
.newInstance();
con = DriverManager.getConnection(url, user, password);
/* Done connecting to DB */;
}
public synchronized void setCon(Connection connect) throws SQLException,
ClassNotFoundException, IllegalAccessException,
InstantiationException {
con = connect;
}
/**
* @return statement
* @throws InstantiationException
* @throws IllegalAccessException
* @throws ClassNotFoundException
*/
public static Statement getStatement() throws ClassNotFoundException,
IllegalAccessException, InstantiationException {
try {
return JdbcConnection.getCon().createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY,
ResultSet.HOLD_CURSORS_OVER_COMMIT);
} catch (SQLException e) {
System.err.println(e.getMessage());
System.exit(-1);
}
return null;
}
} |
Partager