whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/service/MybatisSqlExecuteService.java
@@ -13,6 +13,7 @@
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
@Service
@@ -138,4 +139,70 @@
            throwables.printStackTrace();
        }
    }
    public  boolean makeManualCommit( ArrayList<String> al_sql_strs){
        PreparedStatement ps = null;
        SqlSession sqlSession = openSession();
        Connection mysql_con=sqlSession.getConnection();
        boolean exe_res = true;
        try {
            mysql_con.setAutoCommit(false);
            for(int n=0; n<al_sql_strs.size(); n++) {
                if(true == exe_res) {
                    exe_res = executeCreateTableNoclose(mysql_con,al_sql_strs.get(n));
                } else {
                    break;
                }
            }
            if(true == exe_res) {
                mysql_con.commit();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            exe_res = false;
        } finally {
            try {
                if(false == exe_res) {
                    mysql_con.rollback();
                }
                mysql_con.setAutoCommit(true);
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }finally {
                closeSession(null,ps,sqlSession);
            }
        }
        return exe_res;
    }
    //执行sql语句with no colse
    public static boolean executeCreateTableNoclose(Connection mysql_con,String sql_str)
    {
        PreparedStatement ps=null;
        boolean rest = true;
        try
        {
            ps = mysql_con.prepareStatement(sql_str);
            ps.setQueryTimeout(600);
            ps.execute();
        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
            rest = false;
        }finally {
            try {
                ps.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return rest;
    }
}