Last active
March 20, 2018 11:14
-
-
Save sapanparikh18/e31063e12314658c90671bf5732837e7 to your computer and use it in GitHub Desktop.
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
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