sql2005数据库
<bean id="Source" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost:1433;DatabaseName=zyc_book</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value></property>
</bean>
<bean id="TestDAO" class="zyc.TestDAO">
<property name="dataSource">
<ref local="Source"/>
</property>
</bean><bean id="testAction" class="zyc.TestAction">
<property name="commandClass">
<value>zyc.TestForm</value>
</property>
<property name="validator">
<ref local="testValidator" />
</property>
<property name="testDao">
<ref local="TestDAO"/>
</property>
<property name="formView">
<value>register</value>
</property>
<property name="successView">
<value>success</value>
</property>
</bean>package zyc;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;public class TestAction extends SimpleFormController {
private TestDAO testDao;
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{
TestForm testForm=(TestForm)command;
testDao=this.getTestDao();
if(testDao==null)
System.out.print("\n\n\n\n\ntest null\n\n\n"); //这里注入成功
if(testDao.isExist(testForm.getUsername()))
return new ModelAndView(this.getSuccessView());
else
return new ModelAndView(this.getFormView());
}
public TestDAO getTestDao() {
return testDao;
}
public void setTestDao(TestDAO testDao) {
this.testDao = testDao;
}
}
package zyc;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;public class TestDAO {
private DataSource dataSource;public DataSource getDataSource() {
return dataSource;
}public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean isExist(String username){
if(this.getDataSource()==null)
{ System.out.print("\n\n\nnull\n\n\n");//这里得到空,注入失败
return true;
}
JdbcTemplate jdbcTemplate=new JdbcTemplate(getDataSource());
int count=jdbcTemplate.queryForInt("select count(*) from book_user where user_name='"+username+"'");
if(count>0)
return false;
else
return true;
}
}testDAO 注入到testAction能注入成功
dataSource 注入到testDAO不成功。为啥?
<bean id="Source" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName">
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</property>
<property name="url">
<value>jdbc:sqlserver://localhost:1433;DatabaseName=zyc_book</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value></property>
</bean>
<bean id="TestDAO" class="zyc.TestDAO">
<property name="dataSource">
<ref local="Source"/>
</property>
</bean><bean id="testAction" class="zyc.TestAction">
<property name="commandClass">
<value>zyc.TestForm</value>
</property>
<property name="validator">
<ref local="testValidator" />
</property>
<property name="testDao">
<ref local="TestDAO"/>
</property>
<property name="formView">
<value>register</value>
</property>
<property name="successView">
<value>success</value>
</property>
</bean>package zyc;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;public class TestAction extends SimpleFormController {
private TestDAO testDao;
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{
TestForm testForm=(TestForm)command;
testDao=this.getTestDao();
if(testDao==null)
System.out.print("\n\n\n\n\ntest null\n\n\n"); //这里注入成功
if(testDao.isExist(testForm.getUsername()))
return new ModelAndView(this.getSuccessView());
else
return new ModelAndView(this.getFormView());
}
public TestDAO getTestDao() {
return testDao;
}
public void setTestDao(TestDAO testDao) {
this.testDao = testDao;
}
}
package zyc;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;public class TestDAO {
private DataSource dataSource;public DataSource getDataSource() {
return dataSource;
}public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean isExist(String username){
if(this.getDataSource()==null)
{ System.out.print("\n\n\nnull\n\n\n");//这里得到空,注入失败
return true;
}
JdbcTemplate jdbcTemplate=new JdbcTemplate(getDataSource());
int count=jdbcTemplate.queryForInt("select count(*) from book_user where user_name='"+username+"'");
if(count>0)
return false;
else
return true;
}
}testDAO 注入到testAction能注入成功
dataSource 注入到testDAO不成功。为啥?
解决方案 »
- 求JSP播放器
- ssh框架中事务管理的请教
- 给大家共享一本chm的 Struts2.0.14 api
- spring采用Annotation的方式对AOP的支持的问题
- Ibatis 中的批处理和事物
- 关于web开发数据维护的问题
- 问个关于SpringMVC+SpringJDBC+Velocity保存实体的问题,谢谢。
- JComboBox如何读取数据库里的数据并填充到JComboBox里.????
- java中出现outofmemory的错误,这要怎么解决?
- 关于BMP与CMP的选择问题
- Hibernate 使用 proxool 连接池 隔一段时间自动断开连接,郁闷死了,请求大家帮忙
- 算法题:直线上的3个点,求第二点的坐标。
<property name="dataSource">
<ref local="Source"/>
</property>
</bean> <bean id="Source" class="org.apache.commons.dbcp.BasicDataSource" >
异常说 dataSource is required