Advertisement
Richard_Niescior

Java Database Connectivity Module

Sep 22nd, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. /*
  2. Connection Class for MySQL by Richard Niescior
  3. */
  4. public class DBConnectionHandler {
  5.    
  6.     java.sql.Connection link;
  7.     String u,p,l;
  8.     public void popup(String b, String c, int d){
  9.         javax.swing.JOptionPane.showMessageDialog(null,b,c,d);
  10.     }
  11.     public java.sql.Connection dbcon(String a,String b,String c) throws java.sql.SQLException{
  12.     return java.sql.DriverManager.getConnection(a,b,c);
  13. }
  14. public DBConnectionHandler(String ... a){
  15.     if(a.length == 0)popup("Invalid Parameters","Cannot establish connection",2);
  16.     if(a.length >= 1)
  17.     l=a[0];
  18.     if(a.length <=3){
  19.     u = a[1];
  20.     p = a[2];
  21. }
  22.    
  23.     try{
  24.         Class.forName("com.mysql.jdbc.Driver");
  25. link = dbcon(l,u,p);
  26. }catch( java.sql.SQLException e){
  27.     e.printStackTrace();
  28.     popup(e.getLocalizedMessage(), e.getErrorCode()+"",2);
  29.    // exit(0)
  30.        
  31.         }catch(ClassNotFoundException e){
  32.     popup("MYSQL Driver Library is missing! Consult the developer for more information!","Driver Missing!",2);
  33.     //exit(0)
  34. }
  35.  
  36. }
  37. public  java.sql.ResultSet query(String sql){
  38.     java.sql.ResultSet rs = null;
  39.     try{
  40.    rs = ((java.sql.Statement)link.createStatement()).executeQuery(sql);
  41.  
  42.    return rs;
  43. }catch( java.sql.SQLException e){
  44.     popup(e.getLocalizedMessage()+"\n\n A connection will be attempted again","Connection error",2);
  45.    try{
  46.        link = dbcon(l,u,p);
  47.         rs = ((java.sql.Statement)link.createStatement()).executeQuery(sql);
  48.  
  49.    return rs;
  50.    }
  51.    catch( java.sql.SQLException em){
  52.        popup(em.getLocalizedMessage()+"\n\nReconnection failed! Program will exit.","Reconnect failed!",2);
  53.        System.exit(0);
  54.                
  55.    }
  56. }
  57.     return rs;
  58. }
  59. public void update(String sql){
  60.    
  61.     try{
  62.  ((java.sql.Statement)link.createStatement()).executeUpdate(sql);
  63. }catch( java.sql.SQLException e){
  64.     popup(e.getLocalizedMessage()+"\n\n A connection will be attempted again","Connection error",2);
  65.    try{
  66.        link = dbcon(l,u,p);
  67.       ((java.sql.Statement)link.createStatement()).executeUpdate(sql);
  68.  
  69.    }
  70.    catch( java.sql.SQLException em){
  71.        popup(em.getLocalizedMessage()+"\n\nReconnection failed! Program will exit.","Reconnect failed!",2);
  72.        System.exit(0);
  73.                
  74.    }
  75. }
  76. }
  77. public java.sql.ResultSet tableList(){
  78.      try{
  79.          return (link.getMetaData()).getTables(null, null, null, new String [] {"TABLE"});
  80. }catch( java.sql.SQLException e){
  81.     popup(e.getLocalizedMessage()+"\n\n A connection will be attempted again","Connection error",2);
  82.     try{
  83.        link = dbcon(l,u,p);
  84.        return (link.getMetaData()).getTables(null, null, null, new String [] {"TABLE"});
  85.  
  86.    }
  87.    catch( java.sql.SQLException em){
  88.        popup(em.getLocalizedMessage()+"\n\nReconnection failed! Program will exit.","Reconnect failed!",2);
  89.        System.exit(0);
  90.                
  91.    }
  92. }
  93.        return null;
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement