From:http://blog.youkuaiyun.com/qqhjqs/article/details/40263021
上了四年大学,做了关于jsp的课程设计不少于五个,有关于servlet,MVC,Struts等,无论用什么框架,都是对数据库的增删查改。。。数据库都被玩烂了
想记下一些代码,加入以后再用到直接复制,不想花时间自己写了
首先是Bean(这是我看马士兵马老师教学视频 写的,都是这样,我就没有必要重新写了)
- public class Category {
- private int id;
- private String name;
- private String description;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- }
- public class CategoryService {
- /*增加*/
- public void add(Category c) {
- Connection conn = DB.createConn();
- String sql = "insert into _category values (null, ?, ?)";
- PreparedStatement ps = DB.prepare(conn, sql);
- try {
- ps.setString(1, c.getName());
- ps.setString(2, c.getDescription());
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- DB.close(ps);
- DB.close(conn);
- }
- /*全部数据*/
- public List<Category> list() {
- Connection conn = DB.createConn();
- String sql = "select * from _category";
- PreparedStatement ps = DB.prepare(conn, sql);
- List<Category> categories = new ArrayList<Category>();
- try {
- ResultSet rs = ps.executeQuery();
- Category c = null;
- while(rs.next()) {
- c = new Category();
- c.setId(rs.getInt("id"));
- c.setName(rs.getString("name"));
- c.setDescription(rs.getString("description"));
- categories.add(c);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- DB.close(ps);
- DB.close(conn);
- return categories;
- }
- /*删除*/
- public void delete(Category c) {
- deleteById(c.getId());
- }
- /*根据ID删除*/
- public void deleteById(int id) {
- Connection conn = DB.createConn();
- String sql = "delete from _category where id = ?";
- PreparedStatement ps = DB.prepare(conn, sql);
- try {
- ps.setInt(1, id);
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- DB.close(ps);
- DB.close(conn);
- }
- /*更新*/
- public void update(Category c) {
- Connection conn = DB.createConn();
- String sql = "update _category set name = ?, description = ? where id = ?";
- PreparedStatement ps = DB.prepare(conn, sql);
- try {
- ps.setString(1, c.getName());
- ps.setString(2, c.getDescription());
- ps.setInt(3, c.getId());
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- DB.close(ps);
- DB.close(conn);
- }
- /*根据ID查找*/
- public Category loadById(int id) {
- Connection conn = DB.createConn();
- String sql = "select * from _category where id = ?";
- PreparedStatement ps = DB.prepare(conn, sql);
- Category c = null;
- try {
- ps.setInt(1, id);
- ResultSet rs = ps.executeQuery();
- if(rs.next()) {
- c = new Category();
- c.setId(rs.getInt("id"));
- c.setName(rs.getString("name"));
- c.setDescription(rs.getString("description"));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- DB.close(ps);
- DB.close(conn);
- return c;
- }
数据库的链接
- public class DB {
- public static Connection createConn() {
- Connection conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:mysql://localhost/DataBaseName", "root", "root");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- public static PreparedStatement prepare(Connection conn, String sql) {
- PreparedStatement ps = null;
- try {
- ps = conn.prepareStatement(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return ps;
- }
- public static void close(Connection conn) {
- try {
- conn.close();
- conn = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void close(Statement stmt) {
- try {
- stmt.close();
- stmt = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void close(ResultSet rs) {
- try {
- rs.close();
- rs = null;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
上面的三段代码可以说是核心代码了,以下是学习struts2时用到的一些,也是可以用到的
action代码:
- public class CategoryAction extends ActionSupport {
- private List<Category> categories;
- private CategoryService categoryService = new CategoryService();
- private Category category;
- private int id;
- public String list() {
- categories = categoryService.list();
- return SUCCESS;
- }
- public String add() {
- categoryService.add(category);
- return SUCCESS;
- }
- public String update() {
- categoryService.update(category);
- return SUCCESS;
- }
- public String delete() {
- categoryService.deleteById(id);
- return SUCCESS;
- }
- public String addInput() {
- return INPUT;
- }
- public String updateInput() {
- this.category = this.categoryService.loadById(id);
- return INPUT;
- }
- public List<Category> getCategories() {
- return categories;
- }
- public void setCategories(List<Category> categories) {
- this.categories = categories;
- }
- public CategoryService getCategoryService() {
- return categoryService;
- }
- public void setCategoryService(CategoryService categoryService) {
- this.categoryService = categoryService;
- }
- public Category getCategory() {
- return category;
- }
- public void setCategory(Category category) {
- this.category = category;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
以上的代码片段以后绝对会用到,如果看这篇的是大一大二的学妹,如果想自己做一个系统出来的一定要记得保存哦!!!绝对有用
!!!!!!!