Advertisement
Guest User

Davide

a guest
Sep 5th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. public final class DatabaseModel implements Model{
  2.     private final static String HOST    = "sql11.freesqldatabase.com";
  3.     private final static int    PORT    = 3306;
  4.     private final static String USER_DB = "sql11192316";
  5.     private final static String PASS_DB = "l3dn9N16VL";
  6.     public final static String DB      = "sql11192316";
  7.    
  8.     public static Connection   defaultConnection = null;
  9.  
  10.     public static Connection getDefaultConnection() throws SQLException{
  11.         if( defaultConnection == null || defaultConnection.isClosed() )
  12.             defaultConnection = newConnection();
  13.         return defaultConnection;
  14.     }
  15.    
  16.     public static Connection newConnection() throws SQLException {
  17.         Connection connection = null;
  18.         try{
  19.             connection = DriverManager.getConnection(
  20.                 "jdbc:mysql://"+HOST+":"+PORT+"",
  21.                 USER_DB,
  22.                 PASS_DB
  23.             );
  24.         }
  25.         catch (SQLException ex) {
  26.             throw new RuntimeException("Connection not available.");
  27.         }
  28.         return connection;
  29.     }
  30.  
  31.     public static void showSQLError(SQLException e, String query, int contesto) {
  32.       String msg;
  33.       if ((e.getErrorCode() == 17068 | e.getErrorCode() == 17011) &
  34.               contesto == 0) {
  35.          return; //questo errore non mi interessa
  36.       }
  37.       msg = "ErrorCode= " + e.getErrorCode() + "\n";
  38.       msg += "Message= " + e.getMessage() + "\n";
  39.       msg += "SQLState= " + e.getSQLState() + "\n";
  40.       System.out.println(msg);
  41.    }
  42.    
  43.    public static void showSQLError(SQLException e){
  44.        showSQLError(e,"",0);
  45.    }
  46.  
  47.     /*@Override
  48.     public void attachView(View v) {
  49.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  50.     }*/
  51.    
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement