后台调用数据库

在类中:

import org.springframework.jdbc.core.JdbcTemplate;

public JdbcTemplate jdbcTemplate;

 

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

this.jdbcTemplate = jdbcTemplate;

}

在方法中:

Connection conn = null;

Statement stm = null;

try {

conn = jdbcTemplate.getDataSource().getConnection();

conn.setAutoCommit(false);

stm = conn.createStatement();

String updateSql = "";

stm.executeUpdate(updateSql);//修改、删除

conn.commit();

}catch (Exception e) {

LogUtil.error(e.getMessage(),e);

try {

conn.rollback();

} catch (SQLException e1) {

LogUtil.error(e1.getMessage(),e1);

}

} finally {

try {

if (stm != null) {

stm.close();

}

if (conn != null) {

conn.close();

}

} catch (Exception e) {

LogUtil.error(e.getMessage(),e);

}

}

查询:

StringBuffer sql = new StringBuffer();

sql.append(" SELECT ");

List<CLS_VO_Obj>objList = jdbcTemplate.queryForList(sql.toString(), CLS_VO_Obj.class);

猜你喜欢

转载自www.cnblogs.com/7q4w1e/p/9592441.html
今日推荐