Skip to content

Instantly share code, notes, and snippets.

@sapanparikh18
Last active March 20, 2018 11:14
Show Gist options
  • Save sapanparikh18/e31063e12314658c90671bf5732837e7 to your computer and use it in GitHub Desktop.
Save sapanparikh18/e31063e12314658c90671bf5732837e7 to your computer and use it in GitHub Desktop.
public static void runQuery(){ // This method does 3 things 1. Gets the db connection and 2. Runs the query 3. Closes the connection
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/db","user","password");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next()) {
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
}catch(Exception e){ //handle exception}
finally{
if(connection!=null)
con.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment