喜欢原味奶茶 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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵