Skip to content

Instantly share code, notes, and snippets.

@moxious
Created October 13, 2020 18:06
Show Gist options
  • Select an option

  • Save moxious/814c072d8449aad3a08287e3d1866335 to your computer and use it in GitHub Desktop.

Select an option

Save moxious/814c072d8449aad3a08287e3d1866335 to your computer and use it in GitHub Desktop.
Java JDBC Access to Neo4j Example
public class JDBCExample {
public static void main(String[] args) throws Exception {
Connection conn = null;
Statement stmt = null;
try {
//STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, null, null);
while(rs.next()) {
String schema = rs.getString("TABLE_SCHEM");
String name = rs.getString("TABLE_NAME");
System.out.println("\n\nThis connection has a table named " + schema + "." + name);
firstRecordFrom(conn, name);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
} finally {
//finally block used to close resources
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}// nothing we can do
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} //end finally try
} //end try
System.out.println("Goodbye!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment