Skip to content

Instantly share code, notes, and snippets.

@Makistos
Created October 28, 2013 07:34
Show Gist options
  • Save Makistos/7192719 to your computer and use it in GitHub Desktop.
Save Makistos/7192719 to your computer and use it in GitHub Desktop.
This little function will create a connection to a MySql database in the local machine. Database and user name and password are supplied as parameters. #mysql #java
private void initDb(String db, String user, String pw) {
String url = "jdbc:mysql://localhost/" + db;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, user, pw);
if (con.isClosed()) {
System.err.println("Failed to connecto to " + db);
}
}
catch (Exception e) {
System.err.println("Exception: "+ e.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment