DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Data Source 101
// Way too many people hard code their JDBC connections to // the database server(s). Use data sources to connect to // your databases. They can be setup easily in any major // Java application server as well as JSP/servlet engines // like Tomcat and Spring. // // If you aren't running inside a container that provides // you with easy access to a data source though, you can // include the Jakarta Commons DBCP library and create a // data source as shown in the code below. The result is // a data source complete with pooling, caching, etc. that // you can pass to other code that just expects to receive // a data source.
BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(System.getProperty("driverClassName")); dataSource.setUsername(System.getProperty("username")); dataSource.setPassword(System.getProperty("password")); dataSource.setUrl(System.getProperty("url"));