📄 daoimp.java
字号:
package com.dao.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.sql.*;
import javax.sql.*;
/**
* @author
*
* 数据库操作的实现
*/
public class DaoImp {
/**
* @see com.westcn.util.business.core.dao.Dao#insert(String)
*/
public int insert(String sql) throws Exception {
Connection conn=null;
Statement stmt=null;
int result=0;
try{
DbConnect db = new DbConnect();
conn=db.getConnection();
stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
return result;
}catch(Exception e){
Exception ke=new Exception("操作数据库出错!DAO Insert方法!!SQL:"+sql);
e.printStackTrace();
throw ke;
}finally{
try {
conn.close();
stmt.close();
} catch (SQLException e) {
//System.out.println("关闭连接失败");
}
}
}
/**
* @see com.westcn.util.business.core.dao.Dao#delete(String)
*/
public int delete(String sql) throws Exception {
Connection conn=null;
Statement stmt=null;
int result=0;
try{
DbConnect db = new DbConnect();
conn=db.getConnection();
stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
return result;
}catch(Exception e){
Exception ke=new Exception("操作数据库出错!DAO Delete方法!!SQL:"+sql);
throw ke;
}finally{
try {
conn.close();
stmt.close();
} catch (SQLException e) {
//System.out.println("关闭连接失败");
}
}
}
/**
* @see com.westcn.util.business.core.dao.Dao#find(String)
*/
public HashMap find(String sql) throws Exception {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
DbConnect db = new DbConnect();
conn=db.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
HashMap map=new HashMap();
ResultSetMetaData meta;
meta=rs.getMetaData();
int cc;
cc=meta.getColumnCount();
if(rs.next()){
String tbl=meta.getTableName(1);
for(int i=1;i<=cc;i++){
String name=meta.getColumnLabel(i).toUpperCase();
Object val=rs.getObject(i);
if(val==null){
val="";
}
if(val.equals("null")){
val="";
}
map.put(name,val);
}
}
return map;
}catch(Exception e){
Exception ke=new Exception("操作数据库出错!DAO Find方法!!SQL:"+sql);
throw ke;
}finally{
try {
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
//System.out.println("关闭连接失败");
}
}
}
/**
* @see com.westcn.util.business.core.dao.Dao#select(String)
*/
public List select(String sql) throws Exception {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
List list=new ArrayList();
try{
DbConnect db = new DbConnect();
conn=db.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
ResultSetMetaData meta;
meta=rs.getMetaData();
int cc;
cc=meta.getColumnCount();
while(rs.next()){
HashMap map=new HashMap();
String tbl=meta.getTableName(1);
for(int i=1;i<=cc;i++){
String name=meta.getColumnLabel(i).toUpperCase();
Object val=rs.getObject(i);
if(val==null){
val="";
}
if(val.equals("null")){
val="";
}
map.put(name,val);
}
list.add(map);
}
return list;
}catch(Exception e){
Exception ke=new Exception("操作数据库出错!DAO Find方法!!SQL:"+sql);
throw ke;
}finally{
try {
conn.close();
stmt.close();
rs.close();
} catch (Exception e) {
//System.out.println("关闭连接失败");
}
}
}
/**
* @see com.westcn.util.business.core.dao.Dao#insertN(String)
*/
public int insertN(String sql) throws Exception {
return 0;
}
/**
* @see com.westcn.util.business.core.dao.Dao#update(String)
*/
public int update(String sql) throws Exception {
Connection conn=null;
Statement stmt=null;
int result=0;
try{
DbConnect db = new DbConnect();
conn=db.getConnection();
stmt=conn.createStatement();
result=stmt.executeUpdate(sql);
return result;
}catch(Exception e){
Exception ke=new Exception("操作数据库出错!DAO Update方法!!SQL:"+sql);
throw ke;
}finally{
try {
conn.close();
stmt.close();
} catch (SQLException e) {
//System.out.println("关闭连接失败");
}
}
}
/**
* @see com.westcn.util.business.core.dao.Dao#updateN(String)
*/
public Collection updateN(String sql) throws Exception {
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -