spring+derby
package com.ray.insurance.dao;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.derby.jdbc.EmbeddedDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import com.ray.insurance.model.WishList;
public class DerbyManager
{
private SimpleJdbcTemplate template;
// private EmbeddedDataSource dataSource;
public void setDataSource(EmbeddedDataSource dataSource)
{
this.template = new SimpleJdbcTemplate(dataSource);
}
public List<WishList> getList()
{
String sql = "select WISH_ID, ENTRY_DATE, WISH_ITEM from WISH_LIST";
ParameterizedRowMapper<WishList> mapper = new ParameterizedRowMapper<WishList>()
{
public WishList mapRow(ResultSet rs, int rowNum) throws SQLException
{
WishList wishList = new WishList();
wishList.setWishId(rs.getInt("WISH_ID"));
wishList.setEntryDate(rs.getTimestamp("ENTRY_DATE"));
wishList.setWishItem(rs.getString("WISH_ITEM"));
return wishList;
}
};
return template.query(sql, mapper);
}
public void insert(String a)
{
String sql = "insert into wish_list(ENTRY_DATE, WISH_ITEM) values(?, ?)";
Map<String, Object> parameters = new HashMap<String, Object>(2);
parameters.put("ENTRY_DATE", new Date());
parameters.put("WISH_ITEM", a);
template.update(sql, new Object[]{new Date(), a});
}
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DerbyManager manager = (DerbyManager)context.getBean("derbyManager");
manager.insert("item1");
// manager.dataSource.setShutdownDatabase("shutdown");
List<WishList> list = manager.getList();
for (WishList wishList : list)
{
System.out.println(wishList.getWishId());
System.out.println(wishList.getEntryDate());
System.out.println(wishList.getWishItem());
}
}
}
package com.ray.insurance.model;
import java.util.Date;
public class WishList
{
private int wishId;
private Date entryDate;
private String wishItem;
public int getWishId()
{
return wishId;
}
public void setWishId(int wishId)
{
this.wishId = wishId;
}
public Date getEntryDate()
{
return entryDate;
}
public void setEntryDate(Date entryDate)
{
this.entryDate = entryDate;
}
public String getWishItem()
{
return wishItem;
}
public void setWishItem(String wishItem)
{
this.wishItem = wishItem;
}
}
<?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-2.0.xsd">
<!--数据源 src -->
<bean id="dataSource" class="org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource">
<property name="databaseName" value="jdbcDemoDB" />
</bean>
<bean id="derbyManager"
class="com.ray.insurance.dao.DerbyManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
<img alt="">
页:
[1]