Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ComboPooledDataSource cpds = new ComboPooledDataSource();
- cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
- cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
- cpds.setUser("swaldman");
- cpds.setPassword("test-password");
- // the settings below are optional -- c3p0 can work with defaults
- cpds.setMinPoolSize(5);
- cpds.setAcquireIncrement(5);
- cpds.setMaxPoolSize(20);
- // The DataSource cpds is now a fully configured and usable pooled DataSource
- DataSource ds = (DataSource) new InitialContext().lookup("jdbc/myDS");
- BasicDataSource ds = new BasicDataSource();
- ds.setDriverClassName("oracle.jdbc.driver.OracleDriver")
- ds.setUsername("scott");
- ds.setPassword("tiger");
- ds.setUrl(connectURI);
- ...
- Connection conn = ds.getConnection();
- try {
- context = new InitialContext();
- jdbcURL = (DataSource) context.lookup("jdbc/CachedDS");
- System.out.println("Obtained Cached Data Source ");
- }
- catch(NamingException e)
- {
- System.err.println("Error looking up Data Source from Factory: "+e.getMessage());
- }
- public class StringPoolTest {
- public static void main(String[] args) { // Integer.valueOf(), String.equals()
- String eol = System.getProperty("line.separator"); //java7 System.lineSeparator();
- String s1 = "Yash".intern();
- System.out.format("Val:%s Hash:%s SYS:%s "+eol, s1, s1.hashCode(), System.identityHashCode(s1));
- String s2 = "Yas"+"h".intern();
- System.out.format("Val:%s Hash:%s SYS:%s "+eol, s2, s2.hashCode(), System.identityHashCode(s2));
- String s3 = "Yas".intern()+"h".intern();
- System.out.format("Val:%s Hash:%s SYS:%s "+eol, s3, s3.hashCode(), System.identityHashCode(s3));
- String s4 = "Yas"+"h";
- System.out.format("Val:%s Hash:%s SYS:%s "+eol, s4, s4.hashCode(), System.identityHashCode(s4));
- }
- }
- public class ConnectionPool {
- static final BasicDataSource ds_dbcp2 = new BasicDataSource();
- static final ComboPooledDataSource ds_c3p0 = new ComboPooledDataSource();
- static final DataSource ds_JDBC = new DataSource();
- static Properties prop = new Properties();
- static {
- try {
- prop.load(ConnectionPool.class.getClassLoader().getResourceAsStream("connectionpool.properties"));
- ds_dbcp2.setDriverClassName( prop.getProperty("DriverClass") );
- ds_dbcp2.setUrl( prop.getProperty("URL") );
- ds_dbcp2.setUsername( prop.getProperty("UserName") );
- ds_dbcp2.setPassword( prop.getProperty("Password") );
- ds_dbcp2.setInitialSize( 5 );
- ds_c3p0.setDriverClass( prop.getProperty("DriverClass") );
- ds_c3p0.setJdbcUrl( prop.getProperty("URL") );
- ds_c3p0.setUser( prop.getProperty("UserName") );
- ds_c3p0.setPassword( prop.getProperty("Password") );
- ds_c3p0.setMinPoolSize(5);
- ds_c3p0.setAcquireIncrement(5);
- ds_c3p0.setMaxPoolSize(20);
- PoolProperties pool = new PoolProperties();
- pool.setUrl( prop.getProperty("URL") );
- pool.setDriverClassName( prop.getProperty("DriverClass") );
- pool.setUsername( prop.getProperty("UserName") );
- pool.setPassword( prop.getProperty("Password") );
- pool.setValidationQuery("SELECT 1");// SELECT 1(mysql) select 1 from dual(oracle)
- pool.setInitialSize(5);
- pool.setMaxActive(3);
- ds_JDBC.setPoolProperties( pool );
- } catch (IOException e) { e.printStackTrace();
- } catch (PropertyVetoException e) { e.printStackTrace(); }
- }
- public static Connection getDBCP2Connection() throws SQLException {
- return ds_dbcp2.getConnection();
- }
- public static Connection getc3p0Connection() throws SQLException {
- return ds_c3p0.getConnection();
- }
- public static Connection getJDBCConnection() throws SQLException {
- return ds_JDBC.getConnection();
- }
- }
- public static boolean exists(String UserName, String Password ) throws SQLException {
- boolean exist = false;
- String SQL_EXIST = "SELECT * FROM users WHERE username=? AND password=?";
- try ( Connection connection = ConnectionPool.getDBCP2Connection();
- PreparedStatement pstmt = connection.prepareStatement(SQL_EXIST); ) {
- pstmt.setString(1, UserName );
- pstmt.setString(2, Password );
- try (ResultSet resultSet = pstmt.executeQuery()) {
- exist = resultSet.next(); // Note that you should not return a ResultSet here.
- }
- }
- System.out.println("User : "+exist);
- return exist;
- }
- URL : jdbc:mysql://localhost:3306/myDBName
- DriverClass : com.mysql.jdbc.Driver
- UserName : root
- Password :
- ===== ===== context.xml ===== =====
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- The contents of this file will be loaded for a web application -->
- <Context>
- <Resource name="jdbc/MyAppDB" auth="Container"
- factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
- type="javax.sql.DataSource"
- initialSize="5" minIdle="5" maxActive="15" maxIdle="10"
- testWhileIdle="true"
- timeBetweenEvictionRunsMillis="30000"
- testOnBorrow="true"
- validationQuery="SELECT 1"
- validationInterval="30000"
- driverClassName="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/myDBName"
- username="yash" password="777"
- />
- </Context>
- ===== ===== web.xml ===== =====
- <resource-ref>
- <description>DB Connection</description>
- <res-ref-name>jdbc/MyAppDB</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
- ===== ===== DBOperations ===== =====
- servlet « init() {}
- Normal call used by sevlet « static {}
- static DataSource ds;
- static {
- try {
- Context ctx=new InitialContext();
- Context envContext = (Context)ctx.lookup("java:comp/env");
- ds = (DataSource) envContext.lookup("jdbc/MyAppDB");
- } catch (NamingException e) { e.printStackTrace(); }
- }
Add Comment
Please, Sign In to add comment