Java Utililty Methods DataSource

List of utility methods to do DataSource

  1. HOME
  2. Java
  3. D
  4. DataSource

Description

The list of methods to do DataSource are organized into topic(s).

Method

void assertNotNull(DataSource dataSource)
Throws IllegalArgumentException if provided datasource is NULL
if (dataSource == null) {
 throw new IllegalArgumentException("Provided datasource instance is NULL, can't detect database type");
int[] batchExecute(DataSource ds, String sql, Object[]... args)
batch Execute
if (ds == null || sql == null || sql.trim().length() == 0)
 return null;
int[] result = new int[] { 0 };
Connection con = null;
PreparedStatement pstmt = null;
try {
 con = ds.getConnection();
 pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
...
void checkTableExists(DataSource dataSource, String tableName)
check Table Exists
Connection connection = null;
try {
 connection = dataSource.getConnection();
 checkTableExists(connection, tableName);
} finally {
 closeQuietly(connection);
void cleanDB(String ip, DataSource dataSource)
clean DB
String DELETE = "DELETE FROM jpstats_statistics WHERE ip = ? ";
PreparedStatement prepStat = null;
Connection conn = dataSource.getConnection();
prepStat = conn.prepareStatement(DELETE);
try {
 prepStat.setString(1, ip);
 prepStat.executeUpdate();
} catch (Throwable t) {
...
int countTable(DataSource dataSource, String tableName)
count Table
Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select count(*) from " + tableName);
rs.next();
conn.close();
return rs.getInt(1);
DataSource createDataSource()
create Data Source
MysqlDataSource ds = new MysqlDataSource();
ds.setUser("root");
ds.setPassword("sP6prUCe");
ds.setServerName("localhost");
ds.setDatabaseName("db_patterns");
return ds;
void createDataTable(DataSource dataSource, String tableName)
create Data Table
try (Connection connection = dataSource.getConnection()) {
 try (Statement statement = connection.createStatement()) {
 statement.execute(String.format("create table %s (id INT, name VARCHAR(500))", tableName));
DataSource createDerbyEmbeddedDataSource(Properties props)
create Derby Embedded Data Source
String dbName = props.getProperty("db.name", "JdbcConnectorSampleDb");
String DERBY_DATA_SOURCE = "org.apache.derby.jdbc.EmbeddedDataSource";
Class<?> nsDataSource = null;
try {
 nsDataSource = Class.forName(DERBY_DATA_SOURCE);
} catch (ClassNotFoundException e) {
 String msg = "Fix the test classpath. ";
 if (System.getenv("DERBY_HOME") == null) {
...
void createTable(DataSource ds, String ddl)
create Table
Connection conn = null;
try {
 conn = ds.getConnection();
 Statement st = conn.createStatement();
 st.executeUpdate(ddl);
} finally {
 if (conn != null) {
 conn.close();
...
void createTable(DataSource ds, String tableName)
create Table
try (Connection conn = ds.getConnection()) {
 conn.createStatement().executeUpdate(String.format("CREATE TABLE %s (%s INT, %s VARCHAR(255))",
 tableName, ID_COLUMN_NAME, VALUE_COLUMN_NAME));
} catch (Exception sqle) {
 throw new RuntimeException("can't create table", sqle);


AltStyle によって変換されたページ (->オリジナル) /