Advertisement
Guest User

File2

a guest
Mar 14th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package co.windall.twitter;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. /**
  9.  * The Class Database.
  10.  * Provides a connection to the SQLite database.
  11.  */
  12. public class Database {
  13.  
  14.     /** The jdbc sqlite connection. */
  15.     private Connection connection;
  16.  
  17.     /**
  18.      * Instantiates a new database object.
  19.      */
  20.     public Database() {
  21.         super();
  22.        
  23.         try {
  24.             //Connect to the database
  25.             Class.forName("org.sqlite.JDBC");
  26.             this.connection = DriverManager.getConnection("jdbc:sqlite:bot2.db");
  27.            
  28.             //If this is a first time use or the database has been reset, make the need tables
  29.             Statement stmt = connection.createStatement();
  30.             String sql = "CREATE TABLE IF NOT EXISTS User ("
  31.                         + "id INT PRIMARY KEY," //Twitter ID
  32.                         + "followedMe INT," //TIME
  33.                         + "followedThem INT," //TIME
  34.                         + "unfollowedMe INT," //TIME
  35.                         + "unfollowedThem INT," //TIME
  36.                         + "status INT)"; //0=DEFAULT,1=PROTECTED,2=VIT,3=CHURN,4=CULL,5=FOLLOW,6=OUTSIDER,7=DEAD
  37.             stmt.executeUpdate(sql);
  38.             stmt.close();
  39.         } catch (Exception e) {
  40.             System.err.println(e.getClass().getName() + ": " + e.getMessage());
  41.             System.exit(0);
  42.         }
  43.     }
  44.  
  45.     /**
  46.      * Gets the database connection.
  47.      *
  48.      * @return the connection
  49.      */
  50.     public Connection getConnection() {
  51.         return connection;
  52.     }
  53.  
  54.     /**
  55.      * Closes the database connection.
  56.      */
  57.     public void close() {
  58.         try {
  59.             this.connection.close();
  60.         } catch (SQLException e) {
  61.             System.err.println(e.getClass().getName() + ": " + e.getMessage());
  62.         }
  63.     }
  64.  
  65.     /* (non-Javadoc)
  66.      * @see java.lang.Object#finalize()
  67.      * Adds a call to the close method
  68.      */
  69.     @Override
  70.     protected void finalize() throws Throwable {
  71.         this.close();
  72.         super.finalize();
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement