/* Connection Class for MySQL by Richard Niescior */ public class DBConnectionHandler { java.sql.Connection link; String u,p,l; public void popup(String b, String c, int d){ javax.swing.JOptionPane.showMessageDialog(null,b,c,d); } public java.sql.Connection dbcon(String a,String b,String c) throws java.sql.SQLException{ return java.sql.DriverManager.getConnection(a,b,c); } public DBConnectionHandler(String ... a){ if(a.length == 0)popup("Invalid Parameters","Cannot establish connection",2); if(a.length >= 1) l=a[0]; if(a.length <=3){ u = a[1]; p = a[2]; } try{ Class.forName("com.mysql.jdbc.Driver"); link = dbcon(l,u,p); }catch( java.sql.SQLException e){ e.printStackTrace(); popup(e.getLocalizedMessage(), e.getErrorCode()+"",2); // exit(0) }catch(ClassNotFoundException e){ popup("MYSQL Driver Library is missing! Consult the developer for more information!","Driver Missing!",2); //exit(0) } } public java.sql.ResultSet query(String sql){ java.sql.ResultSet rs = null; try{ rs = ((java.sql.Statement)link.createStatement()).executeQuery(sql); return rs; }catch( java.sql.SQLException e){ popup(e.getLocalizedMessage()+"\n\n A connection will be attempted again","Connection error",2); try{ link = dbcon(l,u,p); rs = ((java.sql.Statement)link.createStatement()).executeQuery(sql); return rs; } catch( java.sql.SQLException em){ popup(em.getLocalizedMessage()+"\n\nReconnection failed! Program will exit.","Reconnect failed!",2); System.exit(0); } } return rs; } public void update(String sql){ try{ ((java.sql.Statement)link.createStatement()).executeUpdate(sql); }catch( java.sql.SQLException e){ popup(e.getLocalizedMessage()+"\n\n A connection will be attempted again","Connection error",2); try{ link = dbcon(l,u,p); ((java.sql.Statement)link.createStatement()).executeUpdate(sql); } catch( java.sql.SQLException em){ popup(em.getLocalizedMessage()+"\n\nReconnection failed! Program will exit.","Reconnect failed!",2); System.exit(0); } } } public java.sql.ResultSet tableList(){ try{ return (link.getMetaData()).getTables(null, null, null, new String [] {"TABLE"}); }catch( java.sql.SQLException e){ popup(e.getLocalizedMessage()+"\n\n A connection will be attempted again","Connection error",2); try{ link = dbcon(l,u,p); return (link.getMetaData()).getTables(null, null, null, new String [] {"TABLE"}); } catch( java.sql.SQLException em){ popup(em.getLocalizedMessage()+"\n\nReconnection failed! Program will exit.","Reconnect failed!",2); System.exit(0); } } return null; } }