急急急!!!求助各位大神,小弟不胜感激!

天涯路zy 2018-06-06 03:41:33
写连接池实现类时,报错如下:

连接池接口及代码如下:

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Repository;

import entity.Admin;
/**
* 持久层实现
* @author zy
*
*/
@Repository
public class AdminDAOJdbcImpl implements AdminDAO {
//注入连接池
@Resource(name="ds")
private DataSource ds;
public Admin findByAdminCode(String adminCode){
Admin admin = null;
Connection conn = null;
try {
System.out.println(ds);

System.out.println("未建立连接");

ds.getConnection();

System.out.println("建立连接");

String sql = "select * from admin_info_zy where admin_code = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, adminCode);
ResultSet rs = ps.executeQuery();
if(rs.next()){
admin = new Admin();
admin.setAdminId(rs.getInt("admin_id"));
admin.setAdminCode(rs.getString("admin_code"));
admin.setPassword(rs.getString("password"));
admin.setName(rs.getString("name"));
admin.setTelephone(rs.getString("telephone"));
admin.setEmail(rs.getString("email"));
admin.setEnrolldate(rs.getTimestamp("enrolldate"));
}
} catch (SQLException e) {
//记日志(保留现场)
e.printStackTrace();
/*
* 看异常能否恢复,如果能够恢复,则立即恢复,
* 如果不能够恢复,如系统发生了异常,比如数
* 据库停止服务,则提示用户稍后再试;
*/
throw new RuntimeException(e);
}finally{
try {
conn.close();
} catch (SQLException e) {
//把异常打印到控制台
e.printStackTrace();
throw new RuntimeException(e);
}
}
return admin;
}
public static void main(String[] args) {
AdminDAO dao = new AdminDAOJdbcImpl();
Admin admin = dao.findByAdminCode("caocao");
System.out.println(admin);
}
}

AdminDAO接口代码如下:

package dao;

import entity.Admin;
/**
* 持久层接口
*
* @author zy
*
*/

public interface AdminDAO {
public Admin findByAdminCode(String adminCode);
}

springmvc的配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<!-- 配置组件扫描 -->
<context:component-scan base-package="dao"/>
<!-- 配置mvc注解扫描 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

<util:properties id="config" location="classpath:db.properties"/>
<!--
配置连接池
-->
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="#{config.driver}"/>
<property name="url" value="#{config.url}"/>
<property name="username" value="#{config.username}"/>
<property name="password" value="#{config.password}"/>
</bean>
</beans>

求哪位大神帮忙看下,小弟不胜感激!!!
...全文
587 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧