KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > DbUtil


1 package com.jdon.util;
2
3 import java.util.*;
4 import javax.sql.*;
5 import java.sql.*;
6
7 public class DbUtil {
8
9   public final static String JavaDoc ORACLE = "oracle";
10   public final static String JavaDoc MYSQL = "mysql";
11   public final static String JavaDoc SQLSERVER = "sql server";
12   public final static String JavaDoc INTERBASE = "interbase";
13   public final static String JavaDoc POSTGRES = "postgres";
14
15
16   private static boolean init = false;
17   // True if the database requires large text fields to be streamed.
18
public static boolean streamLargeText;
19
20   // True if the database supports the Statement.setMaxRows() method.
21
public static boolean supportsMaxRows;
22   // True if the database supports the Statement.setFetchSize() method.
23
public static boolean supportsFetchSize;
24   // True if the database supports correlated subqueries.
25

26   public static void testConnection(Connection con) {
27     if (init)
28       return;
29
30     init = true;
31     // Set defaults for other meta properties
32
streamLargeText = false;
33     supportsMaxRows = true;
34     supportsFetchSize = true;
35
36     try {
37       DatabaseMetaData metaData = con.getMetaData();
38       // Supports transactions?
39

40       // Get the database name so that we can perform meta data settings.
41
String JavaDoc dbName = metaData.getDatabaseProductName().toLowerCase();
42       String JavaDoc driverName = metaData.getDriverName().toLowerCase();
43
44       // Oracle properties.
45
if (dbName.indexOf(ORACLE) != -1) {
46
47         streamLargeText = true;
48         // The i-net AUGURO JDBC driver
49
if (driverName.indexOf("auguro") != -1) {
50           streamLargeText = false;
51           supportsFetchSize = true;
52           supportsMaxRows = false;
53         }
54       }
55       // Postgres properties
56
else if (dbName.indexOf(POSTGRES) != -1) {
57         supportsFetchSize = false;
58       }
59       // Interbase properties
60
else if (dbName.indexOf(INTERBASE) != -1) {
61         supportsFetchSize = false;
62         supportsMaxRows = false;
63       }
64       // SQLServer, JDBC driver i-net UNA properties
65
else if (dbName.indexOf(SQLSERVER) != -1 &&
66                driverName.indexOf("una") != -1) {
67         supportsFetchSize = true;
68         supportsMaxRows = false;
69       }
70       // MySQL properties
71
else if (dbName.indexOf(MYSQL) != -1) {
72
73       }
74
75     } catch (Exception JavaDoc ex) {
76
77     }
78
79   }
80
81
82  public static int getProductAllCount(DataSource ds, String JavaDoc key, String JavaDoc sql_allcount) throws Exception JavaDoc {
83    Connection c = null;
84    PreparedStatement ps = null;
85    ResultSet rs = null;
86    int ret = 0;
87    try {
88      c = ds.getConnection();
89      ps = c.prepareStatement(sql_allcount,
90                              ResultSet.TYPE_SCROLL_INSENSITIVE,
91                              ResultSet.CONCUR_READ_ONLY);
92      ps.setString(1, key);
93      rs = ps.executeQuery();
94      if (rs.first()) {
95        ret = rs.getInt(1);
96      }
97    } catch (SQLException se) {
98      throw new Exception JavaDoc("SQLException: " + se.getMessage());
99    } finally {
100      if (rs != null)
101        rs.close();
102      if (ps != null)
103        ps.close();
104      if (c != null)
105        c.close();
106    }
107    return ret;
108
109  }
110
111   //该方法ä¸?能返回hasNext,待完善ï¼?
112
public List getDataList(DataSource ds, String JavaDoc key, String JavaDoc sql, int start, int count) throws
113      Exception JavaDoc {
114    Connection c = null;
115    Statement stmt = null;
116    ResultSet rs = null;
117    boolean hasNext = false;
118    List items = new ArrayList(count);
119    try {
120      c = ds.getConnection();
121      stmt = c.createStatement();
122
123      DbUtil.testConnection(c);
124      if (DbUtil.supportsMaxRows)
125        stmt.setMaxRows(count + start + 1);
126      rs = stmt.executeQuery(sql);
127      if (DbUtil.supportsFetchSize)
128        rs.setFetchSize(count);
129      if (start >= 0 && rs.absolute(start + 1)) {
130
131        do {
132          items.add(rs.getString(1));
133        }
134        while ( (hasNext = rs.next()) && (--count > 0));
135
136      }
137
138    } catch (SQLException se) {
139      throw new Exception JavaDoc("SQLException: " + se.getMessage());
140    } finally {
141      if (rs != null)
142        rs.close();
143      if (stmt != null)
144        stmt.close();
145      if (c != null)
146        c.close();
147    }
148     return items;
149  }
150
151
152 }
Popular Tags