码迷,mamicode.com
首页 > 数据库 > 详细

java EE实现动态SQL的

时间:2015-04-02 01:10:41      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

结果图

情况一:

 技术分享

技术分享

情况二:

 技术分享

 技术分享

预先软件准备

Eclipse软件

JDK 最好是1.5以上

Tomcat

MySQL

Windows xp及其以上版本的操作系统

浏览器(随便哪一种的较新版本)

 

jquery-1.11.2.js------------网络上自己下载(“百度”à“jquery-1.11.2.js包”)

mysql-connector-java-5.0.5-bin.jar------网络上自己下载(“百度”à”mysql驱动jar包”)

 

项目截图

 技术分享

 技术分享

 技术分享

具体代码如下

JSP网页

report_select.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript" src="../js/jquery-1.11.2.js"></script>

<script type="text/javascript" src="../js/mannager.js"></script>

<title>打印选项</title>

</head>

<body>

<button type="button" class="button" onclick="SelectAllBox()" value="全部选中">全部选中</button>

&nbsp;&nbsp;

<button type="button" class="button" onclick="UnSelectAllBox()" value="全部不选中">全部不选中</button>

<br>

<form action="../Report_select" method="post">

<fieldset style="width: 80%; clear: left">

       <legend>&nbsp;

     <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op00" id="user_user" value="user">用户名单

       &nbsp;</legend>

       <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op0" id="user_username" value="username">用户名

       <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op0" id="user_pwd_encrypt" value="pwd_encrypt">密码密文

       <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op0" id="user_sex" value="sex">性别

       <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op0" id="user_ID_number" value="ID_number">身份证号

       <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op0" id="user_phone" value="phone">联系电话

     <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op0" id="user_email" value="email">邮箱

       <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op0" id="user_home_address" value="home_address">家庭住址

       <input type="checkbox" class="checkbox" onclick="getSelect(this.id)" name="user_op0" id="user_info_status" value="info_status">状态

<br><input type="submit" value="确定">

</fieldset>

</form>

</body>

</html>

report_make.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ page import="bean.*,dao.*,java.util.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>打印确认并输出文档</title>

</head>

<body>

<%

    String[] s = (String[]) request.getAttribute("user");

    LinkedList<User> list = null;

    list = (LinkedList<User>) request.getAttribute("list");

    int i = 0;

%>

<center>

<table border=1>

    <tr>

       <%

           for (i = 0; i < s.length; i++) {

       %>

       <th><%=s[i]%></th>

       <%

           }

       %>

    </tr>

    <tr>

       <%

           int j = 0;

           while (list.size() > j) {

              /*

               *user表的预选择项:

               * 0 username

               * 1 pwd_encrypt

               * 2 sex

               * 3 ID_number

               * 4 phone

               * 5 email

               * 6 home_address

               * 7 info_status

               */

              for (i = 0; i < s.length; i++) {

                  if (s[i].equalsIgnoreCase("username")) {

       %>

       <td><%=list.get(j).getUsername()%></td>

       <%

           i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("pwd_encrypt")) {

       %>

       <td><%=list.get(j).getPwd_encrypt()%></td>

       <%

           i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("sex")) {

       %>

       <td><%=list.get(j).getSex()%></td>

       <%

           i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("ID_number")) {

       %>

       <td><%=list.get(j).getID_number()%></td>

       <%

           i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("phone")) {

       %>

       <td><%=list.get(j).getPhone()%></td>

       <%

           i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("email")) {

       %>

       <td><%=list.get(j).getEmail()%></td>

       <%

           i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("home_address")) {

       %>

       <td><%=list.get(j).getHome_address()%></td>

       <%

           i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("info_status")) {

       %>

       <td><%=list.get(j).getInfo_status()%></td>

       <%

                  }

              }

       %>

    </tr>

    <%

       j++;

       }

    %>

</table>

</center>

</body>

</html>Java文件

jdbcUtil.java

package connect;

import java.sql.*;

public class jdbcUtil {

    public static String url ="jdbc:mysql://localhost:3306/project?userUnicode=true&characterEncoding=UTF-8";

    public static String user = "root";

    public static String password = "123456";

    public static String driver = "com.mysql.jdbc.Driver";

    public static Connection getConnection() {

       Connection conn = null;

       try {

           Class.forName(driver);

           conn = DriverManager.getConnection(url, user, password);

       } catch (Exception e) {

           e.printStackTrace();

       }

       return conn;

    }

 

    public static void close(ResultSet rs, Statement sm, Connection conn) {

       try {

           if (rs != null)

              rs.close();

           if (sm != null)

              sm.close();

           if (conn != null)

              conn.close();

       } catch (Exception e) {

           e.printStackTrace();

       }

    }

 

    public static ResultSet doQuery(String sql) {

       Connection conn = getConnection();

       if (conn == null)

           return null;

       Statement sm = null;

       ResultSet rs = null;

       try {

           sm = conn.createStatement();

           rs = sm.executeQuery(sql);

           return rs;

       } catch (SQLException e) {

           e.printStackTrace();

       } finally {

           if (rs == null && sm != null) {

              close(null, sm, conn);

           }

       }

       return rs;

    }

 

    public static int doUpdate(String sql, String[] paras) {

       Connection conn = getConnection();

       if (conn == null)

           return 0;

       PreparedStatement psm = null;

       int result = 0;

       try {

           psm = conn.prepareStatement(sql);

           for (int i = 0; i < paras.length; i++) {

              psm.setString(i + 1, paras[i]);

           }

           result = psm.executeUpdate();

       } catch (Exception e) {

           e.printStackTrace();

       } finally {

           close(null, psm, conn);

       }

       return result;

    }  

}

User.java

package bean;

public class User {

    private int uid;

    private String username;

    private String sex;

    private String userpwd;

    private String pwd_encrypt;

    private String ID_number;

    private String phone;

    private String email;

    private String home_address;

    private String info_status;

    public User() {

    }

    public int getUid() {

       return uid;

    }

    public void setUid(int uid) {

       this.uid = uid;

    }

    public String getUsername() {

       return username;

    }

    public void setUsername(String username) {

       this.username = username;

    }

    public String getUserpwd() {

       return userpwd;

    }

    public void setUserpwd(String userpwd) {

       this.userpwd = userpwd;

    }

    public String getPwd_encrypt() {

       return pwd_encrypt;

    }

    public void setPwd_encrypt(String pwd_encrypt) {

       this.pwd_encrypt = pwd_encrypt;

    }

    public void setSex(String sex) {

       this.sex = sex;

    }

    public String getSex() {

       return sex;

    }

    public void setID_number(String iD_number) {

       ID_number = iD_number;

    }

    public String getID_number() {

       return ID_number;

    }

    public void setPhone(String phone) {

       this.phone = phone;

    }

    public String getPhone() {

       return phone;

    }

    public void setEmail(String email) {

       this.email = email;

    }

    public String getEmail() {

       return email;

    }

    public void setHome_address(String home_address) {

       this.home_address = home_address;

    }

    public String getHome_address() {

       return home_address;

    }

    public void setInfo_status(String info_status) {

       this.info_status = info_status;

    }

    public String getInfo_status() {

       return info_status;

    }

}

UserDao.java

import java.sql.*;

import java.util.LinkedList;

import bean.User;

import connect.jdbcUtil;

// 随机获取预选项的SQL处理

    // get all user message

    public static LinkedList<User> getUser(String[] s) {

       String sql = "select ";

       for (int i = 0; i < s.length - 1; i++) {

           sql = sql + s[i] + ",";

       }

       sql = sql + s[s.length - 1] + " from `user`";

       System.out.println(sql);

       ResultSet rs = jdbcUtil.doQuery(sql);

       LinkedList<User> list = new LinkedList<User>();

       try {

           while (rs.next()) {

              User user = new User();

              int i = 0;

              /*

        * 0 username 1 pwd_encrypt 2 sex 3 ID_number 4 phone 5 email 6

               * home_address 7 info_status

               */

              for (i = 0; i < s.length; i++) {

                  if (s[i].equalsIgnoreCase("username")) {

                     user.setUsername(rs.getString("username"));

                     i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("pwd_encrypt")) {

                     user.setPwd_encrypt(rs.getString("pwd_encrypt"));

                     i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("sex")) {

                     user.setSex(rs.getString("sex"));

                     i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("ID_number")) {

                     user.setID_number(rs.getString("ID_number"));

                     i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("phone")) {

                     user.setPhone(rs.getString("phone"));

                     i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("email")) {

                     user.setEmail(rs.getString("email"));

                     i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("home_address")) {

                     user.setHome_address(rs.getString("home_address"));

                     i++;

                     if (i == s.length)

                         continue;

                  }

                  if (s[i].equalsIgnoreCase("info_status")) {

                  user.setInfo_status(rs.getString("info_status"));

                  }

              }

              list.add(user);

           }

       } catch (SQLException e) {

           e.printStackTrace();

       }

       return list;

    }CharacterEncoding.java

package filter;

import java.io.*;

import javax.servlet.*;

public class CharacterEncoding implements Filter {

    private FilterConfig config;

    String encoding = null;

    public CharacterEncoding() {

    }

    public void destroy() {

       config = null;

    }

    public void doFilter(ServletRequest request, ServletResponse response,

           FilterChain chain) throws IOException, ServletException {

       request.setCharacterEncoding(encoding);

       response.setCharacterEncoding(encoding);

       response.setContentType("text/html;charset="+encoding);

       chain.doFilter(request, response);

    }

    public void init(FilterConfig fConfig) throws ServletException {

       this.config = fConfig;

       String str = config.getInitParameter("encoding");

       if(str != null) {

           encoding = str;

       }

    }

}

Report_select.java

package servlet;

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import bean.User;

import dao.UserDao;

public class Report_select extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,

           HttpServletResponse response) throws ServletException, IOException {

       doPost(request, response);

    }

    protected void doPost(HttpServletRequest request,

           HttpServletResponse response) throws ServletException, IOException {

       String user[] = request.getParameterValues("user_op0");

       for(int i = 0;i < user.length;i ++){

           System.out.println(i+"   "+user[i]);

       }

       LinkedList<User> list=UserDao.getUser(user);

       request.setAttribute("user", user);

       request.setAttribute("list", list);

       request.getRequestDispatcher("report/report_make.jsp").forward(request, response);

    }

}

Js文件

jquery-1.11.2.js------------网络上自己下载(“百度”—>“jquery-1.11.2.js包”)

manager.js

function SelectAllBox() {

    $("input[type=checkbox]").prop("checked", "checked");

}

function UnSelectAllBox() {

    $("input[type=checkbox]").prop("checked", null);

}

function getSelect(id) {

    var arrayValue = id.split("_");

       if (arrayValue[0] == arrayValue[1]) {

              if ($("#" + id)[0].checked) {

           $("input[type=checkbox][id^=" + arrayValue[0] + "]").prop(

                  "checked", "checked");

                  } else {

           $("input[type=checkbox][id^=" + arrayValue[0] + "]").prop(

                  "checked", null);

       }

    }

       if (arrayValue[0] != arrayValue[1]) {

           var superid = arrayValue[0] + "_" + arrayValue[0];

           if ($("#" + id)[0].checked) {

           $("#" + superid).prop("checked", "checked");

       } else {

           var flag = false;        var $child = $("input[type=checkbox][id^=" + arrayValue[0]

                  + "]:not([id$=" + arrayValue[0] + "])")

           $child.each(function() {

          

              if (this.checked) {

                  flag = true;

                  return;

              }

           });

              if (!flag) {

              $("#" + superid).prop("checked", null);

           }

       }

    }

}

Xml文件

Web.Xml增加以下内容

<web-app。。。。

<filter>

    <filter-name>CharacterEncoding</filter-name>

    <filter-class>filter.CharacterEncoding</filter-class>

    <init-param>

      <param-name>encoding</param-name>

      <param-value>utf-8</param-value>

    </init-param>

  </filter>

  <filter-mapping>

    <filter-name>CharacterEncoding</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <servlet>

    <servlet-name>Report_select</servlet-name>

    <servlet-class>servlet.Report_select</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>Report_select</servlet-name>

    <url-pattern>/Report_select</url-pattern>

  </servlet-mapping>

</web-app>

SQL文档

Project.sql

SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (

  `uid` bigint(11) NOT NULL AUTO_INCREMENT,

  `username` varchar(20) NOT NULL,

  `userpwd` varchar(20) NOT NULL,

  `pwd_encrypt` varchar(28) DEFAULT NULL,

  `sex` varchar(4) DEFAULT NULL,

  `ID_number` varchar(19) DEFAULT NULL,

  `phone` varchar(11) DEFAULT NULL,

  `email` varchar(20) DEFAULT NULL,

  `home_address` varchar(40) DEFAULT NULL,

  `info_status` varchar(10) DEFAULT NULL,

  PRIMARY KEY (`uid`)

) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

java EE实现动态SQL的

标签:

原文地址:http://www.cnblogs.com/xiaocao1434/p/4385712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!