📄 sqlexecproxy.java
字号:
package com.gs.util;
import java.sql.*;
import javax.sql.*;
import java.util.*;
//SQL语句执行代理
public class SQLExecProxy {
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
private boolean isAutoCommit=true;
public SQLExecProxy(){
}
//设置设置事务
public void setAutoCommit(boolean isAutoCommit) throws Exception{
try{
this.isAutoCommit=isAutoCommit;
// conn.setAutoCommit(isAutoCommit) ;
}
catch(Exception e){
throw new Exception("[SQLExecProxy-->setAutoCommit()]Exception:"+e.toString());
}
}
public void commitProxy() throws Exception{
try{
if(!isAutoCommit){
//conn.commit();
}
}
catch(Exception e){
throw new Exception("[SQLExecProxy-->commitProxy()]Exception:"+e.toString());
}
}
//执行返回结果集的SQL语句
public ResultSet executeQuery(String sql) throws Exception{
try{
rs=stmt.executeQuery(sql);
}
catch(Exception e){
throw new Exception("[SQLExecProxy-->executeQuery]Exception:"+e.toString());
}
finally{
return rs;
}
}
//执行更新SQL语句
public void executeUpdate(String sql) throws Exception{
try{
stmt.executeUpdate(sql);
}
catch(Exception e){
throw new Exception("[SQLProxy-->executeUpdate]Exception:"+e.toString());
}
}
//判断是否有下一行
public boolean nextRow() throws Exception{
boolean bl=false;
try{
if(rs.next())
bl=true;
}
catch(Exception e){
throw new Exception("[SQLExecProxy-->nextRow]Exception:"+e.toString());
}
finally{
return bl;
}
}
//返回字段的值
public String getFieldString(String fieldName) throws Exception{
String value="";
try{
value=rs.getString(fieldName.toLowerCase());
}
catch(Exception e){
throw new Exception("[SQLExecProxy-->getFieldString]Exception:"+e.toString());
}
finally{
return value;
}
}
//得到记录所有的值
public HashMap getRecordValue() throws Exception{
HashMap hm=new HashMap();
try{
ResultSetMetaData fieldMeta=rs.getMetaData();
int fieldCount=fieldMeta.getColumnCount();
for(int i=1;i<=fieldCount;i++){
String fieldName=fieldMeta.getColumnName(i);
String fieldValue=rs.getString(fieldName);
if (fieldName!=null && fieldName.length()>0 && fieldValue!=null && fieldValue.length()>0){
hm.put(fieldName.trim().toLowerCase(),fieldValue.trim());
}
}
}
catch(Exception e){
throw new Exception("[SQLExecProxy-->getRecordValue]Exception:"+e.toString());
}
finally{
return hm;
}
}
//打开SQLExecProxy
public void openProxy(){
try{
//得到Connection对象
conn = Common.getConnection();
stmt = conn.createStatement();
}
catch(Exception e){
Debug.println("[SQLProxy--->SQLProxy]Exception:"+e.toString());
}
}
//关闭SQLExecProxy
public void closeProxy(){
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(Exception e){
Debug.println("[SQLProxy-->closeProxy]Exception:"+e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -