📄 readhelper.java
字号:
package com.common.util;
import java.sql.*;
import java.util.*;
import java.io.*;
import com.dao.util.DbConnect;
public class ReadHelper {
//private Connector connector;
public ReadHelper() {
try {
//this.connector = ConnectorManager.getConnector();
} catch (Exception e) {
e.printStackTrace();
}
}
public List getData(String sqlStr) {
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
List result = new ArrayList();
try {
DbConnect db = new DbConnect();
conn=db.getConnection();
stm = conn.createStatement();
rs = stm.executeQuery(sqlStr);
int count = 0;
Item item = null;
while (rs.next()) {
count = rs.getMetaData().getColumnCount();
item = new Item();
for (int i = 1; i <= count; i++) {
item.addAttribute(
rs.getMetaData().getColumnName(i),
rs.getObject(i));
}
result.add(item);
}
} catch (Exception ex) {
try {
} catch (Exception ue) {
}
} finally {
try {
if (rs != null){
rs.close();
}
if (stm != null){
stm.close();
}
conn.close();
} catch (Exception e) {
//e.printStackTrace();
}
}
return result;
}
/**
* 根据列名取得一个数组。
*/
public Object[] getObjectArray(String sqlStr,String columnName) {
List tempList=getData(sqlStr);
Object[] result=new Object[tempList.size()];
Iterator it=tempList.iterator();
int i=0;
while(it.hasNext()){
Item item=(Item)it.next();
result[i]=item.getObject(columnName);
i++;
}
return result;
}
public List getData(String sqlStr, boolean accessUseIndex) {
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
List result = new ArrayList();
try {
DbConnect db = new DbConnect();
conn=db.getConnection();
stm = conn.createStatement();
rs = stm.executeQuery(sqlStr);
int count = 0;
Item item = null;
while (rs.next()) {
count = rs.getMetaData().getColumnCount();
if (accessUseIndex) {
item = new Item(accessUseIndex);
} else {
item = new Item();
}
for (int i = 1; i <= count; i++) {
item.addAttribute(
rs.getMetaData().getColumnName(i),
rs.getObject(i));
}
result.add(item);
}
} catch (Exception ex) {
try {
} catch (Exception ue) {
}
} finally {
try {
if (rs != null){
rs.close();
}
if (stm != null){
stm.close();
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public List getData(String sqlStr, int size) {
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
List result = new ArrayList();
try {
DbConnect db = new DbConnect();
conn=db.getConnection();
stm = conn.createStatement();
rs = stm.executeQuery(sqlStr);
int count = 0;
Item item = null;
int pos = 0;
while (rs.next()) {
count = rs.getMetaData().getColumnCount();
item = new Item();
for (int i = 1; i <= count; i++) {
item.addAttribute(
rs.getMetaData().getColumnName(i),
rs.getObject(i));
}
result.add(item);
pos++;
if (pos >= size)
break;
}
} catch (Exception ex) {
try {
} catch (Exception ue) {
}
} finally {
try {
if (rs != null){
rs.close();
}
if (stm != null){
stm.close();
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public Item getItem(String sqlStr) {
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
Item result = null;
try {
DbConnect db = new DbConnect();
conn=db.getConnection();
stm = conn.createStatement();
rs = stm.executeQuery(sqlStr);
int count = 0;
if (rs.next()) {
result = new Item();
count = rs.getMetaData().getColumnCount();
for (int i = 1; i <= count; i++) {
result.addAttribute(
rs.getMetaData().getColumnName(i),
rs.getObject(i));
}
}
} catch (Exception ex) {
try {
} catch (Exception ue) {
}
} finally {
try {
if (rs != null){
rs.close();
}
if (stm != null){
stm.close();
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public Item getItem(String sqlStr, boolean accessUseIndex) {
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
Item result = null;
try {
DbConnect db = new DbConnect();
conn=db.getConnection();
stm = conn.createStatement();
rs = stm.executeQuery(sqlStr);
int count = 0;
if (rs.next()) {
if (accessUseIndex) {
result = new Item(accessUseIndex);
} else {
result = new Item();
}
count = rs.getMetaData().getColumnCount();
for (int i = 1; i <= count; i++) {
//Debug.out(ReadHelper.class,rs.getMetaData().getColumnName(i));
result.addAttribute(
rs.getMetaData().getColumnName(i),
rs.getObject(i));
}
}
} catch (Exception ex) {
try {
} catch (Exception ue) {
}
} finally {
try {
if (rs != null){
rs.close();
}
if (stm != null){
stm.close();
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public boolean dumpToFile(String fileName, String sqlStr, String colName) {
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
boolean result = false;
InputStream is = null;
FileOutputStream fos = null;
try {
DbConnect db = new DbConnect();
conn=db.getConnection();
stm = conn.createStatement();
rs = stm.executeQuery(sqlStr);
if (rs.next()) {
is = rs.getBinaryStream(colName);
fos = new FileOutputStream(fileName);
byte[] buffer = null;
buffer = new byte[1024 * 100];
int bytesRead = 0;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
System.out.println("File size=" + bytesRead);
}
result = true;
}
} catch (Exception ex) {
try {
} catch (Exception ue) {
}
} finally {
try {
if (rs != null){
rs.close();
}
if (stm != null){
stm.close();
}
conn.close();
if (is != null)
is.close();
if (fos != null)
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String args[]) {
//ReadHelper.dumpToFile("d:/test.jpg","SELECT CONTENT FROM kinescope WHERE id='" + "36-VOESS-70806-Ldubf" + "'","CONTENT");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -