| | |
| | | public class ErpSqlServer { |
| | | |
| | | // 数据库连接 |
| | | private Connection conn; |
| | | // private Connection conn; |
| | | // 创建预编译语句对象,一般都是用这个而不用Statement |
| | | private PreparedStatement pstm = null; |
| | | // 创建一个结果集对象 |
| | |
| | | |
| | | |
| | | private List<Map<String, Object>> executeQuery(String sql) { |
| | | Connection conn = null; |
| | | try { |
| | | Connection conn = getConn(); |
| | | conn = getConn(); |
| | | pstm = conn.prepareStatement(sql); |
| | | rs = pstm.executeQuery(); |
| | | // List<Map<String, Object>> maps = convertList(rs); |
| | |
| | | return null; |
| | | } finally { |
| | | release(); |
| | | if (conn != null) { |
| | | try { |
| | | conn.close(); |
| | | conn = null; |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | public Number executeQueryCount(String sql, String column) { |
| | | Number value = 0; |
| | | Connection conn = null; |
| | | try { |
| | | conn = getConn(); |
| | | pstm = conn.prepareStatement(sql); |
| | | rs = pstm.executeQuery(); |
| | | // ResultSetMetaData metaData = rs.getMetaData(); |
| | | while (rs.next()) { |
| | | value = (Number) rs.getInt(column); |
| | | } |
| | | return value; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return 0; |
| | | } finally { |
| | | release(); |
| | | if (conn != null) { |
| | | try { |
| | | conn.close(); |
| | | conn = null; |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private int executeUpdate(String sql) { |
| | | Connection conn = null; |
| | | try { |
| | | Connection conn = getConn(); |
| | | pstm = conn.prepareStatement(sql); |
| | | conn = getConn(); |
| | | if(null != conn) |
| | | { |
| | | pstm = conn.prepareStatement(sql); |
| | | } else { |
| | | log.error("更新ERP中间表失败===>>数据库连接conn为空"); |
| | | log.error("更新ERP中间表失败===>>" + sql); |
| | | } |
| | | return pstm.executeUpdate(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | return 0; |
| | | } finally { |
| | | release(); |
| | | if (conn != null) { |
| | | try { |
| | | conn.close(); |
| | | conn = null; |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | return list; |
| | | } |
| | | |
| | | public synchronized Connection getConn() throws SQLException { |
| | | if (null == this.conn || this.conn.isClosed()) { |
| | | try { |
| | | Class.forName(erpDbProperties.getDriver_class_name()).newInstance(); |
| | | this.conn = DriverManager.getConnection(erpDbProperties.getUr(), erpDbProperties.getUsername(), erpDbProperties.getPassword()); |
| | | } catch (Exception e) { |
| | | log.error("获取ERP数据库连接失败"); |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("获取ERP数据库连接失败"); |
| | | } |
| | | //synchronized |
| | | public Connection getConn() throws SQLException { |
| | | Connection conn = null; |
| | | try { |
| | | Class.forName(erpDbProperties.getDriver_class_name()).newInstance(); |
| | | conn = DriverManager.getConnection(erpDbProperties.getUr(), erpDbProperties.getUsername(), erpDbProperties.getPassword()); |
| | | } catch (Exception e) { |
| | | log.error("获取ERP数据库连接失败"); |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("获取ERP数据库连接失败"); |
| | | } |
| | | return this.conn; |
| | | return conn; |
| | | // if (null == this.conn || this.conn.isClosed()) { |
| | | // try { |
| | | // Class.forName(erpDbProperties.getDriver_class_name()).newInstance(); |
| | | // this.conn = DriverManager.getConnection(erpDbProperties.getUr(), erpDbProperties.getUsername(), erpDbProperties.getPassword()); |
| | | // } catch (Exception e) { |
| | | // log.error("获取ERP数据库连接失败"); |
| | | // e.printStackTrace(); |
| | | // throw new RuntimeException("获取ERP数据库连接失败"); |
| | | // } |
| | | // } |
| | | // return this.conn; |
| | | } |
| | | |
| | | private void release() { |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | if (conn != null) { |
| | | try { |
| | | conn.close(); |
| | | conn = null; |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | // if (conn != null) { |
| | | // try { |
| | | // conn.close(); |
| | | // conn = null; |
| | | // } catch (SQLException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | } |
| | | |
| | | /** |