我在3-4天内遇到这个问题,真的不知道该怎么做....
Employee.java
package com.tutorialspoint.test;
public class Employee {
EmployeeManager employeeManager;
private String fname;
private String lname;
private int dob;
private String add;
private int id;
public void setfname(String fname){
this.fname=fname;
}
public String getfname(){
return fname;
}
public void setlname(String lname){
this.lname=lname;
}
public String getlname(){
return lname;
}
public void setdob(int dob){
this.dob=dob;
}
public int getdob(){
return dob;
}
public void setadd(String add){
this.add=add;
}
public String getadd() {
return add;
}
public void setid(int id){
this.id=id;
}
public int getid(){
return id;
}
public void setEmployeeManager(EmployeeManager employeeManager) {
this.employeeManager = employeeManager;
}
}
EmployeeDAO.java
package com.tutorialspoint.test;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.io.Serializable;
public class EmployeeDAO implements Serializable{
private static final long serialVersionUID = 1L;
private Employee employee;
private DataSource dataSource;
//SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource){
this.dataSource=dataSource;
this.jdbcTemplate = new JdbcTemplate(this.dataSource);
}
public int saveEmployee(Employee employee) {
String fname = employee.getfname();
String lname = employee.getlname();
int dob = employee.getdob();
String add = employee.getadd();
int id = employee.getid();
Object p[] = { fname, lname, dob, add, id };
String query = "insert into employee values(?,?,?)";
return this.jdbcTemplate.update(query, p);
}
/*
* public int deleteEmployee(int id){ String sql
* ="delete from employee where id = ?"; return
* this.jdbcTemplate.update(sql, new Object[]{new Integer(id)}); }
*/
public int delete() {
return this.jdbcTemplate.update("DELETE FROM employee WHERE id = ?",
new Object[] { employee.getid() });
}
public void setEmployee(Employee employee){
this.employee=employee;
}
public Employee getEmployee() {
return employee;
}
public List<Employee> getAllEmployee(Employee employee){
return this.jdbcTemplate.query("select * from employee",new RowMapper<Employee>(){
public Employee mapRow(ResultSet rs, int rownumber) throws SQLException {
Employee employee=new Employee();
employee.setfname(rs.getString(1));
employee.setlname(rs.getString(2));
employee.setdob(rs.getInt(3));
employee.setadd(rs.getString(4));
employee.setid(rs.getInt(5));
return employee;
}
});
}
}
> applicationContext.xml
<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"
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">
<!-- Scan the com.itcuties.registration package for Spring -->
<context:component-scan base-package="com.tutorialspoint.test" />
<bean id="employee" class="com.tutorialspoint.test.Employee" >
</bean>
<bean id="em" class="com.tutorialspoint.test.EmployeeManager">
<property name="employee" ref="employee"></property>
<property name="edao" ref="edao"></property>
</bean>
<bean id="ds"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/testdb"/>
<property name="username" value="postgres"/>
<property name="password" value="postgres"/>
</bean>
<bean id="edao" class="com.tutorialspoint.test.EmployeeDAO">
<property name="employee" ref="employee"></property>
<property name="dataSource" ref="ds"></property>
</bean>
</beans>
堆 org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接;嵌套异常是org.apache.commons.dbcp.SQLNestedException:无法加载JDBC驱动程序类'org.postgresql.Driver'
javax.faces.el.EvaluationException: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'org.postgresql.Driver'
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1675)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'org.postgresql.Driver'
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:812)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:868)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:876)
at com.tutorialspoint.test.EmployeeDAO.delete(EmployeeDAO.java:53)
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 com.sun.el.parser.AstValue.invoke(AstValue.java:130)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 20 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'org.postgresql.Driver'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
... 33 more
Caused by: java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 51.0 (unable to load class org.postgresql.Driver)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2822)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1159)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:171)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1415)
... 37 more
applicationContext.xml中jdbc连接的定义是高效还是需要一些代码....
答案 0 :(得分:1)
Pratik,如果您正确查看异常跟踪,则根异常为UnsupportedClassVersionError。
Caused by: java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 51.0 (unable to load class org.postgresql.Driver)
这意味着您包含在项目中的postgres jar与运行时的JDK / JVM版本不兼容。确保包含与JDK / JVM环境版本兼容的jar。