Last active
March 22, 2018 05:02
-
-
Save sapanparikh18/bade880716824de8c63a40dfdb304271 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(Connection connection){ | |
try{ | |
Statement stmt=con.createStatement(); | |
ResultSet rs=stmt.executeQuery("select * from table"); | |
while(rs.next()) { | |
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); | |
} | |
}catch(Exception e){ | |
//Handle exception | |
} | |
} | |
public static Connection getDBConnection(){ | |
Connection con = null; | |
try{ | |
Class.forName("com.mysql.jdbc.Driver"); | |
con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/db","user","password"); | |
} | |
catch(Exception e){ | |
//Handle exception | |
} | |
return con; | |
} | |
public static void closeConnection(Connection connection){ | |
if(connection!=null && !connection.isClosed()){ | |
connection.close(); | |
} | |
} | |
public static void main(String[]args){ | |
Connection con = getDBConnection(); | |
runQuery(con); | |
closeConnection(con); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment