Advertisement
Guest User

Untitled

a guest
Mar 17th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.47 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,Temperatur,Vertype) 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. stmt = conn.createStatement();
  245. ResultSet rs = stmt.executeQuery("select MAX(MalID) as getMalID from mal");
  246.  
  247. if(rs.next()){
  248. //Retrieve by column name
  249. int malId1 = rs.getInt("getMalID");
  250. malId = malId1;
  251. }
  252. }catch(SQLException se){
  253. //Handle errors for JDBC
  254. System.out.println("nope");
  255. se.printStackTrace();
  256. }catch(Exception e){
  257. //Handle errors for Class
  258. e.printStackTrace();
  259. }
  260.  
  261. // insert into mal table
  262. //ta vare på malid ved sql spørring og bruk til når ovelse skal insertes
  263. }
  264.  
  265. // sql for ovelse ------------------------------------------------------------------------
  266. try{
  267. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `ovelse` (Navn,OktNr,Beskrivelse,ID1,ID2,MalID) VALUE (?,?,?,?,?,?)");
  268. pstmt.setString(1, navn);
  269. pstmt.setInt(2, oktnr);
  270. pstmt.setString(3,beskrivelse);
  271. pstmt.setInt(4,id1);
  272. pstmt.setInt(5,id2);
  273. pstmt.setInt(6,malId);
  274. pstmt.executeUpdate();
  275.  
  276. }catch(SQLException se){
  277. //Handle errors for JDBC
  278. System.out.println("nope");
  279. se.printStackTrace();
  280. }catch(Exception e){
  281. //Handle errors for Class
  282. e.printStackTrace();
  283. }
  284.  
  285. //Bunn av koden
  286. System.out.print("Ny øvelse?(Ja/ferdig): ");
  287. handling = input.next();
  288. }
  289. }
  290.  
  291.  
  292. public static void hentLogg(Connection conn, Statement stmt){
  293. //STEP 4: Execute a query
  294. try{
  295. System.out.println("Creating statement...");
  296. stmt = conn.createStatement();
  297. String sql;
  298. sql = "SELECT Navn, OktNr FROM ovelse WHERE OktNr = '1'";
  299. ResultSet rs = stmt.executeQuery(sql);
  300.  
  301. while(rs.next()){
  302. //Retrieve by column name
  303. String navn = rs.getString("Navn");
  304. int oktnr = rs.getInt("OktNr");
  305.  
  306.  
  307. //Display values
  308. System.out.print("navn: " + navn);
  309. System.out.println(", oktnr: " + oktnr);
  310. }
  311. System.out.println("Skipped while");
  312. }
  313. catch(SQLException se){
  314. //Handle errors for JDBC
  315. System.out.println("nope");
  316. se.printStackTrace();
  317. }
  318. }
  319. public static void hentBeste(){
  320.  
  321. }
  322. // + Mange flere metoder ..
  323.  
  324. //--------------------------------------MAIN---------------------------------------------------
  325. //------------------------------ Connection-Oppsett -------------------------------------------
  326. public static void main(String[] args) {
  327. Connection conn = null;
  328. Statement stmt = null;
  329. try{
  330. //STEP 2: Register JDBC driver
  331. Class.forName("com.mysql.jdbc.Driver");
  332.  
  333. //STEP 3: Open a connection
  334. System.out.println("Connecting to database...");
  335. conn = DriverManager.getConnection(DB_URL,USER,PASS); //Connects with DB(URL, Name & Password)
  336.  
  337. //---------------------------------- Logic(for tekst-UI) ------------------------------------
  338. //Lag loop som konstant spør om hva som skal gjøres + Text-input konstant
  339. //Kjører riktig funksjon med riktige parametere avhengig av input
  340. Scanner input = new Scanner(System.in);
  341. String handling;
  342.  
  343. while (true){
  344. System.out.print("Skriv inn handling(ny/hent/beste/avslutt): ");
  345. handling = input.next();
  346. if (handling.equals("ny")){
  347. nyTrening(conn, stmt);
  348. }else if(handling.equals("hent")){
  349. hentLogg(conn, stmt);
  350. }else if(handling.equals("beste")){
  351. hentBeste();
  352. }else if(handling.equals("avslutt")){
  353. break;
  354. }else{
  355. System.out.println("Feil. Skriv inn 'ny, 'hent, 'beste' eller 'avslutt'");
  356. continue;
  357. }
  358. }
  359.  
  360. // --------------------------- Clean-up / End--------------------------------------
  361. input.close();
  362. stmt.close();
  363. conn.close();
  364. }catch(SQLException se){
  365. //Handle errors for JDBC
  366. se.printStackTrace();
  367. }catch(Exception e){
  368. //Handle errors for Class.forName
  369. e.printStackTrace();
  370. }finally{
  371. //finally block used to close resources
  372. try{
  373. if(stmt!=null)
  374. stmt.close();
  375. }catch(SQLException se2){
  376. }// nothing we can do
  377. try{
  378. if(conn!=null)
  379. conn.close();
  380. }catch(SQLException se){
  381. se.printStackTrace();
  382. }//end finally try
  383. }//end try
  384. System.out.println("So long, Suckers!");
  385. }//End Main
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement