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 ORACLE = "oracle"; 10 public final static String MYSQL = "mysql"; 11 public final static String SQLSERVER = "sql server"; 12 public final static String INTERBASE = "interbase"; 13 public final static String POSTGRES = "postgres"; 14 15 16 private static boolean init = false; 17 public static boolean streamLargeText; 19 20 public static boolean supportsMaxRows; 22 public static boolean supportsFetchSize; 24 26 public static void testConnection(Connection con) { 27 if (init) 28 return; 29 30 init = true; 31 streamLargeText = false; 33 supportsMaxRows = true; 34 supportsFetchSize = true; 35 36 try { 37 DatabaseMetaData metaData = con.getMetaData(); 38 40 String dbName = metaData.getDatabaseProductName().toLowerCase(); 42 String driverName = metaData.getDriverName().toLowerCase(); 43 44 if (dbName.indexOf(ORACLE) != -1) { 46 47 streamLargeText = true; 48 if (driverName.indexOf("auguro") != -1) { 50 streamLargeText = false; 51 supportsFetchSize = true; 52 supportsMaxRows = false; 53 } 54 } 55 else if (dbName.indexOf(POSTGRES) != -1) { 57 supportsFetchSize = false; 58 } 59 else if (dbName.indexOf(INTERBASE) != -1) { 61 supportsFetchSize = false; 62 supportsMaxRows = false; 63 } 64 else if (dbName.indexOf(SQLSERVER) != -1 && 66 driverName.indexOf("una") != -1) { 67 supportsFetchSize = true; 68 supportsMaxRows = false; 69 } 70 else if (dbName.indexOf(MYSQL) != -1) { 72 73 } 74 75 } catch (Exception ex) { 76 77 } 78 79 } 80 81 82 public static int getProductAllCount(DataSource ds, String key, String sql_allcount) throws Exception { 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 ("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 public List getDataList(DataSource ds, String key, String sql, int start, int count) throws 113 Exception { 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 ("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 |