public class JdbcTemplateTest {
static JdbcTemplate jdbc = new JdbcTemplate(JdbcUtils.getDataSource());
public static void main(String[] args) {
}
static int addUser(final User user) {
//更灵活的使用connection
jdbc.execute(new ConnectionCallback() {
public Object doInConnection(Connection con) throws SQLException,
DataAccessException {
String sql = "insert into user(name,birthday, money) values (?,?,?) ";
PreparedStatement ps = con.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS);
ps.setString(1, user.getName());
ps.setDate(2, new java.sql.Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next())
user.setId(rs.getInt(1));
return null;
}
});
return 0;
}
static Map getData(int id) {
String sql = "select id as userId, name, money, birthday from user where id="
+ id;
return jdbc.queryForMap(sql);
}
static String getUserName(int id) {
String sql = "select name from user where id=" + id;
Object name = jdbc.queryForObject(sql, String.class);
return (String) name;
}
static int getUserCount() {
String sql = "select count(*) from user";
return jdbc.queryForInt(sql);
}
static List findUsers(int id) {
String sql = "select id, name, money, birthday from user where id<?";
Object[] args = new Object[] { id };
int[] argTypes = new int[] { Types.INTEGER };
List users = jdbc.query(sql, args, argTypes, new BeanPropertyRowMapper(
User.class));
return users;
}
static User findUser(String name) {
String sql = "select id, name, money, birthday from user where name=?";
Object[] args = new Object[] { name };
Object user = jdbc.queryForObject(sql, args, new BeanPropertyRowMapper(
User.class));
return (User) user;
}
static User findUser1(String name) {
String sql = "select id, name, money, birthday from user where name=?";
Object[] args = new Object[] { name };
Object user = jdbc.queryForObject(sql, args, new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setMoney(rs.getFloat("money"));
user.setBirthday(rs.getDate("birthday"));
return user;
}
});
return (User) user;
}
}
public class JdbcTemplateTest {
public static JdbcTemplate jdbcTemplet = new JdbcTemplate(JdbcUtils
.getDataSource());
public static void main(String[] args) {
}
// 查询返回对象类型
public static User getUser(String name) {
String sql = "select id,name,password from user where name=?";
Object[] args = new Object[] { name };
Object user = jdbcTemplet.queryForObject(sql, args,
new BeanPropertyRowMapper(User.class));
return (User) user;
}
// 查询返回List类型
@SuppressWarnings("unchecked")
public static List<User> queryUserForList() {
String sql = "select id,name,password from user";
return jdbcTemplet.query(sql, new BeanPropertyRowMapper(User.class));
}
// 插入
public static void insert() {
String sql = "insert into user(name,password) values(?,?)";
jdbcTemplet.update(sql, new Object[] { "wanger", "123456" });
}
// 删除
public static void delete(int id) {
String sql = "delete from user where id=?";
jdbcTemplet.update(sql, new Object[] { 4 });
}
// 更新
public static void update(User user) {
String sql = "update user set name=?,password=? where id=?";
Object[] args = new Object[] { "张三", "000000", 1 };
int[] argTypes = new int[] { Types.VARCHAR, Types.VARCHAR,
Types.INTEGER };
jdbcTemplet.update(sql, args, argTypes);
}
// 插入日期类型
public static void insert2() {
String sql = "insert into date_table(time) values(?)";
Date date = new Date();
Object[] args = new Object[] { date };
// int[] argTypes = new int[] { Types.TIMESTAMP };
// jdbcTemplet.update(sql, args, argTypes);
jdbcTemplet.update(sql, args);
}
}