程序代码:
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呢?