Skip to content

Instantly share code, notes, and snippets.

@aalmiray

aalmiray/db.groovy

Last active Aug 10, 2020
Embed
What would you like to do?
Groovy/JBang
@Grab('com.oracle.database.jdbc:ojdbc8:19.7.0.0')
@Grab('com.oracle.database.jdbc:ucp:19.7.0.0')
import java.sql.Connection
import java.sql.SQLException
import java.sql.Statement
import java.sql.ResultSet
import oracle.ucp.jdbc.PoolDataSourceFactory
import oracle.ucp.jdbc.PoolDataSource
PoolDataSource pds = PoolDataSourceFactory.poolDataSource.with(true) {
connectionFactoryClassName = 'oracle.jdbc.pool.OracleDataSource'
setURL(System.properties['db.url'])
user = System.properties['db.username']
password = System.properties['db.password']
}
try (Connection conn = pds.connection) {
Statement stmt = conn.createStatement()
ResultSet rs = stmt.executeQuery('SELECT 1 FROM dual')
rs.next()
println(1 == rs.toRowResult()[0]? 'OK' : 'ERROR')
}
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.oracle.database.jdbc:ojdbc8:19.7.0.0
//DEPS com.oracle.database.jdbc:ucp:19.7.0.0
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.PoolDataSource;
import java.math.BigDecimal;
public class db {
public static void main(String... args) throws Exception {
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pds.setURL(System.getProperty("db.url"));
pds.setUser(System.getProperty("db.username"));
pds.setPassword(System.getProperty("db.password"));
try (Connection conn = pds.getConnection()) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1 FROM dual");
rs.next();
if (BigDecimal.valueOf(1).equals(rs.getObject(1))) {
System.out.println("OK");
} else {
System.out.println("ERROR");
}
}
}
}
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.oracle.database.jdbc:ojdbc8:19.7.0.0
//DEPS com.oracle.database.jdbc:ucp:19.7.0.0
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.PoolDataSource;
import java.math.BigDecimal;
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pds.setURL(System.getProperty("db.url"));
pds.setUser(System.getProperty("db.username"));
pds.setPassword(System.getProperty("db.password"));
try (Connection conn = pds.getConnection()) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1 FROM dual");
rs.next();
if (BigDecimal.valueOf(1).equals(rs.getObject(1))) {
System.out.println("OK");
} else {
System.out.println("ERROR");
}
}
@Grab("com.oracle.database.jdbc:ojdbc8:19.7.0.0")
@Grab("com.oracle.database.jdbc:ucp:19.7.0.0")
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import oracle.ucp.jdbc.PoolDataSourceFactory;
import oracle.ucp.jdbc.PoolDataSource;
@groovy.transform.CompileStatic
class db {
static void main(String... args) throws Exception {
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
pds.setURL(System.getProperty("db.url"));
pds.setUser(System.getProperty("db.username"));
pds.setPassword(System.getProperty("db.password"));
try (Connection conn = pds.getConnection()) {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1 FROM dual");
rs.next();
if (1 == rs.getObject(1)) {
System.out.println("OK");
} else {
System.out.println("ERROR");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.