Advertisement
Guest User

Untitled

a guest
Sep 14th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.46 KB | None | 0 0
  1. package database.api;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11.  
  12. import database.model.User;
  13.  
  14. public class DBApi {
  15.  
  16.     public static final String DRIVER = "org.sqlite.JDBC";
  17.     public static final String DB_URL = "jdbc:sqlite:users_table.db";    
  18.    
  19.     private Connection conn;
  20.     private Statement stat;
  21.    
  22.    
  23.     /*******************
  24.     ***** C-TOR *****
  25.     ******************/
  26.     /* 1) Loading driver to system
  27.      * 2) Creating connection with DB
  28.      * Here is type and name of DB
  29.      * 3) Creating object 'stat' of class Statement
  30.      *  - it allows to execute queries base on
  31.      *    earlier defined Strings
  32.     */  
  33.     public DBApi(){
  34.         try {
  35.             Class.forName(DBApi.DRIVER); //1
  36.         } catch (ClassNotFoundException e) {
  37.             System.err.println("Brak sterownika JDBC");
  38.             e.printStackTrace();
  39.         }
  40.         try {
  41.             conn = DriverManager.getConnection(DB_URL); //2
  42.             stat = conn.createStatement(); //3
  43.         } catch (SQLException e) {
  44.             System.err.println("Problem z otwarciem polaczenia");
  45.             e.printStackTrace();
  46.         }
  47.        
  48.         createTables();    
  49.     }
  50.    
  51.     /*******************
  52.      ** CREATE TABLES **
  53.      ******************/
  54.     ///table name: users_table
  55.     ///fields id, login, password, username, privileges
  56.     public boolean createTables()  {
  57.         String createUsers = "CREATE TABLE IF NOT EXISTS users_table (id INTEGER PRIMARY KEY AUTOINCREMENT, "
  58.                 + "login varchar(255), password varchar(255), username varchar(255), privileges INTEGER)";
  59.  
  60.         try {
  61.             stat.execute(createUsers);
  62.         } catch (SQLException e) {
  63.             System.err.println("Blad przy tworzeniu tabeli");
  64.             e.printStackTrace();
  65.             return false;
  66.         }
  67.         return true;
  68.     }
  69.    
  70.     /*******************
  71.      ** INSERT USERS **
  72.      ******************/
  73.     public boolean insertUser(String login, String password,
  74.                               String username, int privileges) {
  75.         try {
  76.             //PreparedStatement class allows to create
  77.             //schema of query which need to be execute
  78.             //and matching appropriate values of values
  79.             //using as arguments method
  80.             PreparedStatement prepStmt = conn.prepareStatement(
  81.                     "insert into users_table values (NULL, ?, ?, ?, ?);");
  82.             prepStmt.setString(1, login);
  83.             prepStmt.setString(2, password);
  84.             prepStmt.setString(3, username);
  85.             prepStmt.setInt(4, privileges);
  86.             System.out.println("Wstawiam usera login: " + login +
  87.                     " password: " + password + " username: " + username
  88.                       + "privileges: " + privileges);
  89.             prepStmt.execute();
  90.         } catch (SQLException e) {
  91.             System.err.println("Blad przy wstawianiu uzytkownika!");
  92.             e.printStackTrace();
  93.             return false;
  94.         }
  95.         return true;
  96.     }
  97.    
  98.     /*******************
  99.      ** SELECT USERS **
  100.      ******************/
  101.     //method return list of all records from table
  102.     public List<User> selectUsers(){
  103.         List<User> users = new LinkedList<User>();
  104.         try{
  105.             ResultSet result = stat.executeQuery("SELECT * FROM users_table");
  106.             int id, privileges;
  107.             String login, password, username;
  108.             while(result.next()){
  109.                 id = result.getInt("id");
  110.                 login = result.getString("login");
  111.                 password = result.getString("password");
  112.                 username = result.getString("username");
  113.                 privileges = result.getInt("privileges");
  114.                 users.add(new User(id, login, password, username, privileges));            
  115.             }
  116.         }catch(SQLException e){
  117.             e.printStackTrace();
  118.             return null;
  119.         }      
  120.         return users;
  121.     }
  122.    
  123.     /*******************
  124.      ** DELETE USER **
  125.      ******************/
  126.     public List<User> deletetUser(String l){
  127.        
  128.         List<User> users = new LinkedList<User>();
  129.                
  130.         users = selectUsers();
  131.         boolean isUserFound = false;
  132.  
  133.         //check if choosen user exists
  134.         for(User u:users){
  135.             System.out.println(u);
  136.             if ( l.equals(u.getLogin()) ){  
  137.                 System.out.println("login kasowanego usera: " + l);
  138.                 isUserFound = true;
  139.             }  
  140.         }
  141.        
  142.         if(isUserFound){
  143.             ///user will be deleted from db
  144.             try{
  145.                
  146.                 String deleteUser = "DELETE FROM users_table WHERE login='"+l+"'";
  147.                 stat.execute(deleteUser);
  148.                 System.out.println("DELETE USER command executed!");       
  149.             }catch(SQLException e){
  150.                 e.printStackTrace();
  151.                 return null;
  152.             }
  153.             ///but it still exist at list, so..    
  154.             users = selectUsers();
  155.            
  156.             ///so now users contains list without deleted user
  157.             return users;      
  158.         }
  159.         else{
  160.             System.out.println("Nie znaleziono takiego usera");
  161.             return users;
  162.         }  
  163.     }
  164.    
  165.     /**********************
  166.      **** DELETE TABLE ****
  167.      *********************/
  168.     public boolean deleteTable(){
  169.        
  170.         String dropTable = "DROP TABLE users_table";
  171.         try {
  172.             stat.execute(dropTable);
  173.             System.out.println("DROP command executed!");
  174.         } catch (SQLException e) {
  175.             System.err.println("Blad przy DROP!");
  176.             e.printStackTrace();
  177.             return false;
  178.         }
  179.         return true;  
  180.     }
  181.    
  182.     /**********************
  183.      ** DELETE ALL USERS **
  184.      *********************/  
  185.     public boolean deleteAllUsers(){
  186.        
  187.         String deleteUsers = "DELETE FROM users_table";
  188.  
  189.         try {
  190.             stat.execute(deleteUsers);
  191.             System.out.println("DELETE command executed!");
  192.         } catch (SQLException e) {
  193.             System.err.println("Blad przy usuwaniu!");
  194.             e.printStackTrace();
  195.             return false;
  196.         }
  197.         return true;         
  198.     }
  199.    
  200.     /*******************
  201.      ** CLOSE CONNECTION **
  202.      ******************/
  203.     public void closeConnection() {
  204.         try {
  205.             conn.close();
  206.         } catch (SQLException e) {
  207.             System.err.println("Problem z zamknieciem polaczenia");
  208.             e.printStackTrace();
  209.         }
  210.     }    
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement