portal.jsp
上传用户:shjgzm
上传日期:2017-08-31
资源大小:2757k
文件大小:2k
源码类别:

Ajax

开发平台:

Java

  1. <%@ page contentType="text/plain; charset=UTF-8"%>
  2. <%@ page language="java"%>
  3. <%@ page import="java.sql.*,ajax.db.DBUtils,java.util.*"%>
  4. <%!
  5.     void updatePortal(String[] column1, String[] column2, String[] column3) {
  6.         int counter = 1;
  7.         String sql = "update portal set seq = ?, col = ? where id = ?";   //定义更新数据库的SQL语句
  8.         Connection conn = null;                 //声明Connection对象
  9.         PreparedStatement pstmt = null;         //声明PreparedStatement对象
  10.         try {
  11.             conn = DBUtils.getConnection();     //获取数据库连接
  12.             pstmt = conn.prepareStatement(sql); //根据sql创建PreparedStatement
  13.             if (column1 != null) {
  14.                 for (int i=0; i<column1.length; i++) {
  15.                     pstmt.setInt(1, counter++);
  16.                     pstmt.setInt(2, 1);
  17.                     pstmt.setString(3, column1[i].substring(4));
  18.                     pstmt.addBatch();
  19.                 }
  20.             }
  21.             if (column2 != null) {
  22.                 for (int i=0; i<column2.length; i++) {
  23.                     pstmt.setInt(1, counter++);
  24.                     pstmt.setInt(2, 2);
  25.                     pstmt.setString(3, column2[i].substring(4));
  26.                     pstmt.addBatch();
  27.                 }
  28.             }
  29.             if (column3 != null) {
  30.                 for (int i=0; i<column3.length; i++) {
  31.                     pstmt.setInt(1, counter++);
  32.                     pstmt.setInt(2, 3);
  33.                     pstmt.setString(3, column3[i].substring(4));
  34.                     pstmt.addBatch();
  35.                 }
  36.             }
  37.             pstmt.executeBatch();
  38.         } catch (SQLException e) {
  39.             System.out.println(e.toString());
  40.         } finally {
  41.             DBUtils.close(pstmt);               //关闭PreparedStatement
  42.             DBUtils.close(conn);                //关闭连接
  43.         }
  44.     }
  45. %>
  46. <%
  47.     out.clear();                                        //清空当前的输出内容(空格和换行符)
  48.     request.setCharacterEncoding("UTF-8");              //设置请求字符集为UTF-8
  49.     String[] column1 = request.getParameterValues("column1[]");
  50.     String[] column2 = request.getParameterValues("column2[]");
  51.     String[] column3 = request.getParameterValues("column3[]");
  52.     updatePortal(column1, column2, column3);
  53. %>