qq948216248
qq948216248
采纳率33.3%
2015-11-07 12:24 阅读 2.2k

spring jdbc 连接错误,急求大神解答

错误信息:java.lang.NoClassDefFoundError: Could not initialize class org.springframework.jdbc.core.StatementCreatorUtils

代码:
package com.xiaoming.spring.jdbc.dao;

import javax.annotation.Resource;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Repository;

import com.xiaoming.spring.jdbc.model.User;

@Repository("userDao")

public class UserDao implements IUserDao{

private JdbcTemplate jdbcTemplate;                                                                  

@Resource(name="dataSource")                                                                        
public void setJdbcTemplate(DataSource dataSource) {                                                
    this.jdbcTemplate = new JdbcTemplate(dataSource);                                               
}                                                                                                   



public JdbcTemplate getJdbcTemplate() {                                                             
    return jdbcTemplate;                                                                            
}                                                                                                   


@Override                                                                                           
public void insert(User user) {                                                                     
    jdbcTemplate.update("insert into tab_user(username,password) values (?,?)",                     
            user.getUsername(),user.getPassword());                                                 
    System.out.println("插入"+user);                                                                  
}                                                                                                   

@Override                                                                                           
public void query(int id) {                                                                         
    // TODO Auto-generated method stub                                                              

}                                                                                                   

@Override                                                                                           
public void del(int id) {                                                                           
    // TODO Auto-generated method stub                                                              

}                                                                                                   

@Override                                                                                           
public void update(User user) {                                                                     
    // TODO Auto-generated method stub                                                              

}                                                                                                   

}

beans:
<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:annotation-config> </context:annotation-config>                                         
<context:component-scan base-package="com.xiaoming.spring.jdbc"></context:component-scan>        

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"                           
    destroy-method="close">                                                                      
    <property name="driverClassName" value="${jdbc.driverClassName}"></property>                 
    <property name="url" value="${jdbc.url}"></property>                                         
    <property name="username" value="${jdbc.username}"></property>                               
    <property name="password" value="${jdbc.password}"></property>                               
    </bean>                                                                                      
<context:property-placeholder location="jdbc.properties"/>                                       

配置文件:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/spring_teach
jdbc.username = root
jdbc.password = 2013

补充说明:我用的的MyEclipse for spring 2014,不过有很多jar文件都要自己导入的,mysql 的驱动包我已经导入

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • Royal_lr Royal_lr 2015-11-08 03:33

    @Resource(name="dataSource")

    public void setJdbcTemplate(DataSource dataSource) {

    this.jdbcTemplate = new JdbcTemplate(dataSource);

    }

    这个应该是你的注入出了问题,,你这段代码,,注入的是你的JdbcTemplate,,而不是参数,,就相当于
    JdbcTemplate jdbcTemplate=new DataSource()..所以会抛异常

    点赞 评论 复制链接分享
  • wojiushiwo945you 毕小宝 2015-11-08 06:06

    可能是spring的jar包不对,检查下spring的版本。看下这个类.StatementCreatorUtils所在的jar是什么,导入它试试。

    点赞 评论 复制链接分享
  • qq_19558705 ITDragon龙 2015-12-04 09:36
    点赞 评论 复制链接分享
  • u011559214 洛轩丶 2015-12-07 12:45

    看是不是包到重复了

    点赞 评论 复制链接分享
  • qq_35572165 qq_35572165 2017-09-10 04:54

    找到问题了吗????????????

    点赞 评论 复制链接分享

相关推荐