Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. public class DataSourceExample {
  2.  
  3. public static DataSource getMysqlDataSource() {
  4. MysqlDataSource dataSource = new MysqlDataSource();
  5.  
  6. // Set dataSource Properties
  7. dataSource.setServerName("localhost");
  8. dataSource.setPortNumber(3306);
  9. dataSource.setDatabaseName("example");
  10. dataSource.setUser("root");
  11. dataSource.setPassword("admin");
  12. return dataSource;
  13. }
  14.  
  15. public static void main(String[] args) {
  16.  
  17. Connection conn = null;
  18. Statement stmt = null;
  19. ResultSet rs = null;
  20. try {
  21.  
  22. // Get connection from DataSource
  23. conn = getMysqlDataSource().getConnection();
  24. stmt = conn.createStatement();
  25. rs = stmt.executeQuery("SELECT version()");
  26.  
  27. if (rs.next()) {
  28. System.out.println("Database Version : " + rs.getString(1));
  29. }
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. } finally {
  33. try {
  34. if (rs != null) {
  35. rs.close();
  36. }
  37. if (stmt != null) {
  38. stmt.close();
  39. }
  40. if (conn != null) {
  41. conn.close();
  42. }
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement