Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. package com.zetcode.version;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. public class Version {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. Connection con = null;
  16. Statement st = null;
  17. ResultSet rs = null;
  18.  
  19. String url = "jdbc:mysql://localhost:3306/testdb";
  20. String user = "testuser";
  21. String password = "test623";
  22.  
  23. try {
  24.  
  25. con = DriverManager.getConnection(url, user, password);
  26. st = con.createStatement();
  27. rs = st.executeQuery("SELECT VERSION()");
  28.  
  29. if (rs.next()) {
  30.  
  31. System.out.println(rs.getString(1));
  32. }
  33.  
  34. } catch (SQLException ex) {
  35.  
  36. Logger lgr = Logger.getLogger(Version.class.getName());
  37. lgr.log(Level.SEVERE, ex.getMessage(), ex);
  38.  
  39. } finally {
  40.  
  41. try {
  42.  
  43. if (rs != null) {
  44. rs.close();
  45. }
  46.  
  47. if (st != null) {
  48. st.close();
  49. }
  50.  
  51. if (con != null) {
  52. con.close();
  53. }
  54.  
  55. } catch (SQLException ex) {
  56.  
  57. Logger lgr = Logger.getLogger(Version.class.getName());
  58. lgr.log(Level.WARNING, ex.getMessage(), ex);
  59. }
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement