123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package test.main.DB;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.StringTokenizer;
- public class TestConection{
- /**
- * 获取服务参数
- *
- * @author wzj
- * @param key
- * --参数名,params--服务得到的参数集合字符串
- * com.sinosoft.lz.system.Utils.getParameter(key, params)
- */
- public static String getParameter(String key, String params) {
- String str = "";
- String g = key + "=";
- String d = "";
- StringTokenizer commaToker = new StringTokenizer(params, "&");
- while (commaToker.hasMoreTokens()) {
- d = commaToker.nextToken();
- if (d.indexOf(g, 0) == 0) {
- if (g.equals(d)) {
- // str = null;
- } else {
- str += d.substring(g.length(), d.length()) + ",";
- }
- // break;
- }
- }
- String result = "";
- if (str.equals("")) {
- result = null;
- } else {
- result = str.substring(0, str.length() - 1);
- }
- return result;
- }
- /**
- * 一次只从数据库中查询最大maxCount条记录
- *
- * @param sql
- * 传入的sql语句
- * @param startNo
- * 从哪一条记录开始
- * @param maxCount
- * 总共取多少条记录
- */
- @SuppressWarnings("null")
- public void getData(String sql, int startNo, int maxCount) {
- Connection conn = null;
- try {
- // conn.prepareStatement(sql,游标类型,能否更新记录);
- // 游标类型:
- // ResultSet.TYPE_FORWORD_ONLY:只进游标
- // ResultSet.TYPE_SCROLL_INSENSITIVE:可滚动。但是不受其他用户对数据库更改的影响。
- // ResultSet.TYPE_SCROLL_SENSITIVE:可滚动。当其他用户更改数据库时这个记录也会改变。
- // 能否更新记录:
- // ResultSet.CONCUR_READ_ONLY,只读
- // ResultSet.CONCUR_UPDATABLE,可更新
- PreparedStatement pstat = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- // 最大查询到第几条记录
- pstat.setMaxRows(startNo + maxCount - 1);
- ResultSet rs = pstat.executeQuery();
- // 将游标移动到第一条记录
- rs.first();
- // 游标移动到要输出的第一条记录
- rs.relative(startNo - 2);
- while (rs.next())
- System.out.println(rs.getInt(1));
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * 从数据库中查询所有记录,然后通过游标来获取所需maxCount条记录
- *
- * @param sql
- * 传入的sql语句
- * @param startNo
- * 从哪一条记录开始
- * @param maxCount
- * 总共取多少条记录
- */
- @SuppressWarnings("null")
- public void getDataFromAll(String sql, int startNo, int maxCount) {
- Connection conn = null;
- try {
- PreparedStatement pstat = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_READ_ONLY);
- ResultSet rs = pstat.executeQuery();
- rs.first();
- rs.relative(startNo - 1);
- int i = startNo - 1;
- while (i < startNo + maxCount - 1 && !rs.isAfterLast()) {
- System.out.println(rs.getInt(1));
- i++;
- rs.next();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- // 测试
- public static void main(String[] args) {
- String ss = "kk=6565&ll=001&kk=";
- String mm[] = TestConection.getParameter("kk", ss).split(",");
- for (int i = 0; i < mm.length; i++) {
- System.out.println(mm[i]);
- }
- System.out.println(TestConection.getParameter("kk", ss));
- System.out.println(TestConection.getParameter("ll", ss));
- }
- }
|