java 代码
- package com.wide.cabaret.dao;
- import com.wide.cabaret.consts.Consts;
- import com.wide.cabaret.utils.DataConn;
- import com.wide.cabaret.utils.Html;
- import com.wide.cabaret.utils.Utils;
- import java.io.PrintStream;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class BaseDao extends DataConn{
- Html html = new Html();
- Utils utils = new Utils();
- Consts consts = new Consts();
- public BaseDao(){
- super();
- }
- /**
- * 單條寫入訊息
- * @param sql String
- * @return int
- */
- public int addData(String sql){
- int retint = consts.DATA_ADD_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- stmt.executeUpdate(sql);
- //this.printBug("建立訊息", sql);
- retint = consts.DATA_ADD_SUCCEED_STATE;
- } catch (Exception ex){
- ex.printStackTrace();
- } finally{
- this.freeCon(con, stmt);
- }
- return retint;
- }
- /**
- * 批量寫入新訊息
- * @param sql String[]
- * @return int
- */
- public int addData(String sql[]){
- int retint = consts.DATA_ADD_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- for (int i=0;i
- stmt.executeUpdate(sql[i]);
- }
- //this.printBug("建立訊息", sql);
- retint = consts.DATA_ADD_SUCCEED_STATE;
- } catch (Exception ex){
- ex.printStackTrace();
- } finally{
- this.freeCon(con, stmt);
- }
- return retint;
- }
- /**
- * 寫入符閤條件的新訊息
- * @param sqla String
- * @param sqlb String
- * @return int
- */
- public int addData(String sqla, String sqlb){
- int retint = consts.DATA_ADD_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- rs=stmt.executeQuery(sqla);
- //this.printBug("條件查詢", sqla);
- if (!rs.next()){
- stmt.executeUpdate(sqlb);
- //this.printBug("新增訊息", sqlb);
- retint = consts.DATA_ADD_SUCCEED_STATE;
- } else{
- retint = consts.DATA_ADD_CONDITION_ERROR_STATE;
- }
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt, rs);
- }
- return retint;
- }
- /**
- * 根據批量條件批量寫入訊息
- * @param sqla String[]
- * @param sqlb String[]
- * @return int
- */
- public int addData(String sqla[],String sqlb[]){
- int retint = consts.DATA_ADD_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- for(int i=0;i
- rs=stmt.executeQuery(sqla[i]);
- //this.printBug("條件查詢", sqla[i]);
- if(!rs.next()){
- stmt.executeUpdate(sqlb[i]);
- //this.printBug("新增訊息", sqlb[i]);
- retint = consts.DATA_ADD_SUCCEED_STATE;
- }else{
- retint = consts.DATA_ADD_CONDITION_ERROR_STATE;
- }
- }
- } catch (Exception ex){
- ex.printStackTrace();
- } finally{
- this.freeCon(con, stmt);
- }
- return retint;
- }
- /**
- * 單條修改訊息
- * @param sql String
- * @return int
- */
- public int editData(String sql){
- int retint = consts.DATA_EDIT_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- stmt.executeUpdate(sql);
- //this.printBug("修改訊息", sql);
- retint = consts.DATA_EDIT_SUCCEED_STATE;
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt);
- }
- return retint;
- }
- /**
- * 批量脩改訊息
- * @param sql String[]
- * @return int
- */
- public int editData(String sql[]){
- int retint = consts.DATA_EDIT_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- for (int i=0;i
- stmt.executeUpdate(sql[i]);
- }
- //this.printBug("修改訊息", sql);
- retint = consts.DATA_EDIT_SUCCEED_STATE;
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt);
- }
- return retint;
- }
- /**
- * 脩改符閤條件后的訊息
- * @param sqla String
- * @param sqlb String
- * @return int
- */
- public int editData(String sqla, String sqlb){
- int retint = consts.DATA_EDIT_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- rs=stmt.executeQuery(sqla);
- //this.printBug("條件查詢", sqla);
- if (!rs.next()){
- stmt.executeUpdate(sqlb);
- //this.printBug("修改訊息", sqlb);
- retint = consts.DATA_EDIT_SUCCEED_STATE;
- } else{
- retint = consts.DATA_EDIT_CONDITION_ERROR_STATE;
- }
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt, rs);
- }
- return retint;
- }
- /**
- * 根據批量條件批量修改訊息
- * @param sqla String[]
- * @param sqlb String[]
- * @return int
- */
- public int editData(String sqla[], String sqlb[]){
- int retint = consts.DATA_EDIT_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- for(int i=0;i
- rs=stmt.executeQuery(sqla[i]);
- //this.printBug("條件查詢", sqla[i]);
- if(!rs.next()){
- stmt.executeUpdate(sqlb[i]);
- //this.printBug("編輯訊息", sqlb[i]);
- retint = consts.DATA_EDIT_SUCCEED_STATE;
- }else{
- retint = consts.DATA_EDIT_CONDITION_ERROR_STATE;
- }
- }
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt, rs);
- }
- return retint;
- }
- /**
- * 單條刪除訊息
- * @param sql String
- * @return int
- */
- public int delData(String sql){
- int retint = consts.DATA_DEL_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- stmt.executeUpdate(sql);
- //this.printBug("刪除訊息", sql);
- retint = consts.DATA_DEL_SUCCEED_STATE;
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt);
- }
- return retint;
- }
- /**
- * 刪除數據訊息
- * @param sql String[]
- * @return int
- */
- public int delData(String sql[]){
- int retint = consts.DATA_DEL_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- for (int i=0;i
- stmt.executeUpdate(sql[i]);
- }
- //this.printBug("刪除訊息", sql);
- retint = consts.DATA_DEL_SUCCEED_STATE;
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt);
- }
- return retint;
- }
- /**
- * 刪除符閤條件后的數據訊息
- * @param sqla String
- * @param sqlb String
- * @return int
- */
- public int delData(String sqla, String sqlb){
- int retint = consts.DATA_DEL_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- rs=stmt.executeQuery(sqla);
- //this.printBug("條件查詢", sqla);
- if (rs.next()){
- stmt.executeUpdate(sqlb);
- //this.printBug("刪除訊息", sqlb);
- retint = consts.DATA_DEL_SUCCEED_STATE;
- } else{
- retint = consts.DATA_DEL_CONDITION_ERROR_STATE;
- }
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt, rs);
- }
- return retint;
- }
- /**
- * 根據批量條件批量刪除訊息
- * @param sqla String[]
- * @param sqlb String[]
- * @return int
- */
- public int delData(String sqla[], String sqlb[]){
- int retint = consts.DATA_DEL_ERROR_STATE;
- Connection con=null;
- Statement stmt=null;
- ResultSet rs=null;
- try{
- con=this.getCon();
- stmt=con.createStatement();
- for(int i=0;i
- rs=stmt.executeQuery(sqla[i]);
- //this.printBug("條件查詢", sqla[i]);
- if(!rs.next()){
- stmt.executeUpdate(sqlb[i]);
- //this.printBug("刪除訊息", sqlb[i]);
- retint = consts.DATA_DEL_SUCCEED_STATE;
- }else{
- retint = consts.DATA_DEL_CONDITION_ERROR_STATE;
- }
- }
- } catch (Exception ex){
- System.err.println(ex.getMessage());
- } finally{
- this.freeCon(con, stmt, rs);
- }
- return retint;
- }
- /**
- * 打印單條錯誤訊息
- * @param str String
- * @param sql String
- */
- public void printBug(String str, String sql){
- System.out.println("---------------------------------------------");
- System.out.println("<=========["+str+"]->打印調試訊息開始=========>");
- System.out.println("*");
- System.out.println("SQL==>"+sql+"");
- System.out.println("*");
- System.out.println("<=========["+str+"]->打印調試訊息結束=========>");
- System.out.println("---------------------------------------------");
- }
- /**
- * 批量打印錯誤訊息
- * @param str String
- * @param sql String[]
- */
- public void printBug(String str, String sql[]){
- System.out.println("---------------------------------------------");
- System.out.println("<=========["+str+"]->打印調試訊息開始=========>");
- for (int i=0;i
- System.out.println("*");
- System.out.println("SQL-("+i+")==>"+sql[i]+"");
- System.out.println("*");
- }
- System.out.println("<=========["+str+"]->打印調試訊息結束=========>");
- System.out.println("---------------------------------------------");
- }
- }