Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.xml.transform.Result;
- import java.io.File;
- import java.io.FileInputStream;
- import java.sql.*;
- public class SQL {
- public String tableName = "CarRental";
- public Connection connection = getConnection(tableName);
- public SQL(){
- createTable(connection, tableName);
- }
- private Connection getConnection(String tableName){
- Connection connection = null;
- try{
- Class.forName("org.sqlite.JDBC");
- connection = DriverManager.getConnection("jdbc:sqlite:" + tableName + ".db");
- }catch(Exception e){
- System.out.println("Napotkano blad podczas polaczenia.");
- e.printStackTrace();
- }
- return connection;
- }
- public void createTable(Connection connection, String tableName) {
- Statement statement = null;
- try {
- statement = connection.createStatement();
- String query = "CREATE TABLE " + tableName + " (ID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, BRAND TEXT, MODEL TEXT, PRODUCTIONDATE TEXT, STATUS TEXT)";
- statement.executeUpdate(query);
- statement.close();
- connection.close();
- } catch (Exception e) {
- File file = new File("CarRental.db");
- FileInputStream fin;
- try{
- fin = new FileInputStream(file);
- fin.close();
- }catch (Exception x){
- System.out.println("Blad");
- }
- }
- }
- public void addNewCar(String tableName, int id, String carbrand,String carmodel,String produkcja,String status){
- Statement statement = null;
- try{
- Class.forName("org.sqlite.JDBC");
- connection = DriverManager.getConnection("jdbc:sqlite:" + tableName + ".db");
- statement = connection.createStatement();
- String query = "INSERT INTO "+tableName+" VALUES ("+id+", "+"\""+carbrand+"\", \""+carmodel+"\", \""+produkcja+"\", \""+status+"\")";
- statement.executeUpdate(query);
- statement.close();
- connection.close();
- }catch(Exception e){
- System.out.println("Blad");
- e.printStackTrace();
- }
- }
- public void updateTable(String tableName, String poleSzukane, String wartoscSzukana, String poleZmienne, String nowaWartosc){
- Statement statement;
- Connection connection;
- try{
- Class.forName("org.sqlite.JDBC");
- connection = DriverManager.getConnection("jdbc:sqlite:" +tableName+ ".db");
- statement = connection.createStatement();
- String query = "UPDATE " + tableName + " SET " + poleZmienne+ " = \""+nowaWartosc+"\" WHERE "+poleSzukane+" = \""+wartoscSzukana+"\"";
- statement.executeUpdate(query);
- statement.close();
- connection.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- public void search(String element, String tablica, String pole, String wartosc){
- Connection connect = null;
- Statement statement = null;
- try{
- Class.forName("org.sqlite.JDBC");
- connect = DriverManager.getConnection("jdbc:sqlite:"+tablica+".db");
- statement = connect.createStatement();
- String query = "SELECT "+element+" FROM "+tablica+" WHERE "+pole+" = "+"\""+wartosc+"\"";
- ResultSet wynik = statement.executeQuery(query);
- while(wynik.next()){
- int id = wynik.getInt("ID");
- System.out.println("ID: "+id);
- System.out.println("Brand: "+wynik.getString("BRAND"));
- System.out.println("Model: "+wynik.getString("MODEL"));
- System.out.println("Production Date: "+wynik.getString("PRODUCTIONDATE"));
- System.out.println("Status: "+wynik.getString("STATUS"));
- System.out.println("============================================================");
- }
- wynik.close();
- statement.close();
- connect.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment