Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Last active August 29, 2015 14:08
Show Gist options
  • Save danielrobertson/12a65b437696f435d28a to your computer and use it in GitHub Desktop.
Save danielrobertson/12a65b437696f435d28a to your computer and use it in GitHub Desktop.
Connect to MySQL with JDBC
// load the MySQL driver. Alternatively, use the Maven http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.33
Class.forName("com.mysql.jdbc.Driver");
// connect to database named openstack_projects on port 3306
String url = "jdbc:mysql://localhost:3306/openstack_projects";
Connection conn = DriverManager.getConnection(url, "root", "");
// make a query
String sql = "select * from projects";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet results = preparedStatement.executeQuery();
// do something with the results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment