本文整理汇总了Java中org.postgresql.ds.PGSimpleDataSource.getConnection方法的典型用法代码示例。如果您正苦于以下问题:Java PGSimpleDataSource.getConnection方法的具体用法?Java PGSimpleDataSource.getConnection怎么用?Java PGSimpleDataSource.getConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.postgresql.ds.PGSimpleDataSource
的用法示例。
在下文中一共展示了PGSimpleDataSource.getConnection方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
@Override
public boolean run(String host, int port,Socket tunnel) throws Exception {
try {
PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setServerName(host);
ds.setPortNumber(port);
ds.setSsl(true);
///this.sslContext = InstallCert.getContext();
ds.setSslfactory(host);
ds.setSslfactory(DumperFactory.class.getName());
Connection c = ds.getConnection();
c.close();
return true;
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return false;
}
示例2: doResultTest
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
private static void doResultTest( final PGSimpleDataSource ds) {
try {
final Connection conn= ds.getConnection();
try {
resultSetTest( ds.getConnection());
} catch( final SQLException e) {
e.printStackTrace();
} finally {
if( conn != null) {
conn.close();
}
}
} catch( final SQLException e1) {
e1.printStackTrace();
}
}
示例3: doResultTestGC
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
private static void doResultTestGC( final PGSimpleDataSource ds) {
try {
final Connection conn= ds.getConnection();
try {
resultSetTestGroupCount( ds.getConnection());
} catch( final SQLException e) {
e.printStackTrace();
} finally {
if( conn != null) {
conn.close();
}
}
} catch( final SQLException e1) {
e1.printStackTrace();
}
}
示例4: getConnection
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
public Connection getConnection() throws IOException {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
Properties dataSourceProperties = new Properties();
dataSourceProperties.load(getClass().getResourceAsStream(
"datasource-test.properties"));
dataSource
.setServerName(dataSourceProperties.getProperty("serverName"));
dataSource.setDatabaseName(dataSourceProperties
.getProperty("databaseName"));
dataSource.setPortNumber(Integer.parseInt(dataSourceProperties
.getProperty("portNumber")));
dataSource.setUser(dataSourceProperties.getProperty("user"));
dataSource.setPassword(dataSourceProperties.getProperty("password"));
try {
return dataSource.getConnection();
} catch (Exception e) {
IllegalArgumentException e1 = new IllegalArgumentException(
"couldn't open database connection: " + e.getMessage());
e1.initCause(e);
throw e1;
}
}
示例5: getDbConnection2
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
private static Connection getDbConnection2(){
PGSimpleDataSource source = new PGSimpleDataSource();
source.setServerName("localhost");
source.setDatabaseName("cookbook");
source.setLoginTimeout(10);
try {
return source.getConnection();
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
示例6: INIT
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
public static synchronized void INIT() {
PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setUrl(PG_URL);
try (final Connection cnx = ds.getConnection(); final Statement st = cnx.createStatement()) {
st.execute(TestSuite.getResourceAsString(PG_SQL));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
TestSuite.setupSessionFactoryBuilder(ds);
}
示例7: getConnection
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
/**
* Returns a manual commit, read uncommitted connection.
*/
public static Connection getConnection(final String jdbcURL)
{
// for the time being, only postgresql
if (!jdbcURL.startsWith("jdbc:postgresql"))
{
return null;
}
// try
// {
// Class.forName("org.postgresql.Driver");
// }
// catch (final ClassNotFoundException e)
// {
// e.printStackTrace();
// return null;
// }
try
{
// jdbc:postgresql://localhost/jive?user=jive&password=
final PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setUser("jive");
ds.setDatabaseName("jive");
ds.setServerName("localhost");
ds.setPassword("");
final Connection conn = ds.getConnection();
conn.setAutoCommit(false);
conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
return conn;
}
catch (final SQLException e)
{
e.printStackTrace();
}
return null;
}
示例8: dropNamespace
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
/**
* Drops a namespace in the database.
*
* <b>Postcondition:</b>Assumes {@link this.hasNamespace()} == true
*
*/
public void dropNamespace(String rootUser, String rootPass)
{
// root needs to log into the application database to create the schema.
PGSimpleDataSource tempRootDatasource = new PGSimpleDataSource();
tempRootDatasource.setServerName(DatabaseProperties.getServerName());
tempRootDatasource.setPortNumber(DatabaseProperties.getPort());
tempRootDatasource.setDatabaseName(DatabaseProperties.getDatabaseName());
tempRootDatasource.setUser(rootUser);
tempRootDatasource.setPassword(rootPass);
Connection conn = null;
Statement statement = null;
try
{
conn = tempRootDatasource.getConnection();
statement = conn.createStatement();
statement.execute(" DROP SCHEMA " + this.getNamespace() + " CASCADE");
}
catch (SQLException e)
{
throw new DatabaseException(e);
}
finally
{
try
{
if (statement != null)
{
statement.close();
}
if (conn != null)
{
conn.close();
}
}
catch (Exception exception)
{
}
}
}
示例9: createNamespace
import org.postgresql.ds.PGSimpleDataSource; //导入方法依赖的package包/类
/**
* Creates a namespace in the database.
*
* <b>Postcondition:</b>Assumes {@link this.hasNamespace()} == true
*
*/
public void createNamespace(String rootUser, String rootPass)
{
// root needs to log into the application database to create the schema.
PGSimpleDataSource tempRootDatasource = new PGSimpleDataSource();
tempRootDatasource.setServerName(DatabaseProperties.getServerName());
tempRootDatasource.setPortNumber(DatabaseProperties.getPort());
tempRootDatasource.setDatabaseName(DatabaseProperties.getDatabaseName());
tempRootDatasource.setUser(rootUser);
tempRootDatasource.setPassword(rootPass);
Connection conn = null;
Statement statement = null;
try
{
String userName = DatabaseProperties.getUser();
String namespace = this.getNamespace();
conn = tempRootDatasource.getConnection();
statement = conn.createStatement();
if (!namespace.trim().equals(userName.trim()))
{
statement.execute("CREATE SCHEMA " + namespace + " AUTHORIZATION " + userName);
}
else
{
statement.execute("ALTER SCHEMA " + namespace + " OWNER TO " + userName);
}
LinkedList<String> statements = new LinkedList<String>();
statements.add("ALTER USER " + userName + " SET search_path = " + namespace + ", public");
executeAsRoot(statements, true);
}
catch (SQLException e)
{
throw new DatabaseException(e);
}
finally
{
try
{
if (statement != null)
{
statement.close();
}
if (conn != null)
{
conn.close();
}
}
catch (Exception exception)
{
}
}
}