请不要将其标记为重复,因为我无法在任何地方找到解决方案。
我是初次使用spring mvc并尝试编写一个程序,我必须将pojo类的值存储在数据库中,但是遇到了以下异常。 请帮助解决错误。
嵌套异常是org.hibernate.exception.GenericJDBCException:无法获取JDBC连接
我从其中输入我的pojo类值的Index.jsp,其值必须存储在数据库中。
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" >
<title>Insert title here</title>
</head>
<body>
<form id="myform" method="post" action="spkr">
<label>First Name</label>
<input type="text" name="fname" >
<label">Last Name</label>
<input type="text" name="lname" >
<label>DOB</label>
<input type="text" name="dob"/ >
<label >Gender</label>
<input type="radio" name="optionradio" id="optionsRadios1"
value="Male" checked="">Male
<input type="radio" name="optionradio" id="optionsRadios2"
value="Female">Female
<label >Emailid</label>
<input type="text" name="mail" placeholder="">
<label>Achivements</label>
<input type="text" name="ach" placeholder="organization">
<label> Skills</label>
<input type="text" name="skill" placeholder="">
<label>Pincode</label>
<input type="text" name="pcode"/>
<label>Organization</label>
<input type="text" name="organization"
placeholder="organization">
<input type="submit">Submit</input>
</form>
</body>
</html>
POJO CLASS,包含
变量的getter和setter Spkr.java
package com.spkr.DO;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Spkr {
@Id
private String fname;
private String lname;
private String gen;
private String mail;
private String ach;
private String skill;
private String org;
public Spkr(){
}
public Spkr( String fname, String lname, String gen, String mail, String ach, String skill, String org) {
super();
this.fname = fname;
this.lname = lname;
this.gen = gen;
this.mail = mail;
this.ach = ach;
this.skill = skill;
this.org = org;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getGen() {
return gen;
}
public void setGen(String gen) {
this.gen = gen;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getAch() {
return ach;
}
public void setAch(String ach) {
this.ach = ach;
}
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
public String getOrg() {
return org;
}
public void setOrg(String org) {
this.org = org;
}
}
SpeakerAddService.java(服务接口)
package com.spkr.service;
import com.spkr.DO.Spkr;
public interface SpeakerAddService {
public void add(Spkr s);
}
SpeakerAddServiceImpl.java
package com.spkr.service;
import org.springframework.beans.factory.annotation.Autowired;
import com.spkr.DO.Spkr;
import com.spkr.dao.SpeakerDao;
public class SpeakerAddServiceImpl implements SpeakerAddService{
@Autowired
SpeakerDao sas;
public SpeakerAddServiceImpl() {
super();
// TODO Auto-generated constructor stub
}
public void add(Spkr s) {
//System.out.println(fname +" "+skill);
System.out.println("Hi service");
System.out.println(s.getMail()+" "+s.getLname()+" "+s.getGen()+"
"+s.getAch());
System.out.println(s.getFname()+" "+s.getSkill());
sas.addSpeaker(s);
}}
SpeakerDao.java(接口)
package com.spkr.dao;
import com.spkr.DO.Spkr;
public interface SpeakerDao {
public void addSpeaker(Spkr s);
}
SpeakerDaoImpl,JAVA
package com.spkr.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import com.spkr.DO.Spkr;
public class SpeakerDaoImpl implements SpeakerDao{
@Autowired()
public SpeakerDaoImpl(SessionFactory sessionFactory) {
this.sessionfactory = sessionFactory;
}
private SessionFactory sessionfactory;
public void addSpeaker(Spkr s) {
// TODO Auto-generated method stub
System.out.println("Hi database");
System.out.println(s.getAch()+" "+s.getFname()+" "+s.getGen()+"
"+s.getLname()+" "+s.getMail()+" "+s.getOrg()+" "+s.getSkill());
/*Session session = sessionfactory.getSessionFactory().openSession();
session.save(s);*/
@SuppressWarnings("deprecation")
Session session = sessionfactory.getSessionFactory().openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
System.out.println("one record friend--");
}
}
SPKR-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<mvc:annotation-driven />
<context:annotation-config/>
<context:component-scan base-package="com.spkr.controller">
</context:component-scan>
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsp/"></beans:property>
<beans:property name="suffix" value=".jsp"></beans:property>
</beans:bean>
<beans:bean id="speakerAddService"
class="com.spkr.service.SpeakerAddServiceImpl"/>
<beans:bean id="s" class="com.spkr.dao.SpeakerDaoImpl"/>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
<beans:prop key="hibernate.current_session_context_class">thread</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.spkr.DO.Spkr</beans:value> <!-- Entity classes-->
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/student" />
<beans:property name="username" value="root" />
<beans:property name="password" value="root" />
</beans:bean>
</beans:beans>
的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spkr</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spkr-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spkr</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
错误说明
nested exception is org.hibernate.exception.GenericJDBCException: Unable to
acquire JDBC Connection
Root cause
org.apache.commons.dbcp.SQLNestedException: Cannot create
PoolableConnectionFactory (initializeConnection: connection closed)
Root cause
java.sql.SQLException: initializeConnection: connection closed