Guest User

Untitled

a guest
Dec 13th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. ComboPooledDataSource cpds = new ComboPooledDataSource();
  2. cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
  3. cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
  4. cpds.setUser("swaldman");
  5. cpds.setPassword("test-password");
  6.  
  7. // the settings below are optional -- c3p0 can work with defaults
  8. cpds.setMinPoolSize(5);
  9. cpds.setAcquireIncrement(5);
  10. cpds.setMaxPoolSize(20);
  11.  
  12. // The DataSource cpds is now a fully configured and usable pooled DataSource
  13.  
  14. DataSource ds = (DataSource) new InitialContext().lookup("jdbc/myDS");
  15.  
  16. BasicDataSource ds = new BasicDataSource();
  17. ds.setDriverClassName("oracle.jdbc.driver.OracleDriver")
  18. ds.setUsername("scott");
  19. ds.setPassword("tiger");
  20. ds.setUrl(connectURI);
  21. ...
  22. Connection conn = ds.getConnection();
  23.  
  24. try {
  25. context = new InitialContext();
  26. jdbcURL = (DataSource) context.lookup("jdbc/CachedDS");
  27. System.out.println("Obtained Cached Data Source ");
  28. }
  29. catch(NamingException e)
  30. {
  31. System.err.println("Error looking up Data Source from Factory: "+e.getMessage());
  32. }
  33.  
  34. public class StringPoolTest {
  35. public static void main(String[] args) { // Integer.valueOf(), String.equals()
  36. String eol = System.getProperty("line.separator"); //java7 System.lineSeparator();
  37.  
  38. String s1 = "Yash".intern();
  39. System.out.format("Val:%s Hash:%s SYS:%s "+eol, s1, s1.hashCode(), System.identityHashCode(s1));
  40. String s2 = "Yas"+"h".intern();
  41. System.out.format("Val:%s Hash:%s SYS:%s "+eol, s2, s2.hashCode(), System.identityHashCode(s2));
  42. String s3 = "Yas".intern()+"h".intern();
  43. System.out.format("Val:%s Hash:%s SYS:%s "+eol, s3, s3.hashCode(), System.identityHashCode(s3));
  44. String s4 = "Yas"+"h";
  45. System.out.format("Val:%s Hash:%s SYS:%s "+eol, s4, s4.hashCode(), System.identityHashCode(s4));
  46. }
  47. }
  48.  
  49. public class ConnectionPool {
  50. static final BasicDataSource ds_dbcp2 = new BasicDataSource();
  51. static final ComboPooledDataSource ds_c3p0 = new ComboPooledDataSource();
  52. static final DataSource ds_JDBC = new DataSource();
  53.  
  54. static Properties prop = new Properties();
  55. static {
  56. try {
  57. prop.load(ConnectionPool.class.getClassLoader().getResourceAsStream("connectionpool.properties"));
  58.  
  59. ds_dbcp2.setDriverClassName( prop.getProperty("DriverClass") );
  60. ds_dbcp2.setUrl( prop.getProperty("URL") );
  61. ds_dbcp2.setUsername( prop.getProperty("UserName") );
  62. ds_dbcp2.setPassword( prop.getProperty("Password") );
  63. ds_dbcp2.setInitialSize( 5 );
  64.  
  65. ds_c3p0.setDriverClass( prop.getProperty("DriverClass") );
  66. ds_c3p0.setJdbcUrl( prop.getProperty("URL") );
  67. ds_c3p0.setUser( prop.getProperty("UserName") );
  68. ds_c3p0.setPassword( prop.getProperty("Password") );
  69. ds_c3p0.setMinPoolSize(5);
  70. ds_c3p0.setAcquireIncrement(5);
  71. ds_c3p0.setMaxPoolSize(20);
  72.  
  73. PoolProperties pool = new PoolProperties();
  74. pool.setUrl( prop.getProperty("URL") );
  75. pool.setDriverClassName( prop.getProperty("DriverClass") );
  76. pool.setUsername( prop.getProperty("UserName") );
  77. pool.setPassword( prop.getProperty("Password") );
  78. pool.setValidationQuery("SELECT 1");// SELECT 1(mysql) select 1 from dual(oracle)
  79.  
  80. pool.setInitialSize(5);
  81. pool.setMaxActive(3);
  82. ds_JDBC.setPoolProperties( pool );
  83. } catch (IOException e) { e.printStackTrace();
  84. } catch (PropertyVetoException e) { e.printStackTrace(); }
  85. }
  86.  
  87. public static Connection getDBCP2Connection() throws SQLException {
  88. return ds_dbcp2.getConnection();
  89. }
  90.  
  91. public static Connection getc3p0Connection() throws SQLException {
  92. return ds_c3p0.getConnection();
  93. }
  94.  
  95. public static Connection getJDBCConnection() throws SQLException {
  96. return ds_JDBC.getConnection();
  97. }
  98. }
  99. public static boolean exists(String UserName, String Password ) throws SQLException {
  100. boolean exist = false;
  101. String SQL_EXIST = "SELECT * FROM users WHERE username=? AND password=?";
  102. try ( Connection connection = ConnectionPool.getDBCP2Connection();
  103. PreparedStatement pstmt = connection.prepareStatement(SQL_EXIST); ) {
  104. pstmt.setString(1, UserName );
  105. pstmt.setString(2, Password );
  106.  
  107. try (ResultSet resultSet = pstmt.executeQuery()) {
  108. exist = resultSet.next(); // Note that you should not return a ResultSet here.
  109. }
  110. }
  111. System.out.println("User : "+exist);
  112. return exist;
  113. }
  114.  
  115. URL : jdbc:mysql://localhost:3306/myDBName
  116. DriverClass : com.mysql.jdbc.Driver
  117. UserName : root
  118. Password :
  119.  
  120. ===== ===== context.xml ===== =====
  121. <?xml version="1.0" encoding="UTF-8"?>
  122. <!-- The contents of this file will be loaded for a web application -->
  123. <Context>
  124. <Resource name="jdbc/MyAppDB" auth="Container"
  125. factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
  126. type="javax.sql.DataSource"
  127.  
  128. initialSize="5" minIdle="5" maxActive="15" maxIdle="10"
  129.  
  130. testWhileIdle="true"
  131. timeBetweenEvictionRunsMillis="30000"
  132.  
  133. testOnBorrow="true"
  134. validationQuery="SELECT 1"
  135. validationInterval="30000"
  136.  
  137.  
  138. driverClassName="com.mysql.jdbc.Driver"
  139. url="jdbc:mysql://localhost:3306/myDBName"
  140. username="yash" password="777"
  141. />
  142. </Context>
  143.  
  144. ===== ===== web.xml ===== =====
  145. <resource-ref>
  146. <description>DB Connection</description>
  147. <res-ref-name>jdbc/MyAppDB</res-ref-name>
  148. <res-type>javax.sql.DataSource</res-type>
  149. <res-auth>Container</res-auth>
  150. </resource-ref>
  151. ===== ===== DBOperations ===== =====
  152. servlet « init() {}
  153. Normal call used by sevlet « static {}
  154.  
  155. static DataSource ds;
  156. static {
  157. try {
  158. Context ctx=new InitialContext();
  159. Context envContext = (Context)ctx.lookup("java:comp/env");
  160. ds = (DataSource) envContext.lookup("jdbc/MyAppDB");
  161. } catch (NamingException e) { e.printStackTrace(); }
  162. }
Add Comment
Please, Sign In to add comment