Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.54 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.*;
  4. import java.util.Scanner;
  5.  
  6. public class Treningslogg {
  7.  
  8. //--------------------------------- Definere Variabler -------------------------------------------
  9. // JDBC driver name and database URL
  10. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  11. static final String DB_URL = "jdbc:mysql://localhost/treningdb";
  12.  
  13. // Database credentials
  14. static final String USER = "root";
  15. static final String PASS = "burger1605";
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. //------------------------------- Metoder for DB-manipulering -------------------------------------
  23. public static void nyTrening(Connection conn, Statement stmt){
  24. Scanner input = new Scanner(System.in);
  25. //Dato
  26. System.out.print("Dato for treningsøkt(yyymmdd): ");
  27. String dato = input.nextLine();
  28. //starttid
  29. System.out.print("Starttid(hh:mm:ss): ");
  30. String starttid = input.next();
  31. //Varighet
  32. System.out.print("Varighet(hh:mm:ss): ");
  33. String varighet = input.next();
  34. //form
  35. System.out.print("Personligform(1 - 10) ");
  36. int form = input.nextInt();
  37. //Prestasjon
  38. System.out.print("Prestasjon(1 - 10): ");
  39. int prestasjon = input.nextInt();
  40. //notat
  41. System.out.print("Notat: ");
  42. String notat = input.next();
  43. Integer oktnr = null;
  44. try{
  45.  
  46. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `treningsokt` (Dato,Starttid,Varighet,PersonligForm,Prestasjon,Notat) VALUE (?,?,?,?,?,?)");
  47. pstmt.setString(1, dato);
  48. pstmt.setString(2, starttid);
  49. pstmt.setString(3,varighet);
  50. pstmt.setInt(4,form );
  51. pstmt.setInt(5,prestasjon );
  52. pstmt.setString(6,notat);
  53. pstmt.executeUpdate();
  54.  
  55. stmt = conn.createStatement();
  56. String sql;
  57. sql = "SELECT OktNr FROM treningsokt WHERE Dato = "+dato+"";
  58.  
  59. ResultSet rs = stmt.executeQuery(sql);
  60.  
  61. while(rs.next()){
  62. //Retrieve by column name
  63. int oktnr1 = rs.getInt("OktNr");
  64. System.out.println("Velykket opprettelse av treningsokt! OktNr: "+oktnr1+"");
  65. oktnr = oktnr1;
  66. }
  67. }catch(SQLException se){
  68. //Handle errors for JDBC
  69. System.out.println("nope");
  70. se.printStackTrace();
  71. }catch(Exception e){
  72. //Handle errors for Class
  73. e.printStackTrace();
  74. }
  75. String handling = input.nextLine();
  76.  
  77.  
  78.  
  79. // --------------------------------------------------------------------
  80. //NY ØVELSE -----------------------------------------------------------
  81. while(!handling.equals("ferdig")){
  82. //Øktnavn
  83. System.out.print("Øktnavn: ");
  84. String navn = input.nextLine();
  85. //ØktBeskrivelse
  86. System.out.print("Beskrivelse: ");
  87. String beskrivelse = input.nextLine();
  88.  
  89. //Inne / Ute -----------------------------------------------------
  90. String luft;
  91. Integer id2 = null;
  92. int tilskuere;
  93. int temperatur;
  94. String vartype;
  95. while (true){
  96. System.out.println("Inne eller ute(inne/ute)? ");
  97. String inne = input.nextLine();
  98. //Om inne:
  99. if(inne.equals("inne")){
  100. System.out.print("ID2: ");
  101. id2 = input.nextInt();
  102. System.out.print("Luft: ");
  103. luft = input.next();
  104. System.out.print("antall tilskuere: ");
  105. tilskuere = input.nextInt();
  106. try{
  107. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `innendors` (ID2,Luft,Tilskuere) VALUE (?,?,?)");
  108. pstmt.setInt(1, id2);
  109. pstmt.setString(2, luft);
  110. pstmt.setInt(3,tilskuere);
  111. pstmt.executeUpdate();
  112.  
  113. }catch(SQLException se){
  114. //Handle errors for JDBC
  115. System.out.println("nope");
  116. se.printStackTrace();
  117. }catch(Exception e){
  118. //Handle errors for Class
  119. e.printStackTrace();
  120. }
  121. break;
  122. //Om ute:
  123. }else if(inne.equals("ute")){
  124. System.out.println("ID2: ");
  125. id2 = input.nextInt();
  126. System.out.println("Temperatur: ");
  127. temperatur = input.nextInt();
  128. System.out.println("Værtype: ");
  129. vartype = input.next();
  130. try{
  131.  
  132. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `utendors` (ID2,Luft,Tilskuere) VALUE (?,?,?)");
  133. pstmt.setInt(1, id2);
  134. pstmt.setInt(2, temperatur);
  135. pstmt.setString(3,vartype);
  136. pstmt.executeUpdate();
  137.  
  138. }catch(SQLException se){
  139. //Handle errors for JDBC
  140. System.out.println("nope");
  141. se.printStackTrace();
  142. }catch(Exception e){
  143. //Handle errors for Class
  144. e.printStackTrace();
  145. }
  146. break;
  147. //Om feil:
  148. }else{
  149. System.out.println("Feil format, prøv igjen!");
  150. continue;
  151. }
  152. }
  153.  
  154. //Type trening: -------------------------------------------------------------------------
  155. int belastning;
  156. int repitisjon;
  157. int sett;
  158.  
  159. int km;
  160. int min;
  161. Integer id1 = null;
  162. while (true){
  163. System.out.print("styrke eller utholdenhet(styrke/utholdenhet)? ");
  164. String styrke = input.next();
  165. //Om Styrke:
  166. if(styrke.equals("styrke")){
  167. System.out.print("ID1: ");
  168. id1 = input.nextInt();
  169. System.out.print("Belastning: ");
  170. belastning = input.nextInt();
  171. System.out.print("Repetisjoner: ");
  172. repitisjon = input.nextInt();
  173. System.out.print("Sett: ");
  174. sett = input.nextInt();
  175. try{
  176.  
  177. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `styrke` (ID1,Belastning,Repitisjon,Sett) VALUE (?,?,?,?)");
  178. pstmt.setInt(1, id1);
  179. pstmt.setInt(2, belastning);
  180. pstmt.setInt(3,repitisjon);
  181. pstmt.setInt(4,sett);
  182. pstmt.executeUpdate();
  183.  
  184. }catch(SQLException se){
  185. //Handle errors for JDBC
  186. System.out.println("nope");
  187. se.printStackTrace();
  188. }catch(Exception e){
  189. //Handle errors for Class
  190. e.printStackTrace();
  191. }
  192. break;
  193. //Om Utholdenhet:
  194. }else if(styrke.equals("utholdenhet")){
  195. System.out.print("Belastning: ");
  196. belastning = input.nextInt();
  197. System.out.print("Repetisjoner: ");
  198. repitisjon = input.nextInt();
  199. System.out.print("Sett: ");
  200. sett = input.nextInt();
  201. System.out.print("Km: ");
  202. km = input.nextInt();
  203. System.out.print("Min: ");
  204. min = input.nextInt();
  205. try{
  206. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `utholdenhet` (ID1,Belastning,Repitisjoner,Sett,Km,Min) VALUE (?,?,?,?,?,?)");
  207. pstmt.setInt(1, id2);
  208. pstmt.setInt(2, belastning);
  209. pstmt.setInt(3,repitisjon);
  210. pstmt.setInt(4,sett);
  211. pstmt.setInt(5,km);
  212. pstmt.setInt(6,min);
  213. pstmt.executeUpdate();
  214.  
  215. }catch(SQLException se){
  216. //Handle errors for JDBC
  217. System.out.println("nope");
  218. se.printStackTrace();
  219. }catch(Exception e){
  220. //Handle errors for Class
  221. e.printStackTrace();
  222. }
  223. break;
  224. //Om feil:
  225. }else{
  226. System.out.println("Feil format, prøv igjen!");
  227. continue;
  228. }
  229.  
  230. }
  231. //Mål eller ikke mål-----------------------------------------------------------------------
  232. System.out.print("Er øvelsen et mål(ja/nai)?");
  233. String mal = input.next();
  234. Integer malId = null;
  235. if(mal.equals("ja")){
  236. System.out.print("Dato(yyyymmdd): ");
  237. String malDato = input.next();
  238. try{
  239. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `mal` (Dato) VALUE (?)");
  240. pstmt.setString(1, malDato);
  241. pstmt.executeUpdate();
  242.  
  243.  
  244.  
  245. }catch(SQLException se){
  246. //Handle errors for JDBC
  247. System.out.println("nope");
  248. se.printStackTrace();
  249. }catch(Exception e){
  250. //Handle errors for Class
  251. e.printStackTrace();
  252. }
  253.  
  254. try {
  255. stmt = conn.createStatement();
  256. String sql;
  257. sql = "SELECT LAST(MalID) FROM mal";
  258.  
  259. ResultSet rs = stmt.executeQuery(sql);
  260.  
  261. while(rs.next()){
  262. //Retrieve by column name
  263. int malId1 = rs.getInt("MalID");
  264. malId = malId1;
  265. }
  266. } catch (SQLException e) {
  267. e.printStackTrace();
  268. }
  269.  
  270. // insert into mal table
  271. //ta vare på malid ved sql spørring og bruk til når ovelse skal insertes
  272. }
  273.  
  274. // sql for ovelse ------------------------------------------------------------------------
  275. try{
  276. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `ovelse` (Navn,OktNr,Beskrivelse,ID1,ID2,MalID) VALUE (?,?,?,?,?,?)");
  277. pstmt.setString(1, navn);
  278. pstmt.setInt(2, oktnr);
  279. pstmt.setString(3,beskrivelse);
  280. pstmt.setInt(4,id1);
  281. pstmt.setInt(5,id2);
  282. pstmt.setInt(6,malId);
  283. pstmt.executeUpdate();
  284.  
  285. }catch(SQLException se){
  286. //Handle errors for JDBC
  287. System.out.println("nope");
  288. se.printStackTrace();
  289. }catch(Exception e){
  290. //Handle errors for Class
  291. e.printStackTrace();
  292. }
  293.  
  294. //Bunn av koden
  295. handling = input.nextLine();
  296. }
  297. input.close();
  298. }
  299.  
  300.  
  301. public static void hentLogg(Connection conn, Statement stmt){
  302. //STEP 4: Execute a query
  303. try{
  304. System.out.println("Creating statement...");
  305. stmt = conn.createStatement();
  306. String sql;
  307. sql = "SELECT Navn, OktNr FROM ovelse WHERE OktNr = '1'";
  308. ResultSet rs = stmt.executeQuery(sql);
  309.  
  310. while(rs.next()){
  311. //Retrieve by column name
  312. String navn = rs.getString("Navn");
  313. int oktnr = rs.getInt("OktNr");
  314.  
  315.  
  316. //Display values
  317. System.out.print("navn: " + navn);
  318. System.out.println(", oktnr: " + oktnr);
  319. }
  320. System.out.println("Skipped while");
  321. }
  322. catch(SQLException se){
  323. //Handle errors for JDBC
  324. System.out.println("nope");
  325. se.printStackTrace();
  326. }
  327. }
  328. public static void hentBeste(){
  329.  
  330. }
  331. // + Mange flere metoder ..
  332.  
  333. //--------------------------------------MAIN---------------------------------------------------
  334. //------------------------------ Connection-Oppsett -------------------------------------------
  335. public static void main(String[] args) {
  336. Connection conn = null;
  337. Statement stmt = null;
  338. try{
  339. //STEP 2: Register JDBC driver
  340. Class.forName("com.mysql.jdbc.Driver");
  341.  
  342. //STEP 3: Open a connection
  343. System.out.println("Connecting to database...");
  344. conn = DriverManager.getConnection(DB_URL,USER,PASS); //Connects with DB(URL, Name & Password)
  345.  
  346. //---------------------------------- Logic(for tekst-UI) ------------------------------------
  347. //Lag loop som konstant spør om hva som skal gjøres + Text-input konstant
  348. //Kjører riktig funksjon med riktige parametere avhengig av input
  349. Scanner input = new Scanner(System.in);
  350. String handling;
  351.  
  352. while (true){
  353. System.out.print("Skriv inn handling(ny/hent/beste/avslutt): ");
  354. handling = input.next();
  355. if (handling.equals("ny")){
  356. nyTrening(conn, stmt);
  357. }else if(handling.equals("hent")){
  358. hentLogg(conn, stmt);
  359. }else if(handling.equals("beste")){
  360. hentBeste();
  361. }else if(handling.equals("avslutt")){
  362. break;
  363. }else{
  364. System.out.println("Feil. Skriv inn 'ny, 'hent, 'beste' eller 'avslutt'");
  365. continue;
  366. }
  367. }
  368.  
  369. // --------------------------- Clean-up / End--------------------------------------
  370. input.close();
  371. stmt.close();
  372. conn.close();
  373. }catch(SQLException se){
  374. //Handle errors for JDBC
  375. se.printStackTrace();
  376. }catch(Exception e){
  377. //Handle errors for Class.forName
  378. e.printStackTrace();
  379. }finally{
  380. //finally block used to close resources
  381. try{
  382. if(stmt!=null)
  383. stmt.close();
  384. }catch(SQLException se2){
  385. }// nothing we can do
  386. try{
  387. if(conn!=null)
  388. conn.close();
  389. }catch(SQLException se){
  390. se.printStackTrace();
  391. }//end finally try
  392. }//end try
  393. System.out.println("So long, Suckers!");
  394. }//End Main
  395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement