Skip to content

Instantly share code, notes, and snippets.

@UrkoLekuona
Last active August 4, 2020 08:26
Show Gist options
  • Save UrkoLekuona/4be14e099aeec2e7d8ef616886b66fdf to your computer and use it in GitHub Desktop.
Save UrkoLekuona/4be14e099aeec2e7d8ef616886b66fdf to your computer and use it in GitHub Desktop.
Test connection to a Database using Java (Simple main class)
package com.example.jdbc_postgres;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* Test connection to a Database using Java
*
* @author Fernando Lozano
*
*/
public class TestConnection {
/**
* JDBC URL
*
* Examples:
*
* Oracle Thin: jdbc:oracle:thin:@//localhost:1521/XE DB2:
* jdbc:db2://127.0.0.1:50000/SAMPLE PostgreSQL:
* jdbc:postgresql://127.0.0.1:5432/test HSQLDB:
* jdbc:hsqldb:hsql://127.0.0.1:1476 HSQLDB Embedded: jdbc:hsqldb:file:filepath
* MySQL: jdbc:mysql://127.0.0.1:3306/test
*
*/
private final static String DB_URL = "jdbc:postgresql://localhost/example";
private final static String USER = "admin";
private final static String PASS = "admin";
private final static String CREATE_TABLE = "CREATE TABLE EXAMPLE_TABLE (id INTEGER not NULL, text_field VARCHAR(255), PRIMARY KEY ( id ))";
private final static String INSERT = "INSERT INTO EXAMPLE_TABLE VALUES (1, 'hello')";
private final static String SELECT = "SELECT * FROM EXAMPLE_TABLE";
private final static String DROP = "DROP TABLE EXAMPLE_TABLE";
/**
* Driver class
*
* Examples:
*
* Oracle Thin: oracle.jdbc.driver.OracleDriver DB2: com.ibm.db2.jcc.DB2Driver
* PostgreSQL: org.postgresql.Driver HSQLDB: org.hsqldb.jdbcDriver HSQLDB
* Embedded: org.hsqldb.jdbcDriver MySQL: com.mysql.jdbc.Driver
*
*/
private final static String DRIVER = "org.postgresql.Driver";
private final static boolean autosave = false;
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DRIVER);
System.out.println("Connecting to database…");
Properties props = new Properties();
props.setProperty("user", USER);
props.setProperty("password", PASS);
if (autosave) {
props.setProperty("autosave", "ALWAYS");
}
conn = DriverManager.getConnection(DB_URL, props);
conn.setAutoCommit(false);
System.out.println("Connected.");
stmt = conn.createStatement();
try {
stmt.executeUpdate(CREATE_TABLE);
System.out.println("Created table.");
} catch (SQLException e) {
if (e.toString().contains("already exists")) {
System.out.println("Table already exists, omiting...");
} else {
e.printStackTrace();
throw e;
}
}
try {
stmt.executeUpdate(INSERT);
System.out.println("Inserted row.");
try {
stmt.executeUpdate(INSERT);
} catch (SQLException e) {
System.err.println("Error inserting.");
}
} catch (SQLException e) {
if (e.toString().contains("duplicate key value violates unique constraint")) {
System.err.println("Failed inserting row.");
} else {
e.printStackTrace();
throw e;
}
}
ResultSet rs = stmt.executeQuery(SELECT);
while (rs.next()) {
System.out.println("ID: " + rs.getString("id") + ", TEXT_FIELD: " + rs.getString("text_field"));
}
stmt.executeUpdate(DROP);
System.out.println("Dropped table.");
} catch (SQLException se) {
System.err.println("SQLException:");
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
conn.close();
} catch (SQLException se) {
} // do nothing
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment