Advertisement
Guest User

dla Bartusia

a guest
Jan 1st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.87 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package baza_danych;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.sql.ResultSet;
  13. import java.util.Scanner;
  14. /**
  15. *
  16. * @author Maciek
  17. */
  18. public class Baza_danych {
  19.  
  20. /**
  21. * @param args the command line arguments
  22. */
  23. public static void main(String[] args) {
  24. String x; //w nim zapiszemy swoje imie
  25.  
  26. Scanner odczyt = new Scanner(System.in); //obiekt do odebrania danych od użytkownika
  27. System.out.println("Wybierz liczbe");
  28.  
  29. x = odczyt.nextLine();
  30.  
  31.  
  32. // TODO code application logic here
  33. switch(x) {
  34. case ("0"):
  35. try{
  36. String host = "jdbc:sqlserver://localhost:1433;databaseName=przychodnia";
  37. String uzytkownik = "sa";
  38. String haslo = "kasiaz";
  39. Connection con = DriverManager.getConnection(host,uzytkownik,haslo);
  40.  
  41. Statement stmt = con.createStatement();
  42. String SQL = "SELECT * FROM lekarz";
  43. ResultSet rs = stmt.executeQuery(SQL);
  44.  
  45. while (rs.next()){
  46. int nr_lekarza = rs.getInt("nr_lekarza");
  47. String imie = rs.getString("Imie");
  48. String nazwisko = rs.getString("Nazwisko");
  49. String telefon = rs.getString("telefon");
  50. String dostepnosc = rs.getString("dostepnosc");
  51.  
  52. String p = nr_lekarza + " " + imie + " " + nazwisko + " " + telefon + " " + dostepnosc;
  53. System.out.println(p);
  54. }
  55.  
  56. } catch(SQLException err){
  57. System.out.println(err.getMessage());
  58. }
  59. break;
  60.  
  61. case ("1"):
  62. System.out.println("wybrales 1");
  63. break;
  64. case ("2"):
  65. try{
  66. String host = "jdbc:sqlserver://localhost:1433;databaseName=przychodnia";
  67. String uzytkownik = "sa";
  68. String haslo = "kasiaz";
  69. Connection con = DriverManager.getConnection(host,uzytkownik,haslo);
  70.  
  71. Statement stmt = con.createStatement();
  72. String SQL = "SELECT * FROM pacjent";
  73. ResultSet rs = stmt.executeQuery(SQL);
  74.  
  75. while (rs.next()){
  76. int nr_ubezpieczenia = rs.getInt("nr_ubezpieczenia");
  77. String imie = rs.getString("Imie");
  78. String nazwisko = rs.getString("Nazwisko");
  79. String telefon = rs.getString("telefon");
  80. String adres = rs.getString("adres");
  81.  
  82. String p = nr_ubezpieczenia + " " + imie + " " + nazwisko + " " + telefon + " " + adres;
  83. System.out.println(p);
  84. }
  85.  
  86. } catch(SQLException err){
  87. System.out.println(err.getMessage());
  88. }
  89. break;
  90. case ("3"):
  91. try{
  92. String host = "jdbc:sqlserver://localhost:1433;databaseName=przychodnia";
  93. String uzytkownik = "sa";
  94. String haslo = "kasiaz";
  95. Connection con = DriverManager.getConnection(host,uzytkownik,haslo);
  96.  
  97. Statement stmt = con.createStatement();
  98. String SQL = "SELECT * FROM choroba";
  99. ResultSet rs = stmt.executeQuery(SQL);
  100.  
  101. while (rs.next()){
  102. int nr_choroby = rs.getInt("nr_choroby");
  103. String objawy = rs.getString("objawy");
  104. String nazwa = rs.getString("Nazwa");
  105. String krotki_opis = rs.getString("krotki_opis");
  106. int id_leczenie = rs.getInt("id_leczenie");
  107. int id_lecz = rs.getInt("id_lecz");
  108.  
  109. String p = nr_choroby + " " + objawy + " " + nazwa + " " + krotki_opis + " " + id_leczenie + " " + id_lecz;
  110. System.out.println(p);
  111. }
  112.  
  113. } catch(SQLException err){
  114. System.out.println(err.getMessage());
  115. }
  116. case("4"):
  117. try{
  118. String host = "jdbc:sqlserver://localhost:1433;databaseName=przychodnia";
  119. String uzytkownik = "sa";
  120. String haslo = "kasiaz";
  121. Connection con = DriverManager.getConnection(host,uzytkownik,haslo);
  122.  
  123. Statement stmt = con.createStatement();
  124. String SQL = "SELECT * FROM leczenie";
  125. ResultSet rs = stmt.executeQuery(SQL);
  126.  
  127. while (rs.next()){
  128. int nr_choroby = rs.getInt("id_leczenia");
  129. String objawy = rs.getString("zalecanie");
  130. String nazwa = rs.getString("ile");
  131. String krotki_opis = rs.getString("jak_czesto");
  132.  
  133. String p = nr_choroby + " " + objawy + " " + nazwa + " " + krotki_opis;
  134. System.out.println(p);
  135. }
  136.  
  137. } catch(SQLException err){
  138. System.out.println(err.getMessage());
  139. }
  140. break;
  141. case ("5"):
  142. try{
  143. String w;
  144. w = odczyt.nextLine();
  145. String host = "jdbc:sqlserver://localhost:1433;databaseName=przychodnia";
  146. String uzytkownik = "sa";
  147. String haslo = "kasiaz";
  148. Connection con = DriverManager.getConnection(host,uzytkownik,haslo);
  149.  
  150. Statement stmt = con.createStatement();
  151.  
  152. String SQL = "DELETE FROM leczenie WHERE nr_choroby = '"+w+"'";
  153. stmt.executeUpdate(SQL);
  154.  
  155.  
  156.  
  157.  
  158. } catch(SQLException err){
  159. System.out.println(err.getMessage());
  160. }
  161. break;
  162. case ("6"):
  163. try{
  164. String nr_ubezpieczenia;
  165. nr_ubezpieczenia = odczyt.nextLine();
  166. String imie;
  167. imie = odczyt.nextLine();
  168. String nazwisko;
  169. nazwisko = odczyt.nextLine();
  170. String telefon;
  171. telefon = odczyt.nextLine();
  172. String adres;
  173. adres = odczyt.nextLine();
  174. String host = "jdbc:sqlserver://localhost:1433;databaseName=przychodnia";
  175. String uzytkownik = "sa";
  176. String haslo = "kasiaz";
  177. Connection con = DriverManager.getConnection(host,uzytkownik,haslo);
  178.  
  179. Statement stmt = con.createStatement();
  180. String SQL = "INSERT INTO pacjent VALUES ('"+nr_ubezpieczenia+"', '"+imie+"','"+nazwisko+"','"+telefon+"','"+adres+"')";
  181. stmt.executeUpdate(SQL);
  182. System.out.println("Inserted records into the table...");
  183.  
  184.  
  185.  
  186.  
  187. } catch(SQLException err){
  188. System.out.println(err.getMessage());
  189. }
  190. break;
  191. case("7"):
  192. try{
  193. String id_wizyty;
  194. id_wizyty = odczyt.nextLine();
  195. String data;
  196. data = odczyt.nextLine();
  197. String choroba;
  198. choroba = odczyt.nextLine();
  199. String nr_ubezpieczenia_pacjenta;
  200. nr_ubezpieczenia_pacjenta = odczyt.nextLine();
  201. String id_lekarza;
  202. id_lekarza = odczyt.nextLine();
  203. String id_choroby;
  204. id_choroby = odczyt.nextLine();
  205. String host = "jdbc:sqlserver://localhost:1433;databaseName=przychodnia";
  206. String uzytkownik = "sa";
  207. String haslo = "kasiaz";
  208. Connection con = DriverManager.getConnection(host,uzytkownik,haslo);
  209.  
  210. Statement stmt = con.createStatement();
  211. String SQL = "INSERT INTO pacjent VALUES ('"+id_wizyty+"', '"+data+"','"+choroba+"','"+nr_ubezpieczenia_pacjenta+"','"+id_lekarza+"','"+id_choroby+"')";
  212. stmt.executeUpdate(SQL);
  213. System.out.println("Inserted records into the table...");
  214.  
  215.  
  216.  
  217.  
  218. } catch(SQLException err){
  219. System.out.println(err.getMessage());
  220. }
  221. break;
  222. case("8"):
  223. try{
  224. String set;
  225. set = odczyt.nextLine();
  226. String wartosc;
  227. wartosc = odczyt.nextLine();
  228. String warunek;
  229. warunek = odczyt.nextLine();
  230. String wartosc_war;
  231. wartosc_war = odczyt.nextLine();
  232.  
  233. String host = "jdbc:sqlserver://localhost:1433;databaseName=przychodnia";
  234. String uzytkownik = "sa";
  235. String haslo = "kasiaz";
  236. Connection con = DriverManager.getConnection(host,uzytkownik,haslo);
  237.  
  238. Statement stmt = con.createStatement();
  239. String SQL = "update pacjent set imie = '"+wartosc+"' where adres = '"+wartosc_war+"'";
  240. stmt.executeUpdate(SQL);
  241. System.out.println("Inserted records into the table...");
  242.  
  243.  
  244.  
  245.  
  246. } catch(SQLException err){
  247. System.out.println(err.getMessage());
  248. }
  249. break;
  250. default:
  251. System.out.println("zle");
  252. }
  253.  
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement