java EE实现动态SQL的

结果图

情况一:

情况二:

预先软件准备

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;

时间: 2024-10-16 17:36:28

java EE实现动态SQL的的相关文章

9、SpringBoot+Mybatis整合------动态sql

开发工具:STS 前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 动态sql中的语法: where标签 if标签 trim标签 set标签 switch\when标签 foreach标签 一.搭建项目 1.创建用户表: 2.添加实体: 1 package com.xm.pojo; 2 /** 3 * 用户实体 4 * @author xm 5 * 6 */ 7 public class User { 8 9 private int id;

Java下拼接执行动态SQL语句(转)

在实际业务中经常需要拼接动态SQL来完成复杂数据计算,网上各类技术论坛都有讨论,比如下面这些问题: http://bbs.csdn.net/topics/390876591 http://bbs.csdn.net/topics/390981627 https://www.linkedin.com/groups/SQL-Query-Help-needed-137774.S.5948812806903119877?trk=groups_items_see_more-0-b-ttl http://bb

Java下拼接执行动态SQL语句

Java拼接动态SQL的一般做法有       1.使用动态语句 很多数据库都提供了处理动态SQL的语法,如Oracle的EXECUTE IMMEDIATE语句.MSSQL的EXEC和SP_EXECUTESQL.Mysql的预处理语句等.这些功能让我们在数据库端来处理动态查询提供了极大遍历,但这种方式只适用于相对简单地动态查询,复杂的情况经常会采用下面的方式. 2.使用存储过程 对于复杂的情况,一般会在存储过程中来拼接动态SQL.使用存储过程完成相对灵活,但编码复杂度过高,有时运行效率较低. 3

java mybatis学习之$和#区别,mapper代理接口,动态SQL,在日志中输出mybatis的sql语句

1.在mybatis中,$和#的区别: #{}:表示一个预处理参数,参数类型不定,是根据传入的参数类型来设定的.类似于JDBC中的? 特例使用,模糊查询:(针对oracle): and username like concat(concat('%',#{username}),'%') ${}:相当于是我们的JDBC里的字符串拼接.这里就相当于传入的就是一个字符串(不管传入什么样的数据类型,都是字符串) and username like '%${value}%' 2.$和#在mybatis中的优

[刘阳Java]_MyBatis_动态SQL标签用法_第7讲

1.MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. 2.MyBatis中用于实现动态SQL的元素主要有 if choose(when,otherwise) trim where set foreach 可以看出MyBatis的动态SQL的标签元素和接近JSP中的JSTL语法,下面我就分别详细的介绍一下 3.动态SQL中if的用法 <?xml version="1.0" encoding="UTF-8" ?&g

Java SSM框架之MyBatis3(八)MyBatis之动态SQL

前言: mybatis框架中最具特色的便是sql语句中的自定义,而动态sql的使用又使整个框架更加灵活. 创建User表 /*Table structure for table `user` */ DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `username` varchar(20

Java EE开发四大常用框架

我们对Java EE的框架有过很多介绍, 本文将对Java EE中常用的四个框架做一下系统的归纳,希望大家喜欢. Struts Struts是一个基于Sun Java EE平台的MVC框架,主要是采用Servlet和JSP技术来实现的. Struts框架可分为以下四个主要部分,其中三个就和MVC模式紧密相关: 1.模型 (Model),本质上来说在Struts中Model是一个Action类(这个会在后面详细讨论),开发者通过其实现商业逻辑,同时用户请求通过控制器(Controller)向Act

影响Java EE性能的十大问题

本文作者是一名有10多年经验的高级系统架构师,他的主要专业领域是Java EE.中间件和JVM技术.他在性能优化和提升方面也有很深刻的见解,下面他将和大家分享一下常见的10个影响Java EE性能问题. 1.缺乏正确的容量规划 容量规划是一个全面的和发展的过程标准,预测当前和未来的IT环境容量需求.制定合理的容量规划不仅会确保和跟踪当前IT生产能力和稳定性,同时也会确保新项目以最小的风险部署到现有的生产环境中.硬件.中间件.JVM.调整等在项目部署之前就应该准备好. 2.Java EE中间件环境

Java EE中常用的四个框架

Java EE中常用的四个框架     Struts     Struts是一个基于Sun Java EE平台的MVC框架,主要是采用Servlet和JSP技术来实现的.     Struts框架可分为以下四个主要部分,其中三个就和MVC模式紧密相关:     1.模型 (Model),本质上来说在Struts中Model是一个Action类(这个会在后面详细讨论),开发者通过其实现商业逻辑,同时用户请求通过控制器(Controller)向Action的转发过程是基于由struts-config