Note that there are some explanatory texts on larger screens.

plurals
  1. POdatasource vs simple connection database
    text
    copied!<p>What would be the best approach?, I need to connect many remote databases to resolve many requests from a Restful web service. I was thinking two solutions:</p> <ol> <li><p>One datasource per remote database, and the connection to each datasource will be like a singleton pattern.</p></li> <li><p>One simple connection per remote database, well just a singleton pattern to connect with each database.</p></li> </ol> <p>One example of the first approach is like this (vía msdn):</p> <pre><code>import java.sql.*; import com.microsoft.sqlserver.jdbc.*; public class connectDS { public static void main(String[] args) { // Declare the JDBC objects. Connection con = null; CallableStatement cstmt = null; ResultSet rs = null; try { // Establish the connection. SQLServerDataSource ds = new SQLServerDataSource(); ds.setUser("UserName"); ds.setPassword("*****"); ds.setServerName("localhost"); ds.setPortNumber(1433); ds.setDatabaseName("AdventureWorks"); con = ds.getConnection(); // Execute a stored procedure that returns some data. cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}"); cstmt.setInt(1, 50); rs = cstmt.executeQuery(); // Iterate through the data in the result set and display it. while (rs.next()) { System.out.println("EMPLOYEE: " + rs.getString("LastName") + ", " + rs.getString("FirstName")); System.out.println("MANAGER: " + rs.getString("ManagerLastName") + ", " + rs.getString("ManagerFirstName")); System.out.println(); } } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) try { rs.close(); } catch(Exception e) {} if (cstmt != null) try { cstmt.close(); } catch(Exception e) {} if (con != null) try { con.close(); } catch(Exception e) {} System.exit(1); } } } </code></pre> <p>For the second approach the singleton example could be:</p> <pre><code>public java.sql.Connection conn; private static Statement statement; public static MysqlConnect db; private MysqlConnect() { String url= "jdbc:mysql://localhost:3306/"; String dbName = "Banco"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "123456"; try { Class.forName(driver).newInstance(); this.conn = (java.sql.Connection)DriverManager.getConnection(url+dbName,userName,password); System.out.println("Connected to DataBase: " + dbName); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException sqle) { System.out.println("Error Inesperado en MysqlConnect" + sqle.toString()); } } /** *Method for connect to a database * @return MysqlConnect Database connection object */ public static synchronized MysqlConnect getDbCon() { if ( db == null ) { try { db = new MysqlConnect(); statement = db.conn.createStatement(); } catch (SQLException ex) { Logger.getLogger(MysqlConnect.class.getName()).log(Level.SEVERE, null, ex); } } System.out.println("Connection to DB: OK"); return db; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload