Guest User

Untitled

a guest
Apr 1st, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. import javax.xml.transform.Result;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.sql.*;
  5.  
  6.  
  7. public class SQL {
  8.  
  9.     public String tableName = "CarRental";
  10.     public Connection connection = getConnection(tableName);
  11.  
  12.     public SQL(){
  13.         createTable(connection, tableName);
  14.     }
  15.  
  16.  
  17.  
  18.     private Connection getConnection(String tableName){
  19.         Connection connection = null;
  20.         try{
  21.             Class.forName("org.sqlite.JDBC");
  22.             connection = DriverManager.getConnection("jdbc:sqlite:" + tableName + ".db");
  23.         }catch(Exception e){
  24.             System.out.println("Napotkano blad podczas polaczenia.");
  25.             e.printStackTrace();
  26.         }
  27.         return connection;
  28.     }
  29.  
  30.     public void createTable(Connection connection, String tableName) {
  31.         Statement statement = null;
  32.         try {
  33.             statement = connection.createStatement();
  34.             String query = "CREATE TABLE " + tableName + " (ID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, BRAND TEXT, MODEL TEXT, PRODUCTIONDATE TEXT, STATUS TEXT)";
  35.             statement.executeUpdate(query);
  36.             statement.close();
  37.             connection.close();
  38.         } catch (Exception e) {
  39.             File file = new File("CarRental.db");
  40.             FileInputStream fin;
  41.             try{
  42.                fin = new FileInputStream(file);
  43.                 fin.close();
  44.             }catch (Exception x){
  45.                 System.out.println("Blad");
  46.             }
  47.         }
  48.     }
  49.  
  50.         public void addNewCar(String tableName, int id, String carbrand,String carmodel,String produkcja,String status){
  51.  
  52.             Statement statement = null;
  53.             try{
  54.                 Class.forName("org.sqlite.JDBC");
  55.                 connection = DriverManager.getConnection("jdbc:sqlite:" + tableName + ".db");
  56.                 statement = connection.createStatement();
  57.                 String query = "INSERT INTO "+tableName+" VALUES ("+id+", "+"\""+carbrand+"\", \""+carmodel+"\", \""+produkcja+"\", \""+status+"\")";
  58.                 statement.executeUpdate(query);
  59.                 statement.close();
  60.                 connection.close();
  61.     }catch(Exception e){
  62.                 System.out.println("Blad");
  63.                 e.printStackTrace();
  64.             }
  65.     }
  66.  
  67.     public void updateTable(String tableName, String poleSzukane, String wartoscSzukana, String poleZmienne, String nowaWartosc){
  68.         Statement statement;
  69.         Connection connection;
  70.         try{
  71.             Class.forName("org.sqlite.JDBC");
  72.             connection = DriverManager.getConnection("jdbc:sqlite:" +tableName+ ".db");
  73.             statement = connection.createStatement();
  74.             String query = "UPDATE " + tableName + " SET " + poleZmienne+ " = \""+nowaWartosc+"\" WHERE "+poleSzukane+" = \""+wartoscSzukana+"\"";
  75.             statement.executeUpdate(query);
  76.             statement.close();
  77.             connection.close();
  78.         }catch(Exception e){
  79.     e.printStackTrace();
  80.         }
  81.     }
  82.  
  83.     public void search(String element, String tablica, String pole, String wartosc){
  84.         Connection connect = null;
  85.         Statement statement = null;
  86.         try{
  87.             Class.forName("org.sqlite.JDBC");
  88.             connect = DriverManager.getConnection("jdbc:sqlite:"+tablica+".db");
  89.             statement = connect.createStatement();
  90.             String query = "SELECT "+element+" FROM "+tablica+" WHERE "+pole+" = "+"\""+wartosc+"\"";
  91.  
  92.             ResultSet wynik = statement.executeQuery(query);
  93.             while(wynik.next()){
  94.                 int id = wynik.getInt("ID");
  95.                 System.out.println("ID:     "+id);
  96.                 System.out.println("Brand:     "+wynik.getString("BRAND"));
  97.                 System.out.println("Model:     "+wynik.getString("MODEL"));
  98.                 System.out.println("Production Date:     "+wynik.getString("PRODUCTIONDATE"));
  99.                 System.out.println("Status:     "+wynik.getString("STATUS"));
  100.                 System.out.println("============================================================");
  101.             }
  102.  
  103.             wynik.close();
  104.             statement.close();
  105.             connect.close();
  106.         }catch(Exception e){
  107.             e.printStackTrace();
  108.         }
  109.     }
  110. }
Add Comment
Please, Sign In to add comment