Skip to content

Instantly share code, notes, and snippets.

@jyeary
Last active February 10, 2017 20:42
Show Gist options
  • Save jyeary/6ddcae9900880a14ea2a35349dd9111f to your computer and use it in GitHub Desktop.
Save jyeary/6ddcae9900880a14ea2a35349dd9111f to your computer and use it in GitHub Desktop.
Oracle Database Connection Validation
.level=SEVERE
oracle.jdbc.level=INFO
oracle.jdbc.handlers=java.util.logging.FileHandler
java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=jdbc.log
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
/*
* Copyright 2017 John Yeary <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bluelotussoftware.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Properties;
/**
*
* @author John Yeary <[email protected]>
* @version 1.0.2
*/
public class OracleConnectionValidation {
public OracleConnectionValidation() {
}
public static void main(String[] args) throws SQLException {
if (args.length < 3) {
System.out.println("USAGE: OracleConnectionValidation jdbc:oracle:thin:@localhost:1521:orcl scott tiger\n");
System.out.println("1. You must provide the jdbc connection URL:\n");
System.out.println("\tjdbc:oracle:thin:@localhost:1521:orcl\n");
System.out.println("2. username\n");
System.out.println("3. password");
}
//JDBC URL
String url = args[0];
Properties properties = new Properties();
properties.setProperty("user", args[1]);
properties.setProperty("password", args[2]);
System.out.println(MessageFormat.format("Attempting to connect to --> {0}", url));
try (Connection connection = DriverManager.getConnection(url, properties)) {
String sql = "select sysdate as current_day from dual";
try (PreparedStatement ps = connection.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
System.out.println(MessageFormat.format("Current Date from Oracle : {0}", rs.getString("current_day")));
}
System.out.println("Connection Successful");
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bluelotussoftware</groupId>
<artifactId>oracle-connection-validator</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
<name>Oracle Connection Validator (oracle-connection-validator)</name>
<description>This application connects to the provided Oracle database with the credentials provided. Once connected,
the application performs a simple query to get the current date (sysdate), and displays the result.
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<developers>
<developer>
<id>jyeary</id>
<name>John Yeary</name>
<email>[email protected]</email>
<organization>Blue Lotus Software, LLC.</organization>
<organizationUrl>http://bluelotussoftware.com</organizationUrl>
<timezone>-5</timezone>
<url>http://javaevangelist.blogspot.com</url>
<roles>
<role>Principal</role>
</roles>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
</dependencies>
<!-- A valid Oracle Maven Repository Setup Required. See https://blogs.oracle.com/dev2dev/entry/how_to_get_oracle_jdbc -->
<repositories>
<repository>
<id>maven.oracle.com</id>
<name>oracle-maven-repo</name>
<url>https://maven.oracle.com</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven.oracle.com</id>
<name>oracle-maven-repo</name>
<url>https://maven.oracle.com</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment