Advertisement
Guest User

Untitled

a guest
May 31st, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  4.  
  5. public class DBConnection {
  6.    
  7.     // Info connessioni DB
  8.     private static final String dbName = "MotoDB";
  9.     private static final String url = "jdbc:mysql://motodb.c1s5lipxxzvy.eu-west-1.rds.amazonaws.com:3306";
  10.     private static final String username = "Administrator";
  11.     private static final String password = "cocomero";
  12.     private Connection connection;
  13.     private MysqlDataSource dataSource;
  14.    
  15.     public Connection getConnection()  {
  16.  
  17.         dataSource = new MysqlDataSource();
  18.         dataSource.setUser(username);
  19.         dataSource.setPassword(password);
  20.         dataSource.setServerName(url);
  21.         dataSource.setDatabaseName(dbName);
  22.  
  23.         ResultSet rs;
  24.         try {
  25.             connection = dataSource.getConnection();
  26.             /*Statement stmt = conn.createStatement();
  27.             rs = stmt.executeQuery("SELECT * FROM CAMPIONATO");
  28.             while (rs.next()) {
  29.                 System.out.println(rs.getString("anno"));
  30.             }
  31.             rs.close();
  32.             stmt.close();
  33.             */
  34.             connection.close();
  35.         } catch (SQLException e) {
  36.             e.printStackTrace();
  37.         }
  38.         return connection;
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement