daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package kwclan.metodos;
  2.  
  3. import java.io.File;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. public class Dados {
  12.  
  13.     private File file;
  14.     private Connection conn;
  15.     private Statement stmt;
  16.  
  17.     private Dados(File file) {
  18.         this.file = file;
  19.         try {
  20.             Class.forName("org.sqlite.JDBC");
  21.             conn = DriverManager.getConnection("jdbc:sqlite:" + this.file);
  22.             stmt = conn.createStatement();
  23.         } catch (ClassNotFoundException | SQLException e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.     private Dados(String urlconn) {
  29.         try {
  30.             conn = DriverManager.getConnection(urlconn);
  31.             stmt = conn.createStatement();
  32.         } catch (SQLException e) {
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.  
  37.     public Dados load(File file) {
  38.         return new Dados(file);
  39.     }
  40.  
  41.     public Dados load(String host, String user, String pass, String database) {
  42.         return new Dados("jdbc:mysql://" + host + "/" + database + "?" + "user=" + user + "&password=" + pass);
  43.     }
  44.  
  45.     public void update(String query) {
  46.         try {
  47.             stmt.executeUpdate(query);
  48.         } catch (SQLException e) {
  49.             e.printStackTrace();
  50.         }
  51.     }
  52.  
  53.     public ResultSet query(String q) {
  54.         try {
  55.             return stmt.executeQuery(q);
  56.         } catch (SQLException e) {
  57.         }
  58.         return null;
  59.     }
  60.  
  61.     public void close() {
  62.         try {
  63.             stmt.close();
  64.             conn.close();
  65.         } catch (SQLException e) {
  66.         }
  67.     }
  68.  
  69.     public boolean isConnected() {
  70.         try {
  71.             return stmt != null && conn != null && !stmt.isClosed() && !conn.isClosed();
  72.         } catch (SQLException e) {
  73.         }
  74.         return false;
  75.     }
  76.  
  77.     public Connection getConnection() {
  78.         return conn;
  79.     }
  80.  
  81.     public PreparedStatement ps(String query) throws SQLException {
  82.         return conn.prepareStatement(query);
  83.     }
  84.  
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top