夢(mèng)幻之旅

          DEBUG - 天道酬勤

             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評(píng)論 :: 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) 評(píng)論(1)  編輯  收藏 所屬分類: Spring

          評(píng)論

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

          主站蜘蛛池模板: 台前县| 历史| 田阳县| 略阳县| 宣武区| 和硕县| 迁西县| 新邵县| 玉溪市| 台南市| 化德县| 桦南县| 井冈山市| 荥经县| 从江县| 芦山县| 永福县| 蒙城县| 太仓市| 阿克陶县| 永春县| 新营市| 双柏县| 海宁市| 托克逊县| 汕头市| 武安市| 宜黄县| 西乡县| 运城市| 文登市| 西安市| 克什克腾旗| 闽侯县| 华亭县| 临汾市| 巢湖市| 罗源县| 客服| 台北县| 新竹市|