/* Copyright (c) 2004 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Iterator; import java.util.LinkedList; public class SQLConnection { private Connection db; //private PipedInputStream PIS; //private PipedOutputStream POS; //private ObjectOutputStream OOS; private int OnLine; private ConfigFile Config; private LinkedList OpenStatements; public SQLConnection(ConfigFile C) { Config=C; OpenStatements=new LinkedList(); OnLine=0; try { Class.forName(C.getString("SQLDriver")).newInstance(); String url=Config.getString("SQLUrl"); String username=Config.getString("SQLUsername"); String password=Config.getString("SQLPassword"); db = DriverManager.getConnection(url, username, password); if (db==null) { OnLine=-8; System.out.println("Database connection failed. Returned null db."); } else { Reset(); OnLine=1; TestConnection(); } } catch (SQLException e) { System.out.println("Failed to establish database connection"); OnLine=-2; } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); OnLine=-4; } //System.out.println(OnLine); } public void Reset() { if (OnLine>0) { setAutoCommit(true); for(Iterator I=OpenStatements.iterator(); I.hasNext(); ) { Statement S=(Statement)I.next(); try { S.close(); } catch (SQLException e) { OnLine=-5; // TODO Auto-generated catch block e.printStackTrace(); } } OpenStatements.clear(); } } public void close() { OnLine=-100; try { db.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void commit() { try { db.commit(); } catch (SQLException e) { OnLine=-5; System.out.println(e.toString()); e.printStackTrace(); } } public void setAutoCommit(boolean B) { try { db.setAutoCommit(B); } catch (SQLException e) { OnLine=-5; System.out.println(e.toString()); e.printStackTrace(); } } public boolean ok() { if (OnLine <= 0) return false; try { TestConnection(); if (OnLine <= 0) return false; return true; } catch (Exception e) { e.printStackTrace(); return false; } } public int Statement(String Str) { Statement S; boolean ResultType; try { S=db.createStatement(); ResultType=S.execute(Str); OpenStatements.add(S); } catch (SQLException e) { System.out.println(e.toString()); System.out.println("Failure to execute SQL query"); OnLine=-6; return (-1); } int Updates=-1; if (ResultType) { Updates=0; } else { try { Updates=S.getUpdateCount(); } catch(SQLException e) { System.out.println(e.toString()); e.printStackTrace(); OnLine=-6; return (-1); } } return Updates; } public ResultSet SingleQuery(String Str) { return SingleQuery(Str,false); } public ResultSet SingleQuery(String Str, boolean Silent) { Statement S; ResultSet R; try { S=db.createStatement(); R=S.executeQuery(Str); OpenStatements.add(S); return R; } catch (SQLException e) { if(!Silent) { System.out.println(e.toString()); System.out.println("Failure to execute SQL query"); } OnLine=-6; } return null; } public ResultSet SingleUpdatableQuery(String Str) { Statement S; ResultSet R; try { S=db.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); R=S.executeQuery(Str); OpenStatements.add(S); return R; } catch (SQLException e) { System.out.println(e.toString()); System.out.println("Failure to execute SQL query"); OnLine=-6; } return null; } public void TestConnection() { SingleQuery("show tables",true); } }