alter table tfw_dash_user drop constraint tfw_dash_user_id_pk; drop table tfw_dash_user; drop sequence tfw_seq_dash_user_id; create table tfw_dash_user( id number (8), name varchar2 (48BYTE), passwd varchar2 (96BYTE), stat number (1), remark varchar2 (768BYTE) ); alter table tfw_dash_user add constraint tfw_dash_user_id_pk primary key(id); create sequence tfw_seq_dash_user_id start with 10000 increment by 1; desc tfw_dash_user;
……
public interface I_UserDAO
{
……
int create(User user) throws SQLException;
……
User findById(Integer itgUserId) throws SQLException;
……
List<User> listByName(String strUserName) throws SQLException;
……
List<User> listAll() throws SQLException;
……
List<User> listByRange(int intStartRowIdx, int intSelectRowCount)
throws SQLException;
……
int update(User user) throws SQLException;
……
int discardById(Integer itgUserId) throws SQLException;
}
……
public class UserDAO implements I_UserDAO
{
……
private DataSource ds;
……
public void setDataSource(DataSource ds)
{
this.ds = ds;
}
……
public UserDAO()
{
}
……
public UserDAO(DataSource ds)
{
setDataSource(ds);
}
……
@Override
public String toString()
{
return "UserDAO [ds=" + ds + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <bean id="ds" ……> …… </bean> <bean id="crud.basic_jdbc.userDAO" class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.dao.impl.oracle.UserDAO"> <constructor-arg index="0" ref="ds"/> </bean> </beans>
…… public class UserMgrTestCase { …… @Test public void testObjects() throws FileNotFoundException, SQLException { …… Connection cnct = ds.getConnection(); System.out.println("Connection:\n\t" + cnct); // I_UserDAO userDAO = new UserDAO(ds); I_UserDAO userDAO = actxt.getBean("crud.basic_jdbc.userDAO", I_UserDAO.class); System.out.println("DAO:\n\t" + userDAO); cnct.close(); System.out.println("Connection Closed:\n\t" + cnct); …… } }
…… public class UserDAO implements I_UserDAO { …… public UserDAO(DataSource ds) { …… } …… public int create(User user) throws SQLException { …… try { String strSql = "insert into tfw_dash_user(id, name, passwd, stat, remark) values(tfw_seq_dash_user_id.nextval, ?, ?, ?, ?)"; System.out.println(strSql); cnct = ds.getConnection(); // [!] Pit! Note the second argument! pstmt = cnct.prepareStatement(strSql, new String[] { "id" }); pstmt.setString(1, user.getName()); …… int intJdbcCode = pstmt.executeUpdate(); rs = pstmt.getGeneratedKeys(); rs.next(); Object oCol1 = rs.getObject(1); user.setId(NumberToolE.o2I(oCol1, null)); return intJdbcCode; } catch (SQLException e) { …… } finally { …… } } …… …… public List<User> listByRange(int intStartRowIdx, int intSelectRowCount) throws SQLException { …… // from ( // select rownum rn, t.* // from ( // select id, name, passwd, stat, remark // from tfw_dash_user // order by name, id // ) t // where rownum<=? // ) // where rn>=? String strSql = TextToolE.concat( "select id, name, passwd, stat, remark\n", "from (\n", " select rownum rn, t.*\n", " from (\n", " select id, name, passwd, stat, remark\n", " from tfw_dash_user\n", " order by name, id\n", " ) t\n", " where rownum<=?\n", ")\n", "where rn>=?\n"); …… } …… @Override public String toString() { …… } …… }
…… public class UserMgrTestCase { private String strClasspathConfFile = "tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml"; private AbstractApplicationContext actxt; private I_UserDAO userDAO; @Test public void testObjects() throws FileNotFoundException, SQLException { …… } @Before public void init() { System.out.println("Spring Configuration File:\n\t" + strClasspathConfFile); actxt = new ClassPathXmlApplicationContext(strClasspathConfFile); System.out.println("Application Context:\n\t" + actxt); userDAO = actxt.getBean("crud.basic_jdbc.userDAO", I_UserDAO.class); System.out.println("DAO:\n\t" + userDAO); } @After public void close() { actxt.close(); System.out.println("Application Context Closed:\n\t" + actxt); } @Test public void testUserDAO_create() throws SQLException { // [S] Preparing. User user; { System.out.println("DAO:\n\t" + userDAO); String strDate = DateToolE.simpleFormat(new Date(), "HH-mm-ss.SSS"); String strPasswd = null; // "Password"; user = // null; new User(null, "Name_" + strDate, strPasswd, new Byte((byte) 0), "Remark_" + strDate); System.out.println("User:\n\t" + user); } // [E] Preparing. // Main. int intRtnCode = userDAO.create(user); System.out.println("Return Code:\n\t" + intRtnCode); System.out.println("User:\n\t" + user); } @Test public void testUserDAO_findById() throws SQLException { …… } …… private void showUsers(List>User> lstUsers) { if (null == lstUsers) { System.out.println(lstUsers); } else if (1 > lstUsers.size()) { System.out.println("Empty!"); } else { for (User user : lstUsers) { System.out.println(user); } } } }