package db; import com.alibaba.fastjson.JSONObject; import entity.JConfig; import entity.QuestionKeyWord; import processing.MyFunc; import java.sql.*; import java.util.ArrayList; import java.util.List; public class SqlHelper { private Connection conn = null; /* public SqlHelper(){ conn = DB.getConnection(); } */ public SqlHelper() throws SQLException { conn = DBCPUtils.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"; } */ 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"); rs.close(); ps.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){ String sql = "delete from chatbot_config 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 batchInsertConfigByBotId(int botId, List jConfigList, 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 : jConfigList) { ps.setString(1, String.valueOf(botId)); ps.setString(2, String.valueOf(0)); ps.setString(3, jConfig.toJson().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 id = ?"; String userName = ""; String result = "empty"; try{ PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, String.valueOf(id)); ResultSet rs = ps.executeQuery(); if(rs.next()) result = rs.getString("json_text"); rs.close(); ps.close(); }catch(Exception e){ e.printStackTrace(); } return JSONObject.parseObject(result); } }