java_jdbc标准模板

package cn.edu.imut.ies.dao; import java.util.List; public interface BaseDao { } int insert (T obj); int update (T obj); int delete (int id); T queryObject (int id); List queryAll(); int queryCount(); List queryPage(int offset,int size); int querySearchCount(String search); List querySearch(String search,int offset,int size);

package cn.edu.imut.ies.dao; import cn.edu.imut.ies.pojo.User; public interface UserDao extends BaseDao { }

package cn.edu.imut.ies.dao.factory; import cn.edu.imut.ies.dao.UserDao; import cn.edu.imut.ies.dao.impl.UserDaoImpl; public class DaoFactory { } public static UserDao geUserDao() { } return new UserDaoImpl();

package cn.edu.imut.ies.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import cn.edu.imut.ies.dao.UserDao; import cn.edu.imut.ies.db.DBUtils; import cn.edu.imut.ies.pojo.User; public class UserDaoImpl implements UserDao { @Override public int insert(User obj) { // TODO Auto-generated method stub int flag = 0; String sql=\into users(id,username,password)values(users_seq.nextval,?,?)\ //?占位符 Connection connection = DBUtils.getConnection(); PreparedStatement psmt=null; try { psmt = connection.prepareStatement(sql); psmt.setString(1, obj.getUserName()); psmt.setString(2, obj.getPassword());

} flag = psmt.executeUpdate(); } catch (SQLException e1) { } try { psmt.close(); connection.close(); // TODO Auto-generated catch block e1.printStackTrace(); } catch (SQLException e) { } return flag; // TODO Auto-generated catch block e.printStackTrace(); @Override public int update(User obj) { // TODO Auto-generated method stub int flag = 0; String sql=\ where id = ?\//?占位符 Connection connection = DBUtils.getConnection(); PreparedStatement psmt=null; try { psmt = connection.prepareStatement(sql); psmt.setString(1, obj.getUserName()); psmt.setString(2, obj.getPassword());

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4