Skip to content

Instantly share code, notes, and snippets.

@Alcar32
Created May 15, 2013 23:17
Show Gist options
  • Save Alcar32/5588192 to your computer and use it in GitHub Desktop.
Save Alcar32/5588192 to your computer and use it in GitHub Desktop.
Open a database connection
import java.sql.*;
Connection connection = null;
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String connectionURL = "jdbc:derby:"
connectionURL += my_db_path;
try {
// load driver
Class.forName(driver);
} catch (ClassNotFoundException ex) {
// driver not found!
System.err.println(ex);
ex.printStackTrace();
System.exit(1); // quit
}
try {
// try to access to db
connection = DriverManager.getConnection(connectinURL);
} catch (SQLException ex) {
// access failed!
System.err.println(ex);
ex.printStackTrace();
System.exit(1); // quit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment