gpt4 book ai didi

java - Sprig jdbc queryTimeOutException

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:00 24 4
gpt4 key购买 nike

我有一个简单的 spring jdbc 程序,但是当我运行该程序时,我得到一个 queryTimeOut 异常。

这里是完整的stackTrace

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/dao/QueryTimeoutException
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:87)
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.<init>(SQLErrorCodeSQLExceptionTranslator.java:103)
at org.springframework.jdbc.support.JdbcAccessor.getExceptionTranslator(JdbcAccessor.java:99)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:407)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:519)
at com.test.spring.EmployeeDao.saveEmployee(EmployeeDao.java:14)
at com.test.spring.Test.main(Test.java:13)
Caused by: java.lang.ClassNotFoundException: org.springframework.dao.QueryTimeoutException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 7 more

表:

create table employee(emp_name varchar2(30),address varchar2(30),salery number(10),emp_id number(10));

雇员.java:

package com.test.spring;
public class Employee {
private String emp_name;
private String address;
private int salery;
private int emp_id;
public Employee()
{

}
public Employee(String emp_name,int emp_id)
{
this.emp_name=emp_name;
this.emp_id=emp_id;
}
public Employee(String emp_name,String address,int salery,int emp_id)
{
this.emp_name=emp_name;
this.address=address;
this.salery=salery;
this.emp_id=emp_id;
}
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getSalery() {
return salery;
}
public void setSalery(int salery) {
this.salery = salery;
}
public int getEmp_id() {
return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}

}

员工道.java:

package com.test.spring;

import org.springframework.jdbc.core.JdbcTemplate;

public class EmployeeDao {
private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int saveEmployee(Employee e){
//String query="insert into employee (emp_name,address,salery,emp_id)values(?,?,?,?)";//not getting any exception
String query="insert into employee (emp_name,address,salery,emp_id)values("+e.getEmp_name()+","+e.getAddress()+","+e.getSalery()+","+e.getEmp_id()+")";//getting queryTimeOutException
return jdbcTemplate.update(query);
}

}

应用上下文.xml:

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="mysql" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>

<bean id="edao" class="com.test.spring.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

</beans>

测试.java:

package com.test.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
int status=dao.saveEmployee(new Employee("Amit","Pingla",35000,102));
System.out.println(status);

}

}

最佳答案

错误指出您的类路径中缺少一个类。这并不意味着在该特定调用期间会抛出特定异常;但它根本找不到它的类。

要在类路径中添加 QueryTimeoutException 类,您需要添加对 spring-tx 模块的依赖。

关于java - Sprig jdbc queryTimeOutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29179969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com