package db; import com.alibaba.fastjson.JSONObject; import entity.JConfig; import entity.QuestionKeyWord; import gameDesign.ElementConstructor; import processing.MyFunc; import java.sql.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class SqlHelper { private Connection conn = null; /* public SqlHelper(){ conn = DB.getConnection(); } */ public SqlHelper() throws SQLException { conn = C3p0Utils.getConnection(); } public void destroy() { if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } /* public boolean insertAction(QuestionKeyWord qkw){ String sql = "insert into user_info (chat_key,action_name) values(?,?)"; boolean b = false; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,qkw.getChatKey()); ps.setString(2,qkw.getActionName()); b = ps.execute(); return b; } catch(Exception e){ e.printStackTrace(); } return b; } public boolean updateAction(QuestionKeyWord qkw){ String sql = "update user_info set action_name=? where chat_key=?"; boolean b = false; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,qkw.getActionName()); ps.setString(2,qkw.getChatKey()); b = ps.execute(); return b; } catch (SQLException e) { e.printStackTrace(); } return b; } public String queryActionByKey(String chat_key){ String sql = "select * from user_info where chat_key= ?"; String pre_name = ""; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,chat_key); ResultSet rs = ps.executeQuery(); if(rs.next()) return rs.getString("action_name"); }catch(Exception e){ e.printStackTrace(); } return "empty"; } */ private static void releaseSource(Connection _conn, PreparedStatement _st, ResultSet _rs) throws SQLException { C3p0Utils.close(_conn, _st, _rs); } public String queryUsernameByKey(String chat_key) { String sql = "select * from chats where chat_key = ?"; String userName = ""; String result = "empty"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, chat_key); ResultSet rs = ps.executeQuery(); if (rs.next()) result = rs.getString("bot"); ps.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } return result; } public boolean deleteConfigByIdGroup(int botId, List idGroup) throws SQLException { StringBuilder inClause = new StringBuilder(); boolean firstValue = true; int batchSize = idGroup.size(); for (int i = 0; i < batchSize; i++) { inClause.append('?'); if (firstValue) { firstValue = false; } else { inClause.append(','); } } String sql = "delete from chatbot_config where botId = ? and configId in (" + inClause.toString() + ')'; String userName = ""; boolean b = true; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(botId)); b = ps.execute(); ps.close(); return b; } catch (Exception e) { e.printStackTrace(); } return b; } public boolean deleteConfigByBotId(int botId) { boolean b; try { PreparedStatement ps1 = conn.prepareStatement("delete from chatbot_config where botId= ?"); ps1.setString(1, String.valueOf(botId)); b = ps1.execute(); ps1.close(); return b; } catch (SQLException e) { System.out.println("删除旧数据失败"); e.printStackTrace(); return false; } } public boolean deleteVariableByBotId(int botId) { String sql = "delete from chatbot_variable where botId= ?"; boolean b; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(botId)); b = ps.execute(); ps.close(); return b; } catch (SQLException e) { System.out.println("删除旧数据失败"); e.printStackTrace(); return false; } } public boolean batchInsertVariableByBotId(int botId, ElementConstructor constructor) { int count[]; boolean insertFlag = true; String sql = "", varName, varValue, description; try { conn.setAutoCommit(false); sql = "insert into chatbot_variable (botId, name, categoryId, description, defaultValue) VALUES (?, ?, 0, ?, ?)"; PreparedStatement ps2 = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); Iterator iit = constructor.getVarInitialMap().keySet().iterator(); while (iit.hasNext()) { varName = iit.next(); varValue = constructor.getVarInitialMap().get(varName); if (constructor.getConditionDescription().get(varName) == null) description = ""; else description = constructor.getConditionDescription().get(varName); ps2.setString(1, String.valueOf(botId)); ps2.setString(2, varName); ps2.setString(3, description); ps2.setString(4, varValue); ps2.addBatch(); } count = ps2.executeBatch(); conn.commit(); for (int i : count) { if (i == 0) { conn.rollback(); System.out.println("======出现异常,回滚========="); insertFlag = false; } } ps2.close(); } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } System.out.println("------出现异常,回滚----------"); e.printStackTrace(); } return insertFlag; } public boolean batchInsertConfigByBotId(int botId, ElementConstructor constructor, List idList) { int count[]; boolean insertFlag = true; String sql = "insert into chatbot_config (botId, categoryId, configData, configName) VALUES (?, ?, ?, ?)"; try { conn.setAutoCommit(false); // 需要用到事务,不能让他自动提交,需要手动提交 PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for (JConfig jConfig : constructor.getjConfigList()) { ps.setString(1, String.valueOf(botId)); ps.setString(2, String.valueOf(0)); ps.setString(3, jConfig.toJson(constructor.getConditionDescription()).toJSONString()); ps.setString(4, ""); ps.addBatch(); } count = ps.executeBatch(); conn.commit(); for (int i : count) { if (i == 0) { conn.rollback(); System.out.println("======出现异常,回滚========="); insertFlag = false; } } ResultSet rs = ps.getGeneratedKeys(); while (rs.next()) idList.add(rs.getInt(1)); ps.close(); rs.close(); } catch (SQLException e) { try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } System.out.println("------出现异常,回滚----------"); e.printStackTrace(); } return insertFlag; } public JSONObject queryGameElementsByBotId(int id) { String sql = "select * from generate_json_tb where project_id = ?"; String userName = ""; String result = "{}"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(id)); ResultSet rs = ps.executeQuery(); if (rs.next()) result = rs.getString("json_text"); ps.close(); rs.close(); } catch (Exception e) { e.printStackTrace(); } return JSONObject.parseObject(result); } public static String queryEnvJsonByBotName(String botName) throws SQLException { String sql = "select * from generate_json_tb where bot_name = ?"; Connection conn = C3p0Utils.getConnection(); String result = "{}"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(botName)); ResultSet rs = ps.executeQuery(); if (rs.next()) result = rs.getString("env_json_text"); ps.close(); rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } return result; } public boolean insertVariableByBotId(int id, String variableName, String description, String defaultValue) { String sql = "insert into chatbot_variable (botId, name, description, defaultValue) VALUES (?, ?, ?, ?)"; String userName = ""; String result = "empty"; boolean b = true; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(id)); ps.setString(2, variableName); ps.setString(3, description); ps.setString(4, defaultValue); b = ps.execute(); ps.close(); } catch (Exception e) { e.printStackTrace(); } return b; } public boolean insertConfigByBotId(int botId, ElementConstructor constructor, JConfig jConfig, List idList) { String sql = "insert into chatbot_config (botId, categoryId, configData, configName) VALUES (?, ?, ?, ?)"; String userName = ""; String result = "empty"; ResultSet rs = null; int id = 0; boolean b = true; try { PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setString(1, String.valueOf(botId)); ps.setString(2, String.valueOf(0)); ps.setString(3, jConfig.toJson(constructor.getConditionDescription()).toJSONString()); ps.setString(4, ""); ps.executeUpdate(); rs = ps.getGeneratedKeys(); if(rs.next()){ id = rs.getInt(1); idList.add(id); } ps.close(); } catch (Exception e) { e.printStackTrace(); } return b; } }