Spring研究 (3) 使用JDBC
2004-08-31 23:55
393 查看
Spring对JDBC进行了非常优雅的封装,通过一系列的模板方法,我们只需简单的几行代码就可实现数据库的访问。
在上次的Web App的基础上,我们将通过Spring的JdbcTemplate访问数据库从而实现用户验证。
为了尽量简化,我们创建一个Access数据库,建立表Account,包含两个字段:
username:VARCHAR(20),主键;
password:VARCHAR(20)。
然后输入一些测试数据,注册到系统DSN,名字为Blog。
接下来我们在Tomcat中配置一个名为“jdbc/blog”的DataSource如下:
如果你使用其他数据库,只需要保证表的逻辑结构和JDNI名“jdbc/blog”。在AccountManager中,我们使用JdbcTemplate访问数据库来验证用户:
Account getAccount(String username, String password) throws Exception {
// validate the password:
InitialContext ctx = new InitialContext();
DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/blog");
JdbcTemplate jt = new JdbcTemplate(dataSource);
String pass = (String) jt.queryForObject(
"select password from Account where username=?",
new Object[] {username}, String.class);
if(password.equals(pass))
{
Account account = new Account();
account.setUsername(username);
account.setPassword(password);
return account;
}
throw new Exception("authorization failed.");
}
编译运行,我们可以输入hello.c?username=xxx&password=xxx来测试用户登录,如果匹配数据库中的记录,会显示出用户名,否则可以看到authorization failed异常。要更友好的显示登陆失败,可以在Controller中导向一个login_failed.jsp并给出登陆失败的原因。
下面我们来看这个AccountManager,很显然在验证逻辑中我们写入了JNDI查找DataSource的代码,这将导致比较“生硬”的编码。“依赖注入”此刻显示出了威力:我们让容器注入DataSource:
public class AccountManager implements java.io.Serializable {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
Account getAccount(String username, String password) throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
...
}
}
OK,现在这个AccountManager变得优雅多了,现在如何配置DataSource的注入呢?Spring提供了一个DataSourceUtils类,通过静态方法getDataSourceFromJndi来获得容器中配置的DataSource。我们添加以下配置:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DataSourceUtils"
factory-method="getDataSourceFromJndi">
<constructor-arg><value>jdbc/blog</value></constructor-arg>
</bean>
<bean id="accountManager" class="AccountManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
现在重新部署,效果和以前一样,但是相关代码已被“放到”配置文件中了。
:( 待续... :)
在上次的Web App的基础上,我们将通过Spring的JdbcTemplate访问数据库从而实现用户验证。
为了尽量简化,我们创建一个Access数据库,建立表Account,包含两个字段:
username:VARCHAR(20),主键;
password:VARCHAR(20)。
然后输入一些测试数据,注册到系统DSN,名字为Blog。
接下来我们在Tomcat中配置一个名为“jdbc/blog”的DataSource如下:
如果你使用其他数据库,只需要保证表的逻辑结构和JDNI名“jdbc/blog”。在AccountManager中,我们使用JdbcTemplate访问数据库来验证用户:
Account getAccount(String username, String password) throws Exception {
// validate the password:
InitialContext ctx = new InitialContext();
DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/blog");
JdbcTemplate jt = new JdbcTemplate(dataSource);
String pass = (String) jt.queryForObject(
"select password from Account where username=?",
new Object[] {username}, String.class);
if(password.equals(pass))
{
Account account = new Account();
account.setUsername(username);
account.setPassword(password);
return account;
}
throw new Exception("authorization failed.");
}
编译运行,我们可以输入hello.c?username=xxx&password=xxx来测试用户登录,如果匹配数据库中的记录,会显示出用户名,否则可以看到authorization failed异常。要更友好的显示登陆失败,可以在Controller中导向一个login_failed.jsp并给出登陆失败的原因。
下面我们来看这个AccountManager,很显然在验证逻辑中我们写入了JNDI查找DataSource的代码,这将导致比较“生硬”的编码。“依赖注入”此刻显示出了威力:我们让容器注入DataSource:
public class AccountManager implements java.io.Serializable {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
Account getAccount(String username, String password) throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
...
}
}
OK,现在这个AccountManager变得优雅多了,现在如何配置DataSource的注入呢?Spring提供了一个DataSourceUtils类,通过静态方法getDataSourceFromJndi来获得容器中配置的DataSource。我们添加以下配置:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DataSourceUtils"
factory-method="getDataSourceFromJndi">
<constructor-arg><value>jdbc/blog</value></constructor-arg>
</bean>
<bean id="accountManager" class="AccountManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
现在重新部署,效果和以前一样,但是相关代码已被“放到”配置文件中了。
:( 待续... :)
相关文章推荐
- 使用Spring 的JDBC
- drill java && spring jdbc 连接使用
- Spring JDBC-使用Spring JDBC访问数据库
- Spring+JDBC组合开发 - 使用JdbcTemplate进行insert/update/delete/query操作
- 在Red5中使用Spring-JDBC配置MySql连接池
- spring里使用JDBC(四)SimpleJdbcTemplate方式
- Spring对JDBC封装时使用的一种CallBack来参数化一个操作的模式
- 在JDBC、Spring或者Hibernate中使用proxool连接池
- 使用Spring JDBC框架连接并操作数据库
- Spring中使用JDBC
- 使用Spring的jdbcTemplate进一步简化JDBC操作
- 使用Spring JDBC持久化WebCollector爬取的数据
- [Spring3.x] 第 11 章 使用 Spring JDBC 访问数据库 & 第 12 章 整合其他 ORM 框架
- Spring 使用jdbcDaoSupport
- 使用spring jdbc查询对象
- 【Spring实战】—— 15 Spring JDBC模板使用
- spring开发_JDBC操作MySQL数据库_使用xml配置事务管理
- Spring框架学习(3)spring中使用jdbc
- Spring中使用JDBC做的增删改查
- Spring中使用JDBC