Advertisement
Guest User

asdasdsda

a guest
Nov 14th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.31 KB | None | 0 0
  1.  
  2. package lotto_bazy;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.sql.*;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Scanner;
  12.  
  13.  
  14.  
  15. public class Database {
  16.  
  17. public static Connection conn;
  18. public static Statement stat;
  19. /////////////////////////////////////////////////////////////////////////////////////////////// MAIN
  20.  
  21. public static void main(String[] args) {
  22.  
  23. String baza = "Nagrody";
  24. // Operacje oper= new Operacje();
  25.  
  26.  
  27. try {
  28. Class.forName("org.sqlite.JDBC");
  29. } catch (ClassNotFoundException e) {
  30. System.err.println("Brak sterownika JDBC");
  31. e.printStackTrace();
  32. }
  33.  
  34. try {
  35. conn = DriverManager.getConnection("jdbc:sqlite:"+baza+".db");
  36. stat = conn.createStatement();
  37. } catch (SQLException e) {
  38. System.err.println("Problem z otwarciem polaczenia");
  39. e.printStackTrace();
  40. }
  41.  
  42. Database start = new Database();
  43. // start.dropDB();
  44.  
  45. System.out.println("--Wcisnij [ 1 ] zeby stworzyc tabele z rekordami lub [ 2 ] aby wyjsc do menu--");
  46.  
  47. Scanner odczyt = Scanner(System.in);
  48.  
  49. String x= odczyt.nextLine();
  50.  
  51. if(x == "1"){
  52. start.createTabele();
  53. start.read("lista_lotto.txt");
  54. }
  55.  
  56. if(x == "2"){
  57. start.Menu();
  58. }
  59.  
  60. }
  61.  
  62.  
  63. // throws SQLException
  64. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. void dropDB() {
  66. String win = "DROP TABLE if EXISTS nagroda;";
  67.  
  68. try{
  69. stat.execute(win);
  70. }
  71. catch (SQLException e) {
  72. e.printStackTrace();
  73. System.out.println("Blad przy tworzeniu tabeli!");
  74. }
  75. }
  76.  
  77. // TWORZENIE TABELI
  78.  
  79. public boolean createTabele(){
  80.  
  81. String createNagroda="CREATE TABLE IF NOT EXISTS nagroda(id INTEGER PRIMARY KEY AUTOINCREMENT, kwota_wygranej DECIMAL(20,2), miasto VARCHAR(20) , ulica VARCHAR(40) default NULL, data VARCHAR(20));";
  82. // String createPunkt= "CREATE TABLE IF NOT EXISTS pkt_obslugi(id INTEGER PRIMARY KEY AUTOINCREMENT, miasto VARCHAR(30) , ulica VARCHAR(40) UNIQUE);";
  83.  
  84. String zeroweK= "UPDATE nagroda SET ulica = 'BRAK' WHERE ulica IS NULL;";
  85.  
  86. try{
  87. stat.execute(createNagroda);
  88. stat.execute(zeroweK);
  89. System.out.println("Stworzono tabele \n");
  90.  
  91. return true;
  92. } catch (SQLException e) {
  93. e.printStackTrace();
  94. System.out.println("Blad przy tworzeniu tabeli!");
  95. return false;
  96. }
  97.  
  98. }
  99.  
  100.  
  101. // ZCZYTYWANIE Z PLIKU
  102.  
  103. String[][] read(String path) {
  104.  
  105. BufferedReader br = null;
  106. List<String> line = new ArrayList<>();
  107.  
  108. String baza = "Nagrody";
  109. try {
  110. Class.forName("org.sqlite.JDBC");
  111. } catch (ClassNotFoundException e) {
  112. System.err.println("Brak sterownika JDBC");
  113. e.printStackTrace();
  114. }
  115.  
  116. try {
  117. conn = DriverManager.getConnection("jdbc:sqlite:"+baza+".db");
  118. stat = conn.createStatement();
  119. } catch (SQLException e) {
  120. System.err.println("Problem z otwarciem polaczenia");
  121. e.printStackTrace();
  122. }
  123.  
  124.  
  125. try {
  126. String CurrentLine;
  127.  
  128. br = new BufferedReader(new FileReader(path));
  129.  
  130. while ((CurrentLine = br.readLine()) != null) {
  131. line.add(CurrentLine);
  132. }
  133.  
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. } finally {
  137. try {
  138. if (br != null) {
  139. br.close();
  140. }
  141. } catch (IOException ex) {
  142. ex.printStackTrace();
  143. }
  144. }
  145.  
  146.  
  147. return sortFile(line);
  148. }
  149.  
  150.  
  151. private String[][] sortFile(List<String> lista)
  152. {
  153. String[][] line = new String[lista.size()][5];
  154. ;
  155. for(int i = 0; i < lista.size(); i++) {
  156. String[] zmienna = lista.get(i).split(" -- ");
  157. line[i][0] = zmienna[0];
  158. line[i][1] = zmienna[1];
  159. line[i][2] = zmienna[2];
  160. line[i][3] = zmienna[3];
  161. line[i][4] = zmienna[4];
  162.  
  163. try {
  164. // String x=zmienna[0];
  165. String miasto= zmienna[1];
  166. String ulica= zmienna[2];
  167. String data= zmienna[3];
  168. String money= zmienna[4];
  169.  
  170. String tab1=
  171. "insert into nagroda ( kwota_wygranej, miasto, ulica, data) values ('"+money+"','" +miasto+ "','"+ulica+"','"+data+"');";
  172.  
  173. stat.execute(tab1);
  174.  
  175. } catch (SQLException e) {
  176. System.err.println("Blad przy wstawianiu nagrody");
  177. e.printStackTrace();
  178. System.out.println(line[i][0]);
  179. }
  180.  
  181.  
  182. }
  183.  
  184. return line;
  185. }
  186.  
  187.  
  188.  
  189.  
  190. private static Scanner Scanner(InputStream in) {
  191. // TODO Auto-generated method stub
  192. return null;
  193. }
  194. /////////////////////////////////////////////////////////////////////////////////////////////////////
  195. public void Menu(){
  196.  
  197.  
  198. System.out.print(
  199. "0- Wyjscie z programu \n"
  200. + "1- Wyswietlenie wszystkich wynikow \n"
  201. + "2- Wybor, jakie wyniki chcesz wyswietlic \n"
  202. + "3- Miasto z najwieksza liczba wygranych \n"
  203. + "4- Adres punktu gdzie bylo najwiecej wygranych \n"
  204. + "5- Porownanie miast i ich zsumowane wysokosci nagrod \n"
  205. + "6- Wyswietlenie nowo powstalych tabel \n"
  206. + "7- \n"
  207. + "8- Dodaj nowy rekord \n"
  208. );
  209.  
  210. Scanner odczyt= new Scanner(System.in);
  211.  
  212. int i;
  213. i= odczyt.nextInt();
  214.  
  215.  
  216. switch (i){
  217. case(1):{ case1(); }
  218.  
  219. case(2):{ case2(); }
  220.  
  221. case(3):{ case3(); }
  222.  
  223. case(4):{ case4(); }
  224.  
  225. case(5):{ case5(); }
  226.  
  227. case(6):{ case6(); }
  228.  
  229. case(7):{ case7(); }
  230.  
  231. case(8):{ case8(); }
  232.  
  233. case(9):{ case9(); }
  234.  
  235. case(0):{ closeConnection(); System.exit(0); }
  236.  
  237. }
  238.  
  239. }
  240.  
  241.  
  242. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  243.  
  244. void case1(){
  245. try {
  246. ResultSet result = stat.executeQuery("SELECT * FROM nagroda;");
  247. int id;
  248. String kwota_wygranej;
  249. String miasto;
  250. String ulica;
  251. String data;
  252.  
  253. while(result.next()) {
  254. id = result.getInt("id");
  255. kwota_wygranej = result.getString("kwota_wygranej");
  256. miasto = result.getString("miasto");
  257. ulica = result.getString("ulica");
  258. data = result.getString("data");
  259. System.out.println("id="+id+" Wygrana = "+kwota_wygranej+" Miasto = "+miasto+" Ulica = "+ulica+" Data = "+data+"\n");
  260. }
  261. } catch (SQLException e) {
  262. System.err.println("Blad przy wykonywaniu SELECT");
  263. e.printStackTrace();
  264. }
  265. Menu();
  266. }
  267. //-----------------------------------------------------------------------------------------------
  268. void case2(){
  269.  
  270. System.out.println("Jakie wyniki ma wyswietlic zapytanie SELECT? Zapytanie ma byc napisane jak komenda w sql! \n");
  271.  
  272. Scanner odczyt= new Scanner(System.in);
  273. String x= odczyt.nextLine();
  274.  
  275. try {
  276. ResultSet result = stat.executeQuery(x);
  277. int id;
  278. String kwota_wygranej;
  279. String miasto;
  280. String ulica;
  281. String data;
  282.  
  283. while(result.next()) {
  284.  
  285. }
  286. } catch (SQLException e) {
  287. System.err.println("Blad przy wykonywaniu SELECT");
  288. e.printStackTrace();
  289. }
  290.  
  291.  
  292. }
  293. //---------------------------------------------
  294. void case3(){
  295.  
  296. }
  297. //---------------------------------------------
  298. void case4(){
  299.  
  300. }
  301. //---------------------------------------------
  302. void case5(){
  303.  
  304. }
  305. //---------------------------------------------
  306. void case6(){
  307.  
  308. }
  309. //---------------------------------------------
  310. void case7(){
  311.  
  312. }
  313. //---------------------------------------------
  314. void case8(){
  315.  
  316. }
  317.  
  318. void case9(){
  319. System.out.println("Podaj dane do stworzenia nowego rekordu \n");
  320. Scanner odczyt= new Scanner(System.in);
  321.  
  322. System.out.println("Podaj kwote wygranej: \n");
  323. double kwota=odczyt.nextDouble();
  324. System.out.println("Podaj miasto wygranej: \n");
  325. String miasto=odczyt.nextLine();
  326. System.out.println("Podaj ulice wygranej: \n");
  327. String ulica=odczyt.nextLine();
  328. System.out.println("Podaj date wygranej: (format dd-mm-yy \n");
  329. String data=odczyt.nextLine();
  330. String sql=
  331. "insert into nagroda ( kwota_wygranej, miasto, ulica, data) values ('"+kwota+"','" +miasto+ "','"+ulica+"','"+data+"');";
  332. // String sql2="SELECT * FROM nagroda WHERE ulica="+ulica+" and data="+data+";";
  333. try {
  334. stat.execute(sql);
  335. System.out.println("Dodano rekord: \n");
  336. // stat.execute(sql2);
  337. } catch (SQLException e) {
  338. System.err.println("Blad przy dodawaniu nagrody");
  339. e.printStackTrace();
  340. }
  341. Menu();
  342. }
  343.  
  344.  
  345. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  346. public void closeConnection() {
  347. try {
  348. conn.close();
  349. } catch (SQLException e) {
  350. System.err.println("Problem z zamknieciem polaczenia");
  351. e.printStackTrace();
  352. }
  353. }
  354.  
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement