Last active
August 29, 2015 14:08
-
-
Save danielrobertson/12a65b437696f435d28a to your computer and use it in GitHub Desktop.
Connect to MySQL with JDBC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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