package user.jdbctemplate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
//操作用户的接口类
public class userTemplate {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
public int getallCount(String sql) {
System.out.println(sql);
int count = 1;
try {
count = jdbcTemplate.queryForInt(sql);
} catch (DataAccessException e) {
System.out.println("getallCount里面的报错是:" + e);
return count;
}
return count;
}
public String saveorupdate(String sql) {
System.out.println(sql);
try {
jdbcTemplate.update(sql);
} catch (DataAccessException e) {
System.out.println(e);
return "returnfail";
}
return "returnsuc";
}
/*
* 1.KeyHolder keyHolder = new GeneratedKeyHolder();
* 2.jdbcTemplate.update(new PreparedStatementCreator() { 3. public
* PreparedStatement createPreparedStatement(Connection connection) throws
* SQLException { 4. PreparedStatement ps = connection.prepareStatement( 5.
* "INSERT INTO Test (id, NAME) VALUES (phoenix_sequence.nextval,?)", new
* String[] { "ID" }); 6. ps.setString(1, "heipark"); 7. return ps; 8. }
* 9.}, keyHolder); 10.Long generatedId = keyHolder.getKey().longValue();
* 11.System.out.println(generatedId);
*/
/*
* final String sql =
* "insert into TAG_INFO(SITE_ID,NAME,CONTENT) values(?,?,'"+content+"')";
* KeyHolder keyHolder = new GeneratedKeyHolder(); getJdbcTemplate().update(
* new PreparedStatementCreator() { public PreparedStatement
* createPreparedStatement(Connection con) throws SQLException {
* PreparedStatement ps = getJdbcTemplate().getDataSource()
* .getConnection().prepareStatement(sql,new String[]{ "SITE_ID" ,"NAME"});
* ps.setString(1, "站点号"); ps.setString(2, "我的名字"); return ps; } },
* keyHolder); System.out.println("自动插入id============================" +
* keyHolder.getKey().intValue()); return keyHolder.getKey().intValue();
*/
public int saveProject(String sql) {
final String innersql = sql;
KeyHolder keyHolder = new GeneratedKeyHolder();
System.out.println(innersql);
/*
* System.out.println("插入方案记录,获取当前projectid"); int
* projectid=jdbcTemplate.update(sql);
*/
try {
jdbcTemplate.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con)
throws SQLException {
PreparedStatement ps = jdbcTemplate.getDataSource()
.getConnection().prepareStatement(innersql);
return ps;
}
}, keyHolder);
} catch (DataAccessException e) {
e.printStackTrace();
}
System.out.println("自动插入id============================"
+ keyHolder.getKey().intValue());
return keyHolder.getKey().intValue();
}
public List findAll(String sql) {
List rows;
try {
rows = jdbcTemplate.queryForList(sql);
} catch (DataAccessException e) {
return null;
}
Iterator it = rows.iterator();
if (it.hasNext()) {
return rows;
}
return null;
}
public Object findusermoney(String sql) {
List rows;
Object money = "0.00";
try {
rows = jdbcTemplate.queryForList(sql);
} catch (DataAccessException e) {
return null;
}
Iterator it = rows.iterator();
if (it.hasNext()) {
Object o = rows.get(0);
System.out.println("用户的余额为:" + money);
return money;
}
return null;
}
public String findexistuser(String sql) {
List rows;
String returnstr = "returnsuc";
try {
rows = jdbcTemplate.queryForList(sql);
} catch (DataAccessException e) {
return "fail";
}
Iterator it = rows.iterator();
if (it.hasNext()) {
return returnstr;
}
return "fail";
}
public String findexistusername(String sql) {
List rows;
String returnstr = "returnsuc";
try {
rows = jdbcTemplate.queryForList(sql);
} catch (DataAccessException e) {
return "fail";
}
Iterator it = rows.iterator();
if (false == it.hasNext()) {
return returnstr;
}
return "fail";
}
public List
List rows;
try {
rows = jdbcTemplate.queryForList(sql);
} catch (DataAccessException e) {
return null;
}
Iterator it = rows.iterator();
if (it.hasNext()) {
return rows;
}
return null;
}
}