Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class JavaToMySQL {
  4.  
  5. private static final String url;
  6.  
  7. static {
  8. url = "jdbc:mysql://localhost:3306/world?autoReconnect=true&useSSL=false";
  9. }
  10.  
  11. private static final String user = "root";
  12. private static final String password = "root";
  13.  
  14. private static Connection con;
  15. private static Statement stmt;
  16. private static ResultSet rs;
  17.  
  18. public static void main(String args[]) {
  19. String query = "select count(*) from city";
  20.  
  21. try {
  22.  
  23. con = DriverManager.getConnection(url, user, password);
  24.  
  25.  
  26. stmt = con.createStatement();
  27.  
  28. rs = stmt.executeQuery(query);
  29.  
  30. while (rs.next()) {
  31. int count = rs.getInt(1);
  32. System.out.println("Total number of books in the table : " + count);
  33. }
  34.  
  35. } catch (SQLException sqlEx) {
  36. sqlEx.printStackTrace();
  37. } finally {
  38. try { con.close(); } catch(SQLException se) { }
  39. try { stmt.close(); } catch(SQLException se) { }
  40. try { rs.close(); } catch(SQLException se) { }
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement