夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          package hvp.spring.jdbc.jdbctemplate;

          import hvp.spring.commons.inim.UserPO;

          import java.sql.Connection;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.util.List;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import org.springframework.jdbc.core.JdbcTemplate;
          import org.springframework.jdbc.core.PreparedStatementCreator;
          import org.springframework.jdbc.core.RowMapper;
          import org.springframework.jdbc.support.GeneratedKeyHolder;
          import org.springframework.jdbc.support.KeyHolder;

          public class UserDAO
          {
              JdbcTemplate jt;
              
              
          public UserDAO()
              
          {
                  
          this.jt = this.getJdbcTemplate2();
              }

              
              
          public JdbcTemplate getJdbcTemplate2()
              
          {
                  String configPath 
          = "hvp/spring/jdbc/jdbctemp/beans.xml";
                  ApplicationContext ctx 
          = new ClassPathXmlApplicationContext(configPath);
                  
          return (JdbcTemplate) ctx.getBean("jdbcTemplate");
              }

              
              
          public void createTable()
              
          {
                  StringBuffer sql 
          = new StringBuffer();
                  sql.append(
          "CREATE TABLE t_user(");
                  sql.append(
          "user_id int AUTO_INCREMENT PRIMARY KEY,");
                  sql.append(
          "user_name VARCHAR(32)");
                  sql.append(
          ")");
                  jt.execute(sql.toString());
              }

              
              
          public UserPO addUser(final UserPO po)
              
          {
                  
          final String sql = "INSERT INTO t_user(user_name) VALUES (?)";
                  KeyHolder keyHolder 
          = new GeneratedKeyHolder();
                  jt.update(
          new PreparedStatementCreator()
                      
          {
                          
          public PreparedStatement createPreparedStatement(Connection conn)
                                  
          throws SQLException
                          
          {
                              PreparedStatement ps 
          = conn.prepareStatement(sql);
                              ps.setString(
          1, po.getUserName());
                              
          return ps;
                          }

                      }
          , keyHolder);
                  po.setUserId(keyHolder.getKey().intValue());
                  System.out.println(po.getUserId() 
          + ":" + po.getUserName());
                  
          return po;
              }

              
              @SuppressWarnings(
          "unchecked")
              
          public List<UserPO> queryUserPOs()
              
          {
                  StringBuffer sql 
          = new StringBuffer();
                  sql.append(
          "SELECT user_id,user_name FROM t_user");
                  sql.append(
          " LIMIT ");
                  sql.append(
          0);
                  sql.append(
          ",");
                  sql.append(
          10);
                  
                  
          return jt.query(sql.toString(), new RowMapper()
                      
          {
                          
          public Object mapRow(ResultSet rs, int index)
                                  
          throws SQLException
                          
          {
                              UserPO userPO 
          = new UserPO();
                              userPO.setUserId(rs.getInt(
          "user_id"));
                              userPO.setUserName(rs.getString(
          "user_name"));
                              
          return userPO;
                          }

                      }
          );
              }

              
              
          public UserPO findUser(Integer userId)
              
          {
                  String sql 
          = "SELECT user_id,user_name FROM t_user WHERE user_id=?";
                  
          return (UserPO) jt.queryForObject(sql, new Object[] { userId },
                          
          new RowMapper()
                              
          {
                                  
          public Object mapRow(ResultSet rs, int index)
                                          
          throws SQLException
                                  
          {
                                      UserPO userPO 
          = new UserPO();
                                      userPO.setUserId(rs.getInt(
          "user_id"));
                                      userPO.setUserName(rs.getString(
          "user_name"));
                                      
          return userPO;
                                  }

                              }
          );
              }

              
              
          public int getCount()
              
          {
                  String sql 
          = "SELECT COUNT(*) FROM t_user";
                  
          return jt.queryForInt(sql);
              }

              
              
          public static void main(String[] args)
              
          {
                  UserDAO ct 
          = new UserDAO();
                  
          // UserPO user = new UserPO();
                  
          // user.setUserName("Huy Vanlee");
                  
          // ct.addUser(user);
                  System.out.println(ct.queryUserPOs());
              }

          }

          posted on 2008-08-10 17:46 HUIKK 閱讀(1584) 評論(1)  編輯  收藏 所屬分類: Spring

          評論

          # re: Spring JdbcTemplate 2014-05-04 10:34 zuidaima
          請參考代碼:Spring JdbcTemplate框架搭建及其增刪改查使用指南,下載地址:http://www.zuidaima.com/share/1724429678644224.htm  回復  更多評論
            

          主站蜘蛛池模板: 富锦市| 汤阴县| 宕昌县| 左云县| 肇源县| 河东区| 义乌市| 马公市| 营口市| 南川市| 乌鲁木齐市| 勐海县| 九龙城区| 黑河市| 双城市| 饶阳县| 吐鲁番市| 米泉市| 晋宁县| 隆子县| 松溪县| 伊金霍洛旗| 正定县| 彰化市| 湘潭县| 栖霞市| 阜阳市| 克拉玛依市| 澳门| 安徽省| 萍乡市| 德令哈市| 浏阳市| 额尔古纳市| 聂荣县| 额济纳旗| 平邑县| 霍林郭勒市| 永城市| 合阳县| 香格里拉县|