好久没写jdbc的相关代码了,都忘得差不多了,一直都在用hibernate,这次学习spring,书中有关于jdbc整合的章节,并且讲了很多,于是决定再次写有关jdbc代码。整个写的非常简单,具体如下:
第一步:引入相关的jar包
第二步:在spring中配置好数据源(bean.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="spring.test"/> <!-- <aop:aspectj-autoproxy/> --> <!-- mysql 5.0 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/link?useUnicode=true&characterEncoding=gbk"/> <property name="username" value="root"/> <property name="password" value="hyj_lk"/> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <!-- 连接池的最大值 --> <property name="maxActive" value="500"/> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2"/> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1"/> </bean> </beans>
第三步:创建实体bean(说明:这里其实可以不要创建这个bean,为了方便还是创建了这个bean) Sort.java
//*******************************************************************// // //** 创建人: Link // //** 描 述: sort表实体bean -- 分类(栏目)表 // // //*******************************************************************// package spring.test.bean; import java.util.Date; public class Sort { private int id; //标识(主键) private int sid; //分类ID(父ID) private String title; //标题 private String info; //描述 private String url; //url private String imgurl; //图片路径 private int isindex=0; //是否在首页显示(0否,1是 默认0) private int ishot=0; //是否为热门(0否,1是 默认0) private int isnew=0; //是否为新(0否,1是 默认0) private int sortnum=0; //排序号 private long hits=0; //点击次数 private Date puttime=new Date(); //更新日期 private int property=0; //属性:是否通过(0否,1是 默认0) public int getId() { return id; } public void setId(int id) { this.id = id; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getImgurl() { return imgurl; } public void setImgurl(String imgurl) { this.imgurl = imgurl; } public int getIsindex() { return isindex; } public void setIsindex(int isindex) { this.isindex = isindex; } public int getIshot() { return ishot; } public void setIshot(int ishot) { this.ishot = ishot; } public int getIsnew() { return isnew; } public void setIsnew(int isnew) { this.isnew = isnew; } public int getSortnum() { return sortnum; } public void setSortnum(int sortnum) { this.sortnum = sortnum; } public long getHits() { return hits; } public void setHits(long hits) { this.hits = hits; } public Date getPuttime() { return puttime; } public void setPuttime(Date puttime) { this.puttime = puttime; } public int getProperty() { return property; } public void setProperty(int property) { this.property = property; } }
第四步:SortDao接口 SortDao.java
package spring.test.service; import spring.test.bean.*; public interface SortDao { public void insert(Sort sort); public void update(Sort sort); public void delete(int id); public Sort findById(int id); }
第五步:SortDao接口的实现 SortDaoImp.java
package spring.test.service.impl; import spring.test.bean.Sort; import spring.test.service.SortDao; import java.sql.*; import javax.annotation.Resource; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.stereotype.Service; @Service public class SortDaoImpl implements SortDao { @Resource(name="dataSource") private BasicDataSource dataSource; public void delete(int id) { System.out.println("delete OK"); } public Sort findById(int id) { return null; } public void insert(Sort sort) { String sql="insert into tb_sort (sid,title,info) values(?,?,?)"; Connection conn=null; try{ conn=dataSource.getConnection(); PreparedStatement ps=conn.prepareStatement(sql); ps.setInt(1, sort.getSid());//从1开始,这里不是从0开始,这里要特别注意,我当时不太记得了,调试了很久 ps.setString(2, sort.getTitle()); ps.setString(3, sort.getInfo()); ps.executeUpdate(); ps.close(); }catch(SQLException e){ throw new RuntimeException(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { System.out.println("关闭数据库连接失败!"); } } } } public void update(Sort sort) { } }
第六步:单元测试 SortDaoImplTest.java
package spring.test.service.junit; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.test.bean.Sort; import spring.test.service.SortDao; public class SortDaoImplTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void testDelete() { } @Test public void testFindById() { } @Test public void testInsert() { ApplicationContext context= new ClassPathXmlApplicationContext("beans2.xml"); SortDao sortDao=(SortDao)context.getBean("sortDaoImpl"); //sortDao.delete(0); Sort sort=new Sort(); sort.setSid(0); sort.setTitle("首页"); sort.setInfo("首页,欢迎光临"); sortDao.insert(sort); System.out.println("the sort save success!"); } @Test public void testUpdate() { } }