dba01c65e758378b83044bba97e7964804862ed1.svn-base 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package test.main.DB;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.StringTokenizer;
  7. public class TestConection{
  8. /**
  9. * 获取服务参数
  10. *
  11. * @author wzj
  12. * @param key
  13. * --参数名,params--服务得到的参数集合字符串
  14. * com.sinosoft.lz.system.Utils.getParameter(key, params)
  15. */
  16. public static String getParameter(String key, String params) {
  17. String str = "";
  18. String g = key + "=";
  19. String d = "";
  20. StringTokenizer commaToker = new StringTokenizer(params, "&");
  21. while (commaToker.hasMoreTokens()) {
  22. d = commaToker.nextToken();
  23. if (d.indexOf(g, 0) == 0) {
  24. if (g.equals(d)) {
  25. // str = null;
  26. } else {
  27. str += d.substring(g.length(), d.length()) + ",";
  28. }
  29. // break;
  30. }
  31. }
  32. String result = "";
  33. if (str.equals("")) {
  34. result = null;
  35. } else {
  36. result = str.substring(0, str.length() - 1);
  37. }
  38. return result;
  39. }
  40. /**
  41. * 一次只从数据库中查询最大maxCount条记录
  42. *
  43. * @param sql
  44. * 传入的sql语句
  45. * @param startNo
  46. * 从哪一条记录开始
  47. * @param maxCount
  48. * 总共取多少条记录
  49. */
  50. @SuppressWarnings("null")
  51. public void getData(String sql, int startNo, int maxCount) {
  52. Connection conn = null;
  53. try {
  54. // conn.prepareStatement(sql,游标类型,能否更新记录);
  55. // 游标类型:
  56. // ResultSet.TYPE_FORWORD_ONLY:只进游标
  57. // ResultSet.TYPE_SCROLL_INSENSITIVE:可滚动。但是不受其他用户对数据库更改的影响。
  58. // ResultSet.TYPE_SCROLL_SENSITIVE:可滚动。当其他用户更改数据库时这个记录也会改变。
  59. // 能否更新记录:
  60. // ResultSet.CONCUR_READ_ONLY,只读
  61. // ResultSet.CONCUR_UPDATABLE,可更新
  62. PreparedStatement pstat = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
  63. ResultSet.CONCUR_READ_ONLY);
  64. // 最大查询到第几条记录
  65. pstat.setMaxRows(startNo + maxCount - 1);
  66. ResultSet rs = pstat.executeQuery();
  67. // 将游标移动到第一条记录
  68. rs.first();
  69. // 游标移动到要输出的第一条记录
  70. rs.relative(startNo - 2);
  71. while (rs.next())
  72. System.out.println(rs.getInt(1));
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. /**
  78. * 从数据库中查询所有记录,然后通过游标来获取所需maxCount条记录
  79. *
  80. * @param sql
  81. * 传入的sql语句
  82. * @param startNo
  83. * 从哪一条记录开始
  84. * @param maxCount
  85. * 总共取多少条记录
  86. */
  87. @SuppressWarnings("null")
  88. public void getDataFromAll(String sql, int startNo, int maxCount) {
  89. Connection conn = null;
  90. try {
  91. PreparedStatement pstat = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
  92. ResultSet.CONCUR_READ_ONLY);
  93. ResultSet rs = pstat.executeQuery();
  94. rs.first();
  95. rs.relative(startNo - 1);
  96. int i = startNo - 1;
  97. while (i < startNo + maxCount - 1 && !rs.isAfterLast()) {
  98. System.out.println(rs.getInt(1));
  99. i++;
  100. rs.next();
  101. }
  102. } catch (SQLException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. // 测试
  107. public static void main(String[] args) {
  108. String ss = "kk=6565&ll=001&kk=";
  109. String mm[] = TestConection.getParameter("kk", ss).split(",");
  110. for (int i = 0; i < mm.length; i++) {
  111. System.out.println(mm[i]);
  112. }
  113. System.out.println(TestConection.getParameter("kk", ss));
  114. System.out.println(TestConection.getParameter("ll", ss));
  115. }
  116. }