使用spring jdbc template简化jdbc数据库操作实例代码
扫描二维码
随时随地手机看文章
文章地址: http://blog.csdn.net/5iasp/article/details/12206793
作者: javaboy2012
Email:yanek@163.com
qq: 1046011462
使用spring jdbc template简化jdbc数据库操作实例代码
包括如下几个类:
1. DAO接口
package com.test;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
public interface DAO {
public int getCount(String sql);
public String getResultValue(String sql, String column);
public List getResult(String sql);
public void update(String sql);
public void update(String sql,Object[] params);
public void delete(String sql);
public JdbcTemplate getJt();
}
2. DAO接口实现类 DAOImpl
package com.test;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
public class DAOImpl implements DAO{
protected final Log log = LogFactory.getLog(this.getClass());
private JdbcTemplate jt;
public int getCount(String sql) {
int count = 0;
try {
count = jt.queryForInt(sql);
} catch (DataAccessException e) {
log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
}
return count;
}
public String getResultValue(String sql, String column) {
String value = "";
try {
SqlRowSet s = jt.queryForRowSet(sql);
while (s.next()){
value = s.getString(column);
}
} catch (DataAccessException e) {
log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
}
return value;
}
public List getResult(String sql) {
List list = null;
try {
list = jt.queryForList(sql);
} catch (DataAccessException e) {
log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
}
return list;
}
public void update(String sql) {
try {
jt.update(sql);
} catch (DataAccessException e) {
log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
}
}
public void delete(String sql) {
try {
jt.execute(sql);
} catch (DataAccessException e) {
log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
}
}
@Override
public void update(String sql, Object[] params) {
// TODO Auto-generated method stub
try {
jt.update(sql,params);
} catch (DataAccessException e) {
log.info("!!!!!!!!!!!!!!!!!!!Exception:" + e);
}
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
public JdbcTemplate getJt() {
return jt;
}
}
3. UserManager 接口
package com.test;
import java.util.List;
import java.util.Map;
public interface UserManager {
public void addUser(String name);
public void updateUser(String name,int id);
public void deleteUser(int id);
public String getUser(int id);
public User getUserByID(int id);
public List getUsers();
public List getUserList();
public void init();
}
4. UserManagerImpl:UserManager 接口实现类
package com.test;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.RowMapper;
public class UserManagerImpl implements UserManager {
private DAO dao;
/**
* @param args
*/
public static void main(String[] args) {
}
public void addUser(String name)
{
/*
String sql="insert into t_test(name) values('"+name+"')";
dao.update(sql);
*/
String sql="insert into t_test(name) values(?)";
Object[] params = new Object[] {name};
dao.update(sql, params);
}
public List getUsers()
{
String sql = "select * FROM t_test";
List
5. User类:实体类
package com.test;
public class User {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
6. SpringUtil : 工具类
package com.test;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* spring工具类
* @author
*
*/
public class SpringUtil {
/**
* @param args
*/
public static void main(String[] args) {
UserManager um= (UserManager)SpringUtil.getBean("userManager");
List users=um.getUsers();
for(int i=0;i
7. 测试类: Test
package com.test;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
/*
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserManager um=(UserManager)ctx.getBean("userManager");
*/
UserManager um= (UserManager)SpringUtil.getBean("userManager");
//新增
/*
String name="test";
um.addUser(name);
*/
//列表
List users=um.getUsers();
for(int i=0;i users2=um.getUserList();
for(int i=0;i
8. Spring 配置文件:applicationContext.xml
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1/myweb?useUnicode=true&characterEncoding=gbk
root
root
com.test.DAO
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
9. web环境下调用:
web.xml配置:
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/classes/applicationContext.xml
index.jsp
测试jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page import="com.test.*"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UserManager um = (UserManager) ctx.getBean("userManager");
%>
<%
List users2=um.getUserList();
for(int i=0;i
<%=t_id2 %>-<%=t_name2 %>
<%
System.out.println(t_id2+"-"+t_name2);
}
%>
资源下载地址(不需要下载积分):http://download.csdn.net/detail/5iasp/6344527