Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DataSourceExample {
- public static DataSource getMysqlDataSource() {
- MysqlDataSource dataSource = new MysqlDataSource();
- // Set dataSource Properties
- dataSource.setServerName("localhost");
- dataSource.setPortNumber(3306);
- dataSource.setDatabaseName("example");
- dataSource.setUser("root");
- dataSource.setPassword("admin");
- return dataSource;
- }
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- // Get connection from DataSource
- conn = getMysqlDataSource().getConnection();
- stmt = conn.createStatement();
- rs = stmt.executeQuery("SELECT version()");
- if (rs.next()) {
- System.out.println("Database Version : " + rs.getString(1));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (stmt != null) {
- stmt.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement