如何从页面输入SQL查询条件得到后台结果返回前台页面

拼搏现实的明天。 2021-12-14 14:11 542阅读 0赞

转自: https://blog.csdn.net/yucongyuqian/article/details/77451578

使用Jsp编写网页并实现数据库的增删改查: https://blog.csdn.net/qq_32539825/article/details/70657340

JSP SQL语句大全: https://blog.csdn.net/fengyee_zju/article/details/25118683

开发一个用来执行sql的jsp页面: https://blog.csdn.net/qq_34928194/article/details/83412187

网页上直接sql查询操作数据库,并在网页上展示列表数据的工具页面: https://blog.csdn.net/yule117737767/article/details/83039509

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yucongyuqian/article/details/77451578
一、页面:因为查询的表不固定,所以表格需要自助生成(这是利用了“jqGrid”)
注意:难点一、因为从数据库查询的表不同,所有前台页面遍历时的表头格式不固定
难点二、数据库连接源
若是不利用“jqGrid”也可以参考方法2.
方法1.



























<%—引入jqGerid—%>














方法2.动态生成表格,但是分页还需自己在写一下

参考网址:https://q.cnblogs.com/q/73869/

var table = “

“;
table += ““;
for (var i = 0; i < date.length; i++) {
table += ““;
}
table += ““;
for (var j = 0; j < department.length; j++) {
table += ““;
table += ““;
for (var n = 0; n < date.length; n++) {
table += ““;
}
}
table += ““;
table += “
“ + “” + ““ + date[i] + “
“ + department[j] + ““ + SeriesStr[0].data[n] + “
“;
$(“#Exceltable”).html(table);

效果:

二、controller
1)查询表头

@RequestMapping(value = “transactionFlow/informationQuery1”, method = RequestMethod.POST)

  1. public String informationQuery1(@ModelAttribute("form") @RequestParam (value = "sql") String sql, Model model) \{
  2. List<String> list3 = new LinkedList<>();
  3. String query1 = customerLedgersService.query(sql);
  4. Map map = new HashMap();
  5. if (query1 != null) \{
  6. String\[\] key = query1.split(";");
  7. for (int i = 0, len = key.length; i < len; i++) \{
  8. list3.add(key\[i\]);
  9. \}
  10. model.addAttribute("sql", sql);
  11. model.addAttribute("list3", list3);
  12. return "/complement/informationQuery";
  13. \} else \{
  14. model.addAttribute("sql", sql);
  15. return "/complement/informationQuery";
  16. \}
  17. \}

2)查询分页信息

@RequestMapping(value = “transactionFlow/informationQuery”, method = RequestMethod.POST)
@ResponseBody
public GridData informationQuery(@RequestParam(value = “sql”, required = false) String sql,
@RequestParam(value = “page”, required = false) Integer page,
@RequestParam(value = “rows”, required = false) Integer rows
) {
List list7 = new LinkedList<>();
String queryValue = null;

  1. String records1 = customerLedgersService.query1(sql);
  2. int records = Integer.parseInt(records1);
  3. GridData result = new GridData();
  4. String listTotal = customerLedgersService.query2(sql, page, rows);
  5. String query2 = null;
  6. List<String> list6 = new ArrayList<>();
  7. List<String> list5 = new ArrayList();
  8. if (listTotal != null) \{
  9. String\[\] keyq3 = listTotal.split("%");
  10. for (int i = 0, len = keyq3.length; i < len; i++) \{
  11. list6.add(keyq3\[i\]);
  12. \}
  13. for (int m = 0; m < list6.size(); m++) \{
  14. queryValue = list6.get(m);
  15. String\[\] value = queryValue.split(";");
  16. List<String> list = Arrays.asList(value);
  17. ListIterator<String> li = list.listIterator();
  18. while (li.hasNext()) \{
  19. Object obj = li.next();
  20. if (obj.equals("0E-8")) \{
  21. li.set("0.00000000");
  22. \}
  23. if (obj.equals("null")) \{
  24. li.set("");
  25. \}
  26. \}
  27. list7.add(list);
  28. \}
  29. \}
  30. long totaPage = records % rows == 0 ? records / rows : records / rows + 1;
  31. result.setPage(page);
  32. result.setRecords(records);
  33. result.setTotal((int) totaPage);
  34. if (list7 != null) \{
  35. result.getRows().addAll(list7);
  36. \}
  37. return result;
  38. \}

三、dao层(利用了JDBC原理)
需要考虑的是数据库连接问题

@Repository
public class QueryDaoImpl implements QueryDao {
private static Logger logger = LoggerFactory.getLogger(QueryDaoImpl.class);

  1. @Override
  2. public String selectSql(String sql) \{
  3. DataSource ds = null;
  4. Connection conn = null;
  5. PreparedStatement pstmt = null;
  6. ResultSet rs = null;
  7. StringBuffer sb = new StringBuffer();
  8. int index = 1;
  9. //1 获得连接
  10. try \{
  11. if(sql == null)\{
  12. return null;
  13. \}
  14. InitialContext initialContext = new InitialContext();
  15. ds = (DataSource) initialContext.lookup("jdbc/account-structure");
  16. if (ds != null) \{
  17. conn = ds.getConnection();
  18. \}
  19. if(!sql.contains(";"))\{
  20. sql=sql+";";
  21. \}
  22. if(!sql.contains("limit") && !sql.contains("LIMIT"))\{
  23. if(sql.contains(";"))\{
  24. sql = sql.replaceAll(";", "");
  25. \}
  26. sql = sql + " limit 0, 1;";
  27. \}
  28. pstmt = conn.prepareStatement(sql);
  29. rs = pstmt.executeQuery();
  30. List<LinkedHashMap<String, Object>> values = new ArrayList<>();
  31. ResultSetMetaData rsmd = rs.getMetaData();
  32. LinkedHashMap<String, Object> map = null;
  33. while (rs.next()) \{
  34. //把一条记录放入Map中
  35. map = new LinkedHashMap<String, Object>();
  36. for (int i = 0; i < rsmd.getColumnCount(); i++) \{
  37. String columnLabel = rsmd.getColumnLabel(i + 1);
  38. Object value = rs.getObject(i + 1);
  39. map.put(columnLabel, value);
  40. \}
  41. values.add(map);
  42. \}
  43. if (values.size() > 0) \{
  44. for (Map<String, Object> m : values) \{
  45. Set<String> set = m.keySet();
  46. for (String s : set) \{
  47. sb.append(s + ";");
  48. \}
  49. \}
  50. \}
  51. return sb.toString();
  52. \} catch (Exception e) \{
  53. e.printStackTrace();
  54. \} finally \{
  55. //6关闭资源

// JDBCUtils.close(conn, pstmt, rs);
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
try {
pstmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
rs.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
return null;
}

  1. @Override
  2. public String selectSql1(String sql) \{
  3. DataSource ds = null;
  4. Connection conn = null;
  5. PreparedStatement pstmt = null;
  6. ResultSet rs = null;
  7. StringBuffer sb = new StringBuffer();
  8. int index = 0;
  9. //1 获得连接
  10. try \{
  11. if(sql == null)\{
  12. return null;
  13. \}
  14. InitialContext initialContext = new InitialContext();
  15. ds = (DataSource) initialContext.lookup("jdbc/account-structure");
  16. if (ds != null) \{
  17. conn = ds.getConnection();
  18. \}
  19. if(!sql.contains(";"))\{
  20. sql=sql+";";
  21. \}
  22. if(!sql.contains("limit") && !sql.contains("LIMIT"))\{
  23. if(sql.contains(";"))\{
  24. sql = sql.replaceAll(";", "");
  25. \}
  26. sql = sql + " limit 0, 5000;";
  27. \}
  28. pstmt = conn.prepareStatement(sql);
  29. rs = pstmt.executeQuery();
  30. List<LinkedHashMap<String, Object>> values = new ArrayList<>();
  31. ResultSetMetaData rsmd = rs.getMetaData();
  32. LinkedHashMap<String, Object> map = null;
  33. while (rs.next()) \{
  34. //把一条记录放入Map中
  35. map = new LinkedHashMap<String, Object>();
  36. for (int i = 0; i < rsmd.getColumnCount(); i++) \{
  37. String columnLabel = rsmd.getColumnLabel(i + 1);
  38. Object value = rs.getObject(i + 1);
  39. map.put(columnLabel, value);
  40. \}
  41. values.add(map);
  42. \}
  43. if (values.size() > 0) \{
  44. for (Map<String, Object> m : values) \{
  45. Set<String> set = m.keySet();
  46. for (String s : set) \{
  47. sb.append(s + ";");
  48. \}
  49. sb.append("|"+index++);
  50. \}
  51. \}
  52. return String.valueOf(index);
  53. \} catch (Exception e) \{
  54. e.printStackTrace();
  55. \} finally \{
  56. //6关闭资源

// JDBCUtils.close(conn, pstmt, rs);
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
try {
pstmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
rs.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
return null;
}

  1. @Override
  2. public String selectSq2(String sql, Integer page, Integer rows) \{
  3. DataSource ds = null;
  4. Connection conn = null;
  5. PreparedStatement pstmt = null;
  6. ResultSet rs = null;
  7. StringBuffer sb = new StringBuffer();
  8. StringBuffer sb1 = new StringBuffer();
  9. int index = 1;
  10. //1 获得连接
  11. try \{
  12. InitialContext initialContext = new InitialContext();
  13. ds = (DataSource) initialContext.lookup("jdbc/account-structure");
  14. if (ds != null) \{
  15. conn = ds.getConnection();
  16. \}
  17. String sql1=null;
  18. StringBuffer sql2=null;
  19. if(!sql.contains(";"))\{
  20. sql=sql+";";
  21. \}
  22. sql1=sql.substring(0,sql.length()-1);
  23. sql2=sb1.append(sql1+" "+"limit"+" "+"?"+","+"?;");
  24. pstmt = conn.prepareStatement(sql2.toString());
  25. int startIndex = (page - 1) \* rows;
  26. pstmt.setInt(1,startIndex);
  27. pstmt.setInt(2,rows);
  28. rs = pstmt.executeQuery();
  29. List<LinkedHashMap<String, Object>> values = new ArrayList<>();
  30. ResultSetMetaData rsmd = rs.getMetaData();
  31. LinkedHashMap<String, Object> map = null;
  32. while (rs.next()) \{
  33. //把一条记录放入Map中
  34. map = new LinkedHashMap<String, Object>();
  35. for (int i = 0; i < rsmd.getColumnCount(); i++) \{
  36. String columnLabel = rsmd.getColumnLabel(i + 1);
  37. Object value = rs.getObject(i + 1);
  38. map.put(columnLabel, value);
  39. \}
  40. values.add(map);
  41. \}
  42. if (values.size() > 0) \{
  43. for (Map<String, Object> m : values) \{
  44. Set<String> set = m.keySet();
  45. for (String s : set) \{
  46. sb.append(m.get(s) + ";");
  47. \}
  48. sb.append("%");
  49. \}
  50. \}
  51. return sb.toString();
  52. \} catch (Exception e) \{
  53. e.printStackTrace();
  54. \} finally \{
  55. try \{
  56. conn.close();
  57. \} catch (SQLException e) \{
  58. e.printStackTrace();
  59. try \{
  60. pstmt.close();
  61. \} catch (SQLException e1) \{
  62. e1.printStackTrace();
  63. \}
  64. try \{
  65. rs.close();
  66. \} catch (SQLException e1) \{
  67. e1.printStackTrace();
  68. \}
  69. \}
  70. \}
  71. return null;
  72. \}

}

查询页面:

输入SQL语句:SELECT * from ledgers_entity where customer_business_id=’180050602775019520’;
查询结果:

-——————————
作者:yucongyuqian
来源:CSDN
原文:https://blog.csdn.net/yucongyuqian/article/details/77451578
版权声明:本文为博主原创文章,转载请附上博文链接!

发表评论

表情:
评论列表 (有 0 条评论,542人围观)

还没有评论,来说两句吧...

相关阅读