喜欢原味奶茶 2016-07-25 11:38 采纳率: 33.3%
浏览 1169

Spring访问数据库问题

Dao的内容
public class StudentDao extends JdbcDaoSupport{

public void insertStudent(Student student) {
    String sql = "insert into test(name) values(?)";
    this.getJdbcTemplate().update(sql,student.getName());
}

}

业务代码
public class StudentImp implements IStudentImp{
private StudentDao dao;

public void setDao(StudentDao dao) {
    this.dao = dao;
}
public void add(Student student){
    dao.insertStudent(student);
}

}

配置文件:

<bean id="myDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/studentdao"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>
    <!--注册Dao  -->
<bean id="mydao" class="com.spring.dao.StudentDao">
    <property name="dataSource" ref="myDatasource"></property>
</bean>
<!-- 注册service -->
<bean id="myservice" class="com.spring.service.StudentImp">
    <property name="dao" ref="mydao"/>
</bean>

测试代码:
    public void testadd(){
    ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    IStudentImp studentimp = (IStudentImp)ac.getBean("myservice");
    Student student = new Student("jacke");
    studentimp.add(student);
}

报错:

图片说明
java.lang.NoClassDefFoundError: Could not initialize class org.springframework.jdbc.core.StatementCreatorUtils
at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.cleanupParameters(ArgumentPreparedStatementSetter.java:72)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:924)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:909)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:909)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:970)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:980)
at com.spring.dao.StudentDao.insertStudent(StudentDao.java:12)
at com.spring.service.StudentImp.add(StudentImp.java:13)
at com.spring.test.Test.testadd(Test.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

到底哪里出问题了啊!

在跟断点的时候,里面有一个异常:
EXCEPTION: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
但到底是如何能解决实在没法,求大神帮忙
  • 写回答

1条回答 默认 最新

  • GrayHJX 2016-07-26 03:31
    关注

    程序和数据库断掉了。。。你重启一下tomcat和mysql看看

    评论

报告相同问题?

悬赏问题

  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到
  • ¥15 为什么视频算法现在全是动作识别?
  • ¥15 编写一段matlab代码
  • ¥15 用Python做岩石类别鉴定软件
  • ¥15 关于调取、提交更新数据库记录的问题
  • ¥15 之前删了盘从下vs2022遇见这个问题 搞了一整天了
  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)
  • ¥30 notepad++ 自定义代码补全提示