Web开发常用的代码


From:http://blog.youkuaiyun.com/qqhjqs/article/details/40263021
上了四年大学,做了关于jsp的课程设计不少于五个,有关于servlet,MVC,Struts等,无论用什么框架,都是对数据库的增删查改。。。数据库都被玩烂了

想记下一些代码,加入以后再用到直接复制,不想花时间自己写了

首先是Bean(这是我看马士兵马老师教学视频 写的,都是这样,我就没有必要重新写了)

[java]   view plain  copy
  1. public class Category {  
  2.     private int id;  
  3.     private String name;  
  4.     private String description;  
  5.     public int getId() {  
  6.         return id;  
  7.     }  
  8.     public void setId(int id) {  
  9.         this.id = id;  
  10.     }  
  11.     public String getName() {  
  12.         return name;  
  13.     }  
  14.     public void setName(String name) {  
  15.         this.name = name;  
  16.     }  
  17.     public String getDescription() {  
  18.         return description;  
  19.     }  
  20.     public void setDescription(String description) {  
  21.         this.description = description;  
  22.     }  
  23.       
  24. }  
接下来就是Dao(Service名字叫法不一样)

[java]   view plain  copy
  1. public class CategoryService {  
  2.     /*增加*/  
  3.     public void add(Category c) {  
  4.         Connection conn = DB.createConn();  
  5.         String sql = "insert into _category values (null, ?, ?)";  
  6.         PreparedStatement ps = DB.prepare(conn, sql);  
  7.         try {  
  8.             ps.setString(1, c.getName());  
  9.             ps.setString(2, c.getDescription());  
  10.             ps.executeUpdate();  
  11.         } catch (SQLException e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         DB.close(ps);  
  15.         DB.close(conn);  
  16.     }  
  17.     /*全部数据*/  
  18.     public List<Category> list() {  
  19.         Connection conn = DB.createConn();  
  20.         String sql = "select * from _category";  
  21.         PreparedStatement ps = DB.prepare(conn, sql);  
  22.         List<Category> categories = new ArrayList<Category>();  
  23.         try {  
  24.             ResultSet rs = ps.executeQuery();  
  25.             Category c = null;  
  26.             while(rs.next()) {  
  27.                 c = new Category();  
  28.                 c.setId(rs.getInt("id"));  
  29.                 c.setName(rs.getString("name"));  
  30.                 c.setDescription(rs.getString("description"));  
  31.                 categories.add(c);  
  32.             }  
  33.         } catch (SQLException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         DB.close(ps);  
  37.         DB.close(conn);  
  38.         return categories;  
  39.     }  
  40.     /*删除*/  
  41.     public void delete(Category c) {  
  42.         deleteById(c.getId());  
  43.     }  
  44.     /*根据ID删除*/  
  45.     public void deleteById(int id) {  
  46.         Connection conn = DB.createConn();  
  47.         String sql = "delete from _category where id = ?";  
  48.         PreparedStatement ps = DB.prepare(conn, sql);  
  49.         try {  
  50.             ps.setInt(1, id);  
  51.             ps.executeUpdate();  
  52.         } catch (SQLException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.         DB.close(ps);  
  56.         DB.close(conn);  
  57.     }  
  58.     /*更新*/  
  59.     public void update(Category c) {  
  60.         Connection conn = DB.createConn();  
  61.         String sql = "update _category set name = ?, description = ? where id = ?";  
  62.         PreparedStatement ps = DB.prepare(conn, sql);  
  63.         try {  
  64.             ps.setString(1, c.getName());  
  65.             ps.setString(2, c.getDescription());  
  66.             ps.setInt(3, c.getId());  
  67.             ps.executeUpdate();  
  68.         } catch (SQLException e) {  
  69.             e.printStackTrace();  
  70.         }  
  71.         DB.close(ps);  
  72.         DB.close(conn);  
  73.     }  
  74.     /*根据ID查找*/  
  75.     public Category loadById(int id) {  
  76.         Connection conn = DB.createConn();  
  77.         String sql = "select * from _category where id = ?";  
  78.         PreparedStatement ps = DB.prepare(conn, sql);  
  79.         Category c = null;  
  80.         try {  
  81.             ps.setInt(1, id);  
  82.             ResultSet rs = ps.executeQuery();  
  83.           
  84.             if(rs.next()) {  
  85.                 c = new Category();  
  86.                 c.setId(rs.getInt("id"));  
  87.                 c.setName(rs.getString("name"));  
  88.                 c.setDescription(rs.getString("description"));  
  89.             }  
  90.         } catch (SQLException e) {  
  91.             e.printStackTrace();  
  92.         }  
  93.         DB.close(ps);  
  94.         DB.close(conn);  
  95.         return c;  
  96.     }  
一般情况下用到的方法就是这些了,其他再也没有什么明显的方法了

数据库的链接

[java]   view plain  copy
  1. public class DB {  
  2.     public static Connection createConn() {  
  3.         Connection conn = null;  
  4.         try {  
  5.             Class.forName("com.mysql.jdbc.Driver");  
  6.             conn = DriverManager.getConnection("jdbc:mysql://localhost/DataBaseName""root""root");  
  7.         } catch (ClassNotFoundException e) {  
  8.             e.printStackTrace();  
  9.         } catch (SQLException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.         return conn;  
  13.     }  
  14.       
  15.     public static PreparedStatement prepare(Connection conn, String sql) {  
  16.         PreparedStatement ps = null;  
  17.         try {  
  18.             ps = conn.prepareStatement(sql);  
  19.         } catch (SQLException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.         return ps;  
  23.     }  
  24.       
  25.     public static void close(Connection conn) {  
  26.           
  27.         try {  
  28.             conn.close();  
  29.             conn = null;  
  30.         } catch (SQLException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34.     public static void close(Statement stmt) {  
  35.         try {  
  36.             stmt.close();  
  37.             stmt = null;  
  38.         } catch (SQLException e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.     }  
  42.     public static void close(ResultSet rs) {  
  43.         try {  
  44.             rs.close();  
  45.             rs = null;  
  46.         } catch (SQLException e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.     }  
每次用好数据库记得要关闭资源,以前做web开发的时候没有感觉,做android开发的时候,我还是按照开发jsp的那种方式开发,数据库就有关的有不关的,然后在手机上运行的时候就会出席那“无响应”的提示,原来数据库链接把手机资源耗尽了,相比较而言,电脑资源比较大,数据库的影响对电脑没有多大的影响,但还是要把数据库关上。

上面的三段代码可以说是核心代码了,以下是学习struts2时用到的一些,也是可以用到的

action代码:

[java]   view plain  copy
  1. public class CategoryAction extends ActionSupport {  
  2.     private List<Category> categories;  
  3.     private CategoryService categoryService = new CategoryService();   
  4.     private Category category;  
  5.     private int id;  
  6.       
  7.     public String list() {  
  8.         categories = categoryService.list();  
  9.         return SUCCESS;  
  10.     }  
  11.     public String add() {  
  12.         categoryService.add(category);  
  13.         return SUCCESS;  
  14.     }  
  15.     public String update() {  
  16.         categoryService.update(category);  
  17.         return SUCCESS;  
  18.     }  
  19.     public String delete() {  
  20.         categoryService.deleteById(id);  
  21.         return SUCCESS;  
  22.     }  
  23.     public String addInput() {  
  24.           
  25.         return INPUT;  
  26.     }  
  27.     public String updateInput() {  
  28.         this.category = this.categoryService.loadById(id);  
  29.         return INPUT;  
  30.     }  
  31.     public List<Category> getCategories() {  
  32.         return categories;  
  33.     }  
  34.     public void setCategories(List<Category> categories) {  
  35.         this.categories = categories;  
  36.     }  
  37.     public CategoryService getCategoryService() {  
  38.         return categoryService;  
  39.     }  
  40.     public void setCategoryService(CategoryService categoryService) {  
  41.         this.categoryService = categoryService;  
  42.     }  
  43.     public Category getCategory() {  
  44.         return category;  
  45.     }  
  46.     public void setCategory(Category category) {  
  47.         this.category = category;  
  48.     }  
  49.     public int getId() {  
  50.         return id;  
  51.     }  
  52.     public void setId(int id) {  
  53.         this.id = id;  
  54.     }  
Struts.xml配置文件(我认为Struts在整个网页中就负责一些跳转,所以里面配置的内容就是一些跳转信息)
以上的代码片段以后绝对会用到,如果看这篇的是大一大二的学妹,如果想自己做一个系统出来的一定要记得保存哦!!!绝对有用
!!!!!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值