Advertisement
Guest User

Untitled

a guest
Apr 12th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1.  
  2. public class Db {
  3.     private final String url;
  4.     ResultSet resultSet = null;
  5.     Statement statement = null;
  6.     final String driver;
  7.     String user = "";
  8.     String database = "";
  9.     String password = "";
  10.     String port = "";
  11.     String host = "";
  12.     Connection c = null;
  13.  
  14.     public Db(String Host, String db, String username, String password) {
  15.         this.host = Host;
  16.         this.database = db;
  17.         this.user = username;
  18.         this.password = password;
  19.         url = "jdbc:mysql://" + host + "/" + database + "?user=" + user
  20.                 + "&password=" + password;
  21.         driver = ("com.mysql.jdbc.Driver");
  22.     }
  23.  
  24.     public Db(String filePath) {
  25.         url = "jdbc:sqlite:" + new File(filePath).getAbsolutePath();
  26.         driver = ("org.sqlite.JDBC");
  27.     }
  28.  
  29.     public Connection open() {
  30.         try {
  31.             Class.forName(driver);
  32.             this.c = DriverManager.getConnection(url);
  33.             return c;
  34.         } catch (SQLException e) {
  35.             System.out
  36.                     .println("Could not connect to MySQL/SQLite server! because: "
  37.                             + e.getMessage());
  38.         } catch (ClassNotFoundException e) {
  39.             System.out.println("JDBC Driver not found!");
  40.         }
  41.         return this.c;
  42.     }
  43.  
  44.     public boolean checkConnection() {
  45.         if (this.c != null) {
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50.  
  51.     public Connection getConn() {
  52.         return this.c;
  53.     }
  54.  
  55.     public void closeConnection(Connection c) {
  56.         c = null;
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement