以前写程序时,都是用java自带的DriverManager类进行驱动,学习了DataSource后,才知道它的不足,但是,如果没接触过java.sql包提供的DriverManager,是体会不到DataSource的优点的,DataSource具有以下优点:首先,驱动程序类可以不硬编码在应用程序中,其次,当数据或驱动程序发生更改时,不必更改应用程序,也就是可以对数据源的属性进行修改,第三,可获得连接池和分布式事务,这是用DriverManager建立的连接所做不到的!总结一下用不同DataSource连接数据库的方法,尽管方法很多,但是基本思想和代码的写法都是大同小异,你可以根据需求选择最适合的,而不是挑最时髦的!
1commons.dbcp.BasicDataSource:
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DsDemo {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO 自动生成方法存根
BasicConfigurator.configure();
Log log = LogFactory.getLog(DsDemo.class);
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ds.setUrl("jdbc:sqlserver://localhost;databaseName=UserL");
ds.setUsername("chen");
ds.setPassword("chen");
try {
conn = ds.getConnection();
} catch (SQLException e1) {
// TODO 自动生成 catch 块
log.error("无法建立连接");
}
try {
pstmt = conn.prepareStatement("select * from users");
} catch (SQLException e1) {
// TODO 自动生成 catch 块
log.error("执行语句时出错");
}
try {
rs = pstmt.executeQuery();
} catch (SQLException e1) {
// TODO 自动生成 catch 块
log.error("产生记录集时出错!");
}
try {
while(rs.next())
{
log.info(rs.getString("name"));
}
} catch (SQLException e1) {
// TODO 自动生成 catch 块
log.error("记录集无法显示");
}
finally
{
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("无法关闭记录集");
}
rs = null;
}
if(pstmt != null)
{
try {
pstmt.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("无法关闭Statement");
}
pstmt = null;
}
if(conn != null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("无法关闭连接");
}
conn = null;
}
}
}
}
这是个完整的程序,只要你有相应的jar包,这个小程序跑通是没什么问题的,当然,这只是初学者的练习而已!
2org.springframework.jdbc.datasource.DriverManagerDataSource
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.BasicConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class SpringDmds {
/**
* @param args
*/
DriverManagerDataSource dataSource = null;
Connection conn = null;
PreparedStatement psmtm = null;
ResultSet rs = null;
public void setDataSource(DriverManagerDataSource dataSource)
{
this.dataSource = dataSource;
}
public void getAllInfo()
{
BasicConfigurator.configure();
Log log = LogFactory.getLog(SpringDmds.class);
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("得到数据源时出错!");
}
try {
psmtm = conn.prepareStatement("select * from users");
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("sql语句出错");
}
try {
rs = psmtm.executeQuery();
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("得到记录集时出错");
}
try {
while(rs.next())
{
log.info(rs.getString("name"));
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("读取数据库时出错");
}
finally
{
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("记录集无法关闭");
}
rs=null;
}
if(psmtm!=null)
{
try {
psmtm.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("statment无法关闭");
}
psmtm=null;
}
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
log.error("无法关闭连接");
}
conn=null;
}
}
}
public static void main(String[] args)
{
// TODO 自动生成方法存根
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringDmds sd = (SpringDmds)context.getBean("springprop");
sd.getAllInfo();
}
}
相信高手一看就能就明白,这是Spring提供的一个只供测试用的DataSource类,只是自己做练习时用一下,在项目中千万不能把它搬出来!当然,这个程序还需要在Spring中装配,代码很简单的!
今天只是介绍了一些简单的,由第三方提供的用来得到DataSource的方法,除了这些,还有一些更有实力的,用来得到DataSource的框架,比如ibatis, hibernate-3.1等都为用户提供了很多数据源,所以我们作为开发人员是幸运的!
关于ibatis和 hibernate-3.1的用法下次介绍!