Guest User

Untitled

a guest
Mar 28th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. public static void runQuery(){ // This method does 3 things 1. Gets the db connection and 2. Runs the query 3. Closes the connection
  2. Connection con = null;
  3. try{
  4. Class.forName("com.mysql.jdbc.Driver");
  5. con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/db","user","password");
  6. Statement stmt=con.createStatement();
  7. ResultSet rs=stmt.executeQuery("select * from emp");
  8. while(rs.next()) {
  9. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
  10. }
  11.  
  12. }catch(Exception e){ //handle exception}
  13. finally{
  14. if(connection!=null)
  15. con.close();
  16. }
  17. }
Add Comment
Please, Sign In to add comment