注册 登录
编程论坛 J2EE论坛

哪位高手帮忙给看一下,我的查询功能一直实现不了 ????谢谢

ggskyle 发布于 2017-06-24 23:34, 10414 次点击
这是查询的主界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户信息管理</title>
<script type="text/javascript">
var url;
//打开新增患者对话框
function openPatientAddDialog(){
    //在勾选情况下点击新增要先清除数据
    resetValue();
    $("#dlg").dialog("open").dialog("setTitle","添加患者信息");
    url="patient!save";//为url赋值
}
//重置对话框内数据
function resetValue(){
    $("#patientName").val("");
    $("#zhuangtai").combobox("setValue","");
    $("#birthday").datebox("setValue","");
    $("#chuli").datebox("setValue","");
    $("#patientDesc").val("");
    $("#didian").combobox("setValue","");
    $("#jielun").combobox("setValue","");
    $("#tel").val("");
    $("#money").val("");
    $("#baoxiao").val("");

}
//关闭对话框
function closePatientDialog(){
    $("#dlg").dialog("close");
    resetValue();
}
//提交新增患者数据
function savePatient(){
    $("#fm").form("submit",{
        url:url,
        onSubmit:function(){
            if($('#zhuangtai').combobox("getValue")==""){
                $.messager.alert("系统提示","请选择手续状态");
                return false;
            }
            if($('#didian').combobox("getValue")==""){
                $.messager.alert("系统提示","请选择调查地点");
                return false;
            }
            return $(this).form("validate");
        },
        success:function(result){
            if(result.errorMsg){
                $.messager.alert("系统提示",result.errorMsg);
                return;
            }else{
                $.messager.alert("系统提示","保存成功");
                resetValue();
                $("#dlg").dialog("close");
                $("#dg").datagrid("reload");
            }
        }
    });
}
//删除选中的患者数据
function deletePatient(){
    //获得选中数据对象
    var selectedRows=$("#dg").datagrid('getSelections');
    if(selectedRows.length==0){
        $.messager.alert("系统提示","请选择要删除的数据!");
        return;
    }
    var strIds=[];//要删除的序号组合
    for(var i=0;i<selectedRows.length;i++){
        strIds.push(selectedRows[i].patientId);
    }
    var ids=strIds.join(",");
    $.messager.confirm("系统提示","您确认要删掉这<font color=red>"+selectedRows.length+"</font>条数据吗?",function(r){
        if(r){
            //ajax提交 delIds
            $.post("patient!delete",{delIds:ids},function(result){
                if(result.success){
                    $.messager.alert("系统提示","您已成功删除<font color=red>"+result.delNums+"</font>条数据!");
                    $("#dg").datagrid("reload");
                }else{
                    $.messager.alert('系统提示',result.errorMsg);
                }
            },"json");
        }
    });
}
//修改患者资料
function openPatientModifyDialog(){
    var selectedRows=$("#dg").datagrid('getSelections');
    if(selectedRows.length!=1){
        $.messager.alert("系统提示","请选择一条要编辑的数据!");
        return;
    }
    var row=selectedRows[0];
    $("#dlg").dialog("open").dialog("setTitle","编辑患者资料");
    $("#patientName").val(row.patientName);
    $("#zhuangtai").combobox("setValue",row.zhuangtai);
    $("#birthday").datebox("setValue",row.birthday);
    $("#chuli").datebox("setValue",row.chuli);
    $("#patientDesc").val(row.patientDesc);
    $("#didian").combobox("setValue",row.didian);
    $("#jielun").combobox("setValue",row.jielun);
    $("#tel").val(row.tel);
    $("#money").val(row.money);
    $("#baoxiao").val(row.baoxiao);
    url="patient!save?patientId="+row.patientId;
}
//查询符合条件的用户
function searchPatient(){
    $('#dg').datagrid('load',{
        s_patientName:$('#s_patientName').val(""),
        zhuang:$('#zhuang').combobox("getValue"),
        jie:$('#jie').combobox("getValue"),
    });
}
//倒出excel数据
function exportPatient(){
    $('#export').form("submit",{
        url:"patient!exportPatient"
    })
    //window.location.href="employee!ExportEmployee";
}
</script>
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
</head>
<body style="margin:5px">
<table id="dg" title="患者信息" class="easyui-datagrid" fitColumns="true"
     pagination="true" rownumbers="true" url="patient" fit="true" toolbar="#tb">
        <thead>
            <tr>
                <th field="cb" checkbox="true"></th>
                <th field="patientId" width="40"  align="center">编号</th>
                <th field="patientName" width="80"  align="center">患者名称</th>
                <th field="zhuangtai" width="80"  align="center">手续状态</th>
                <th field="didian" width="80"  align="center">调查地点</th>
                <th field="jielun" width="80" align="center">调查结论 </th>
                <th field="birthday" width="80" align="center">调查日期</th>
                <th field="chuli" width="80" align="center">处理日期</th>
                <th field="tel" width="80" align="center">联系电话</th>
                <th field="patientDesc" width="250" align="center">家庭住址</th>
                <th field="money" width="80" align="center">花费金额</th>
                <th field="baoxiao" width="80" align="center">报销金额</th>
               
               
            </tr>
        </thead>
    </table>
    <div id="tb">
        <div>
            <a href="javascript:openPatientAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
            <a href="javascript:openPatientModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
            <a href="javascript:deletePatient()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
            <a href="javascript:exportPatient()" class="easyui-linkbutton" iconCls="icon-export" plain="true">导出Execl</a>
        </div>
        <div>
            <form id="export" method="post">
            &nbsp;姓名:&nbsp;<input type="text" name="s_patientName" id="s_patientName" size="10"/>
            &nbsp;手续状态:&nbsp;<select  id="zhuang" name="zhuang" editable="false" panelHeight="auto" style="width: 155px">
                        <option value="">请选择...</option>
                        <option value="未调">未调</option>
                        <option value="已调">已调</option>
                    </select>
            &nbsp;调查结论:&nbsp;<select  id="jie" name="jie" editable="false" panelHeight="auto" style="width: 155px">
                        <option value="">请选择...</option>
                        <option value="正常">正常</option>
                        <option value="侧调">侧调</option>
                        <option value="拒赔">拒赔</option>
                    </select>
            <a href="javascript:searchPatient()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
            </form>
        </div>
    </div>
        
    <div id="dlg" class="easyui-dialog" style="width: 570px;height: 340px;padding: 10px 20px"
        closed="true" buttons="#dlg-buttons">
        <form id="fm" method="post" enctype="multipart/form-data">
            <table cellspacing="5px;">
                <tr>        
                    <td>患者姓名:</td>
                    <td><input type="text" name="patient.patientName" id="patientName" class="easyui-validatebox" required/></td>
                    <td>手续状态:</td>
                    <td><select class="easyui-combobox" id="zhuangtai" name="patient.zhuangtai" editable="false" panelHeight="auto" style="width: 155px">
                        <option value="">请选择...</option>
                        <option value="未调">未调</option>
                        <option value="已调">已调</option>
                    </select></td>
                </tr>
                <tr>
                    <td>调查地点:</td>
                    <td><select class="easyui-combobox" id="didian" name="patient.didian" editable="false" panelHeight="auto" style="width: 155px">
                        <option value="">请选择...</option>
                        <option value="医院">医院</option>
                        <option value="实地">实地</option>
                        <option value="公司">公司</option>
                        <option value="无">无</option>
                    </select></td>
                    <td>调查结论:</td>
                    <td><select class="easyui-combobox" id="jielun" name="patient.jielun" editable="false" panelHeight="auto" style="width: 155px">
                        <option value="">请选择...</option>
                        <option value="正常">正常</option>
                        <option value="侧调">侧调</option>
                        <option value="拒赔">拒赔</option>
                        <option value="无">无</option>
                    </select></td>
                </tr>
                <tr>
                <td>调查日期:</td>
                    <td><input class="easyui-datebox" name="patient.birthday" id="birthday" required editable="false" /></td>
                    <td>处理日期:</td>
                    <td><input class="easyui-datebox" name="patient.chuli" id="chuli" required editable="false" /></td>
            
               
                </tr>
                <tr>        
                    <td>花费金额:</td>
                    <td><input type="text" name="patient.money" id="money" class="easyui-validatebox" required/></td>
                    <td>报销金额:</td>
                    <td><input type="text" name="patient.baoxiao" id="baoxiao"  required/></td>
                    </tr>
                    <tr>        
                    <td>联系电话:</td>
                    <td><input type="text" name="patient.tel" id="tel" class="easyui-validatebox" required/></td>
                    </tr>
                <tr>
                    <td valign="top">家庭住址:</td>
                    <td colspan="4"><textarea rows="7" cols="46" name="patient.patientDesc" id="patientDesc"></textarea></td>
                </tr>
            </table>
        </form>
    </div>
   
    <div id="dlg-buttons">
        <a href="javascript:savePatient()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
        <a href="javascript:closePatientDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
    </div>
</body>
</html>

Dao.java:
package

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import
import
import
import

public class PatientDao {
    /**
     * 得到patient查询结果
     * @param con
     * @param pageBean
     * @param patient
     * @return
     * @throws Exception
     */
    public ResultSet patientList(Connection con,PageBean pageBean,Patient patient,String zhuang,String jie)throws Exception{
        StringBuffer sb=new StringBuffer("select * from t_patient");
        
        if(StringUtil.isNotEmpty(patient.getPatientName())){
            sb.append(" and patientName like '%"+patient.getPatientName()+"%'");
        }
        
        //分页
        if(pageBean!=null){
            sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());
        }
        PreparedStatement pstmt=con.prepareStatement(sb.toString());
        return pstmt.executeQuery();
    }
    /**
     * 获取数据总条数
     * @param con
     * @param grade
     * @return
     * @throws Exception
     */
    public int patientCount(Connection con,Patient patient,String zhuang,String jie)throws Exception{
        StringBuffer sb=new StringBuffer("select count(*) as total from t_patient");
        
        if(StringUtil.isNotEmpty(patient.getPatientName())){
            sb.append(" and patientName like '%"+patient.getPatientName()+"%'");
        }
        
        PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
        ResultSet rs=pstmt.executeQuery();
        if(rs.next()){
            return rs.getInt("total");
        }else{
            return 0;
        }
    }
    /**
     * 数据库插入数据
     * @param con
     * @param patient
     * @return
     * @throws Exception
     */
    public int patientAdd(Connection con,Patient patient)throws Exception{
        String sql="insert into t_patient values(null,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1, patient.getPatientName());
        pstmt.setString(2, patient.getZhuangtai());
        pstmt.setString(3, DateUtil.formatDate(patient.getChuli(), "yyyy-MM-dd"));
        pstmt.setString(4, DateUtil.formatDate(patient.getBirthday(), "yyyy-MM-dd"));
        pstmt.setString(5, patient.getDidian());
        pstmt.setString(6, patient.getJielun());
        pstmt.setString(7, patient.getTel());
        pstmt.setString(8, patient.getPatientDesc());
        pstmt.setString(9, patient.getMoney());
        pstmt.setString(10, patient.getBaoxiao());
        return pstmt.executeUpdate();
    }
   
   
    /**
     * 删除数据
     * @param con
     * @param delIds
     * @return
     * @throws Exception
     */
    public int patientDelete(Connection con,String delIds)throws Exception{
        String sql="delete from t_patient where patientId in("+delIds+")";
        PreparedStatement pstmt=con.prepareStatement(sql);
        return pstmt.executeUpdate();
    }
    /**
     * 患者资料更改
     * @param con
     * @param patient
     * @return
     * @throws Exception
     */
    public int patientModify(Connection con,Patient patient)throws Exception{
        String sql="update t_patient set patientName=?,zhuangtai=?,birthday=?,chuli=?,didian=?,jielun=?,tel=?,patientDesc=?,money=?,baoxiao=? where patientId=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setInt(11, patient.getPatientId());
        pstmt.setString(1, patient.getPatientName());
        pstmt.setString(2, patient.getZhuangtai());
        pstmt.setString(3, DateUtil.formatDate(patient.getChuli(), "yyyy-MM-dd"));
        pstmt.setString(4, DateUtil.formatDate(patient.getBirthday(), "yyyy-MM-dd"));
        pstmt.setString(5, patient.getDidian());
        pstmt.setString(6, patient.getJielun());
        pstmt.setString(7, patient.getTel());
        pstmt.setString(8, patient.getPatientDesc());
        pstmt.setString(9, patient.getMoney());
        pstmt.setString(10, patient.getBaoxiao());
        
        return pstmt.executeUpdate();
    }
}


Avtion.java:
package


import
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import
import
import
import
import
import
import
import
import
import

import com.opensymphony.xwork2.ActionSupport;

public class PatientAction extends ActionSupport implements ServletRequestAware{
   
    private Patient patient;//patient对象
    private String page;//分页数据
    private String rows;//分页数据
    private String delIds;//批量删除数据的序列号
    private String patientId;//修改患者资料传递的患者编号
    //查询条件的变量
    private String s_patientName;
    private String zhuang;
    private String jie;
   
   
    public String getPage() {
        return page;
    }
    public void setPage(String page) {
        this.page = page;
    }
   
    public String getRows() {
        return rows;
    }
    public void setRows(String rows) {
        this.rows = rows;
    }
   
    public Patient getPatient() {
        return patient;
    }
    public void setPatient(Patient patient) {
        this.patient = patient;
    }   
    public String getDelIds() {
        return delIds;
    }
    public void setDelIds(String delIds) {
        this.delIds = delIds;
    }
    public String getPatientId() {
        return patientId;
    }
    public void setPatientId(String patientId) {
        this.patientId = patientId;
    }
    public String getS_patientName() {
        return s_patientName;
    }
    public void setS_patientName(String s_patientName) {
        this.s_patientName = s_patientName;
    }
    public String getZhuang() {
        return zhuang;
    }
    public void setZhuang(String zhuang) {
        this.zhuang = zhuang;
    }
    public String getJie() {
        return jie;
    }
    public void setJie(String jie) {
        this.jie = jie;
    }
   



    DbUtil dbUtil=new DbUtil();
    UserDao userDao=new UserDao();
    PatientDao patientDao=new PatientDao();
    HttpServletRequest request;
    //当前用户
    User currentUser;
   
   
    @Override
    public String execute() throws Exception {
        // 获取Session
        HttpSession session=request.getSession();   
        //链接connection
        Connection con=null;
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));//分页pageBean
        if(patient==null){
            patient=new Patient();
        }
        
        if(s_patientName!=null){
            patient.setPatientName(s_patientName);
            patient.setZhuangtai(zhuang);
            patient.setJielun(jie);
        }
        try{
            con=dbUtil.getCon();
            JSONObject result=new JSONObject();
            JSONArray jsonArray=JsonUtil.formatRsToJsonArray(patientDao.patientList(con, pageBean,patient,zhuang,jie));
            int total=patientDao.patientCount(con,patient,zhuang,jie);
            result.put("rows", jsonArray);
            result.put("total", total);
            ResponseUtil.write(ServletActionContext.getResponse(), result);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
    //保存患者数据(新增和修改)
    public String save()throws Exception{
        //判断patientId是否为空
        if(StringUtil.isNotEmpty(patientId)){
            patient.setPatientId(Integer.parseInt(patientId));
        }
        Connection con=null;
        try{
            con=dbUtil.getCon();
            int saveNums=0;
            JSONObject result=new JSONObject();
            if(StringUtil.isNotEmpty(patientId)){
            //不为空,修改患者信息
                saveNums=patientDao.patientModify(con, patient);
            }else{
            //patientId为空,新增患者
                saveNums=patientDao.patientAdd(con, patient);
            }
            if(saveNums>0){
                result.put("success", "true");
            }else{
                result.put("success", "true");//业务逻辑,需要返回success,但返回的是错误message
                result.put("errorMsg", "保存失败");
            }
            ResponseUtil.write(ServletActionContext.getResponse(), result);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
    //删除数据
    public String delete()throws Exception{
        Connection con=null;
        try{
            con=dbUtil.getCon();
            JSONObject result=new JSONObject();
            int delNums=patientDao.patientDelete(con, delIds);
            if(delNums>0){
                result.put("success", "true");
                result.put("delNums", delNums);
            }else{
                result.put("errorMsg", "删除失败");
            }
            ResponseUtil.write(ServletActionContext.getResponse(), result);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
   
    //下拉框赋值 如果是管理员可以查询所有用户的患者,如果是用户只能显示当前用户患者
   
    //输出excel表格(xls)
    public void exportPatient() throws Exception{
        // 创建Excel的工作书册 Workbook,对应到一个excel文档
        HSSFWorkbook wb = new HSSFWorkbook();

        // 创建Excel的工作sheet,对应到一个excel文档的tab
        HSSFSheet sheet = wb.createSheet("sheet1");

        // 设置excel每列宽度
        sheet.setColumnWidth(0, 4000);
        sheet.setColumnWidth(1, 3500);

        // 创建字体样式
        HSSFFont font = wb.createFont();
        font.setFontName("Verdana");
        font.setBoldweight((short) 100);
        font.setFontHeight((short) 300);
        font.setColor(HSSFColor.BLUE.index);

        // 创建单元格样式
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        // 设置边框
        style.setBottomBorderColor(HSSFColor.RED.index);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);

        style.setFont(font);// 设置字体
        style.setWrapText(true);// 自动换行

        // 创建Excel的sheet的一行
        HSSFRow row = sheet.createRow(0);
        row.setHeight((short) 500);// 设定行的高度
        HSSFCell cell=null;
        // 创建一个Excel的单元格
        String[] cellTitle = {"编号", "患者姓名", "手续状态","调查地点", "调查结论", "调查日期","处理日期","家庭住址"};   
        for (int i = 0; i < cellTitle.length; i++) {
            cell = row.createCell(i);
            // 给Excel的单元格设置样式和赋值
            cell.setCellStyle(style);
            cell.setCellValue(cellTitle[i]);
        }
        // 获取Session
        HttpSession session=request.getSession();   
        //链接connection
        Connection con=null;
        if(patient==null){
            patient=new Patient();
        }

        
        //将查询条件赋值给patient对象
        if(s_patientName!=null){
            patient.setPatientName(s_patientName);
            patient.setZhuangtai(zhuang);
            patient.setJielun(jie);
        }
        ResultSet rs=null;
        try{
            con=dbUtil.getCon();
            rs=patientDao.patientList(con, null,patient,zhuang,jie);
            int rowIndex=1;
            while(rs.next()){
                row = sheet.createRow(rowIndex++);
                cell = row.createCell(0);
                cell.setCellValue(rs.getInt("patientId"));
                cell = row.createCell(1);
                cell.setCellValue(rs.getString("patientName"));
                cell = row.createCell(2);
                cell.setCellValue(rs.getString("zhuangtai"));
                cell = row.createCell(3);
                cell.setCellValue(rs.getString("didian"));
                cell = row.createCell(4);
                cell.setCellValue(rs.getString("jielun"));
                cell = row.createCell(5);
                cell.setCellValue(rs.getString("birthday"));
                cell = row.createCell(6);
                cell.setCellValue(rs.getString("chuli"));
                cell = row.createCell(7);
                cell.setCellValue(rs.getString("patientDesc"));
               
            }
            
        String exportFileName = "patient.xls";
        
        ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + new String((exportFileName).getBytes(), "ISO8859-1"));//设定输出文件头
        ServletActionContext.getResponse().setContentType("application/vnd.ms-excel;charset=UTF-8");// 定义输出类型
        
        
        OutputStream out = ServletActionContext.getResponse().getOutputStream();
        wb.write(out);
        out.flush();
        out.close();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }
        
        
    @Override
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request=request;
   查询的功能就是实现不了   也不知道哪的问题    各位大神帮帮忙看看  谢谢

[此贴子已经被作者于2017-6-24 23:35编辑过]

13 回复
#2
林月儿2017-06-25 09:52
程序代码:
/**
     * 得到patient查询结果
     *
@param con
     *
@param pageBean
     *
@param patient
     *
@return
     *
@throws Exception
     
*/
    public ResultSet patientList(Connection con,PageBean pageBean,Patient patient,String zhuang,String jie)throws Exception{
        StringBuffer sb=new StringBuffer("select * from t_patient");
        
        if(StringUtil.isNotEmpty(patient.getPatientName())){
            sb.append(" and patientName like '%"+patient.getPatientName()+"%'");
        }
        
        //分页
        if(pageBean!=null){
            sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());
        }
        PreparedStatement pstmt=con.prepareStatement(sb.toString());
        return pstmt.executeQuery();
    }

是这个查询吗?
#3
ggskyle2017-06-25 19:12
回复 2楼 林月儿
对呀!不知道怎么回事!
#4
林月儿2017-06-25 19:16
回复 3楼 ggskyle
先试试sql语句对不对 ,再debug调试看看调用sql语句的参数是否正常
#5
ggskyle2017-06-25 23:03
回复 4楼 林月儿
sql语句我试过没有问题   因是自学debug我不太会用  我能给您发过去整个项目,您帮忙给看看行么  

[此贴子已经被作者于2017-6-25 23:05编辑过]

#6
林月儿2017-06-25 23:14
回复 5楼 ggskyle
可以吧,不会再问问前辈
#7
ggskyle2017-06-25 23:32
回复 6楼 林月儿
只有本站会员才能查看附件,请 登录
谢谢了   我就用了其中的patientInfoManage的板块  其他的没有用

[此贴子已经被作者于2017-6-25 23:35编辑过]

#8
林月儿2017-06-26 09:52
回复 7楼 ggskyle
初步测了下dao层的方法,没什么问题。试着传null值进去,报空指针异常。这个可能性排查清楚再说吧。

提些建议
变量名命名还是要有意义的好
异常处的处理最好打log
dao那边以及其他地方(比如excel等文件处理)共通程度还可以再高,而且上层接口也没写,建议改成接口定义加下层实现的常规写法。
数据解析处理json用的jar,建议换成gson.jar或版本更高
sql语句部分,关键字大小有交替出现,统一改为大写比较方便解析。

上面的建议偏后台,对后面的编码效率提高有帮助。
异常处理很重要,错误信息有迹可循

前台呢,可以效仿后台进行分层,按对象或功能分层。
ui的文件命名正式版本要调整了
js的操作可以写到js文件里,css样式定义到.css文件里。让jsp文件代码精简逻辑清晰
相应的请求,可以的话,struts.xml那边使用通配符减少action数量


[此贴子已经被作者于2017-6-26 09:55编辑过]

#9
林月儿2017-06-26 19:53
先不管我提的建议,先实现功能吧。

[此贴子已经被作者于2017-6-26 20:38编辑过]

#10
ggskyle2017-06-26 22:39
呵呵  谢谢你的建议我会注意的  谢谢
关键是功能一直实现不了  呵呵  我的感觉是Dao层接受的赋值是空  
#11
林月儿2017-06-26 22:50
回复 10楼 ggskyle
这不是感觉,我测试过,当传值正常时,没问题,传入对象为null,以及分页的条件为0你没有处理
public int getStart() {
return (page-1)*rows;
}
如果page=0,rows>0返回值为负值,你也放行了,后面的操作肯定会报错的、
这里先加个if(page<1)page=1;

如果不会debug,就用log,我看你工程没有用到log。。。
那先写个工具类,功能是写入信息,在catch部分将catch的信息写到文件里,这也是log的思想

你这个项目,功能,对象种类可能不大,分布式也许不需要。但合理的共通还是需要的,对你后续的功能扩展是有帮助的。
#12
林月儿2017-06-27 11:16
程序代码:
jdbc:添加为例(当然用框架更好)修改建议
public void add(Object obj, String tbName){
    Class.forName(jdbcName);
    Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
    String sql1 = "insert into @1(";
    sql1 = sql1.replace("@1", tbName);
    String sql2 = ") values(";
    Field[] fs = obj.getClass().getDeclaredFields();
    for(int i = 0; i < fs.length; i++) {
        sql1 += fs[i].getName() + ",";            
        fs[i].setAccessible(true);
        Object attr = fs[i].get(obj);
        if(attr instanceof String){
            sql2 += "'"+attr+"',";
        }else{               
            sql2 += attr+",";
        }
    }
    sql1 = sql1.substring(0, sql1.length()-1);
    sql2 = sql2.substring(0, sql2.length()-1);
    String sql = sql1+sql2+")";
    System.out.println(sql);
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.executeUpdate();
    ps.close();
    conn.close();
}
这里简单说明,往数据库添加一条记录时,只需要传入对象并指定表名add(obj,tbName);
由于是样例,问题有obj为null,tbName不存在,属性值类型判断有限,连接数据库操作和关闭连接抽离出一个新方法等需完善之处
可以定所有表名为常量,obj进入判空等加强

jdbc:查询为例(全部)修改建议
public <T> void select(T t) throws Exception{
    List<T> as = new ArrayList<T>();
    Class.forName(jdbcName);
    Connection conn = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
    PreparedStatement ps = conn.prepareStatement("select * from @1".replace("@1", tbName));
    ResultSet reSet = ps.executeQuery();
    while(reSet.next()){
        T a = (T) t.getClass().newInstance();
        Field[] fs = AA.class.getDeclaredFields();
        for(Field f:fs){
            f.setAccessible(true);
            f.set(a, reSet.getObject(f.getName()));
        }
        as.add(a);
    }
    reSet.close();
    ps.close();
    conn.close();
    for(T a:as){
        System.out.println(a);
    }
}

public void exportPatient(String[] cellTitle,String exportFileName) throws Exception{
    // 创建Excel的工作书册 Workbook,对应到一个excel文档
    HSSFWorkbook wb = new HSSFWorkbook();

    // 创建Excel的工作sheet,对应到一个excel文档的tab
    HSSFSheet sheet = wb.createSheet("sheet1");

    // 设置excel每列宽度
    sheet.setColumnWidth(0, 4000);
    sheet.setColumnWidth(1, 3500);

    // 创建字体样式
    HSSFFont font = wb.createFont();
    font.setFontName("Verdana");
    font.setBoldweight((short) 100);
    font.setFontHeight((short) 300);
    font.setColor(HSSFColor.BLUE.index);

    // 创建单元格样式
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    // 设置边框
    style.setBottomBorderColor(HSSFColor.RED.index);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);

    style.setFont(font);// 设置字体
    style.setWrapText(true);// 自动换行

   
// 创建Excel的sheet的一行
    HSSFRow row = sheet.createRow(0);
    row.setHeight((short) 500);// 设定行的高度
    HSSFCell cell=null;
    // 创建一个Excel的单元格                                           具体业务逻辑抽离,通过参数传入
   
//String[] cellTitle = {"编号", "患者姓名", "手续状态","调查地点", "调查结论", "调查日期","处理日期","家庭住址"};   
    for (int i = 0; i < cellTitle.length; i++) {
        cell = row.createCell(i);
        // 给Excel的单元格设置样式和赋值
        cell.setCellStyle(style);
        cell.setCellValue(cellTitle[i]);
    }
    // 获取Session
    HttpSession session=request.getSession();   
    //链接connection
    Connection con=null;
    if(patient==null){
        patient=new Patient();
    }

   
    //将查询条件赋值给patient对象
    if(s_patientName!=null){
        patient.setPatientName(s_patientName);
        patient.setZhuangtai(zhuang);
        patient.setJielun(jie);
    }
    ResultSet rs=null;
    try{
        con=dbUtil.getCon();
        rs=patientDao.patientList(con, null,patient,zhuang,jie);
        int rowIndex=1;
        while(rs.next()){
            row = sheet.createRow(rowIndex++);
            cell = row.createCell(0);
            cell.setCellValue(rs.getInt("patientId"));//可改为数组参数据传入
            cell = row.createCell(1);
            cell.setCellValue(rs.getString("patientName"));
            cell = row.createCell(2);
            cell.setCellValue(rs.getString("zhuangtai"));
            cell = row.createCell(3);
            cell.setCellValue(rs.getString("didian"));
            cell = row.createCell(4);
            cell.setCellValue(rs.getString("jielun"));
            cell = row.createCell(5);
            cell.setCellValue(rs.getString("birthday"));
            cell = row.createCell(6);
            cell.setCellValue(rs.getString("chuli"));
            cell = row.createCell(7);
            cell.setCellValue(rs.getString("patientDesc"));
            
        }
        
    //String exportFileName = "patient.xls";
   
    ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + new String((exportFileName).getBytes(), "ISO8859-1"));//设定输出文件头
    ServletActionContext.getResponse().setContentType("application/vnd.ms-excel;charset=UTF-8");// 定义输出类型
   
   
    OutputStream out = ServletActionContext.getResponse().getOutputStream();
    wb.write(out);
    out.flush();
    out.close();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        try {
            dbUtil.closeCon(con);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

struts.xml
<package name="PatientInfo" namespace="/" extends="struts-default" >
        <action name="login" class=" name="error">/index.jsp</result>
            <result name="user" type="redirect">/main.jsp</result>
            <result name="admin" type="redirect">/main.jsp</result>
            <result name="logout" type="redirect">/index.jsp</result>
        </action>
        <!--
             前台请求可写成/Patient_add,/Patient_delete,/Patient_update,
            相应Action中方法得以add,delete,update...命名
            这样写的好处就是,方法命名有规律,而且配置的action数量明显减少
        -->
        <action name="*_*" class="{1}Action" method="{2}">
            <result>/{1}_index.jsp</result><!-- 可自行决定 -->
        </action>
</package>

可能这样处理有点单调,不过简单直接


不特别详细了解你的需求,只能大概提些建议
有点不理解,既然struts都用了,为啥不加个hibernate或者mybatis呢?
#13
wlrjgzs2017-06-30 18:31
楼主把整个代码发给我给你看一下
#14
老道游天下2019-04-04 10:43
提示: 作者被禁止或删除 内容自动屏蔽,只有管理员可见
1