Oct 18, 2018 2:01:41 AM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'data' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'DriverClass' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'DriverClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'data' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'DriverClass' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'DriverClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1684)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:848)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:865)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:548)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.jdbcSpring.dao.testJdbc.main(testJdbc.java:11)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'DriverClass' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'DriverClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:243)
at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:426)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:278)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:266)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:97)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1680)
... 13 more
the person class
package com.jdbcSpring.dao;
public class person {
private int id;
private String fname;
private String lname;
private int age;
private String address;
private int phone;
public person(String string, String string2, int i, String string3, int j) {
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
calss DataBaseInfo
package com.jdbcSpring.dao;
import org.springframework.jdbc.core.JdbcTemplate;
public class DataBaseInfo {
private JdbcTemplate template;
public int insertPerson(person p) {
String inserData="insert into person values("+p.getFname()+","+p.getLname()+","+p.getAge()+","+p.getAddress()+","+p.getPhone()+")";
return template.update(inserData);
}
public int updatePerson(person p) {
String updateData="update person('fname','lname','age','address','phone') set values("+p.getFname()+","+p.getLname()+","+p.getAge()+","+p.getAddress()+","+p.getPhone()+") ";
return template.update(updateData);
}
public int deletePerson(person p ) {
String deleteData="delet from person where ('id= "+p.getId()+"')";
return template.update(deleteData);
}
public int selectAllPerson() {
String selectAllData="select * from person ";
return template.update(selectAllData);
}
}
applicationContext.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"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="data" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="DriverClass" value="com.jdbc.mysql.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="data" value="data"></property>
</bean>
<bean id="dbinfo" class="com.jdbcSpring.dao">
<property name="jdbcTemplate" value="jdbcTemplate"></property>
</bean>
</beans>
testJdbc class
package com.jdbcSpring.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testJdbc {
public static void main(String[] args) {
ApplicationContext cont= new ClassPathXmlApplicationContext("applicationContext.xml");
DataBaseInfo dao=( DataBaseInfo) cont.getBean("dbinfo");
int insert=dao.insertPerson(new person("yousuf","ararar",33,"pisa",31));
System.out.println(insert);
}
}
please who know the solution write in the comments down
thank you
2 Comments
i change name here
ReplyDeleteto DriverClassName
and i put the property of username and password
it give me an other error
: Error creating bean with name 'data' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'DriverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.jdbc.mysql.Driver]
i put throws Exceptions after insert and delete and update and selectalldata methhods as bellow
Deletepublic int insertPerson(person p) throws Exception
it solve the broblem but have error in the code in testJdpc