Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 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. try{
  44.  
  45. PreparedStatement pstmt = conn.prepareStatement("INSERT INTO `treningsokt` (Dato,Starttid,Varighet,PersonligForm,Prestasjon,Notat) VALUE (?,?,?,?,?,?)");
  46. pstmt.setString(1, dato);
  47. pstmt.setString(2, starttid);
  48. pstmt.setString(3,varighet);
  49. pstmt.setInt(4,form );
  50. pstmt.setInt(5,prestasjon );
  51. pstmt.setString(6,notat);
  52. pstmt.executeUpdate();
  53.  
  54. stmt = conn.createStatement();
  55. String sql;
  56. sql = "SELECT OktNr FROM treningsokt WHERE Dato = "+dato+"";
  57.  
  58. ResultSet rs = stmt.executeQuery(sql);
  59.  
  60. while(rs.next()){
  61. //Retrieve by column name
  62. int oktnr = rs.getInt("OktNr");
  63. System.out.println("Velykket opprettelse av treningsokt! OktNr: "+oktnr+"");
  64. }
  65. }catch(SQLException se){
  66. //Handle errors for JDBC
  67. System.out.println("nope");
  68. se.printStackTrace();
  69. }catch(Exception e){
  70. //Handle errors for Class
  71. e.printStackTrace();
  72. }
  73. String handling = input.nextLine();
  74. //NY ØVELSE
  75. while(!handling.equals("ferdig")){
  76. //Øktnavn
  77. System.out.print("Øktnavn");
  78. String navn = input.nextLine();
  79. //ØktBeskrivelse
  80. System.out.print("Beskrivelse");
  81. String beskrivelse = input.nextLine();
  82.  
  83. //Inne / Ute
  84. String luft;
  85. int tilskuere;
  86. int temperatur;
  87. String vartype;
  88. while (true){
  89. System.out.println("Inne eller ute(inne/ute)? ");
  90. String inne = input.nextLine();
  91. //Om inne:
  92. if(inne.equals("inne")){
  93. System.out.println("Luft: ");
  94. luft = input.nextLine();
  95. System.out.println("antall tilskuere: ");
  96. tilskuere = input.nextInt();
  97. break;
  98. //Om ute:
  99. }else if(inne.equals("ute")){
  100. System.out.println("Temperatur: ");
  101. temperatur = input.nextInt();
  102. System.out.println("Værtype: ");
  103. vartype = input.nextLine();
  104. break;
  105. //Om feil:
  106. }else{
  107. System.out.println("Feil format, prøv igjen!");
  108. continue;
  109. }
  110. }
  111.  
  112. //Type trening:
  113. int belastning;
  114. int repetisjon;
  115. int sett;
  116.  
  117. int km;
  118. int min;
  119. while (true){
  120. System.out.println("styrke eller utholdenhet(styrke/utholdenhet)? ");
  121. String inne = input.nextLine();
  122. //Om Styrke:
  123. if(inne.equals("inne")){
  124. System.out.println("Belastning: ");
  125. belastning = input.nextInt();
  126. System.out.println("Repetisjoner: ");
  127. repetisjon = input.nextInt();
  128. System.out.println("Sett: ");
  129. sett = input.nextInt();
  130. break;
  131. //Om Utholdenhet:
  132. }else if(inne.equals("ute")){
  133. System.out.println("Belastning: ");
  134. belastning = input.nextInt();
  135. System.out.println("Repetisjoner: ");
  136. repetisjon = input.nextInt();
  137. System.out.println("Sett: ");
  138. sett = input.nextInt();
  139. System.out.println("Km: ");
  140. km = input.nextInt();
  141. System.out.println("Min: ");
  142. min = input.nextInt();
  143. break;
  144. //Om feil:
  145. }else{
  146. System.out.println("Feil format, prøv igjen!");
  147. continue;
  148. }
  149.  
  150. }
  151. //Mål eller ikke mål
  152. System.out.print("Er øvelsen et mål(ja/nai)?");
  153. String mal = input.nextLine();
  154. int malId;
  155. if(mal.equals("ja")){
  156. System.out.print("Dato(yyyymmdd): ");
  157. String malDato = input.nextLine();
  158. // insert into mal table
  159. //ta vare på malid ved sql spørring og bruk til når ovelse skal insertes
  160. }
  161.  
  162.  
  163.  
  164.  
  165. handling = input.nextLine();
  166. }
  167. input.close();
  168. }
  169.  
  170.  
  171. public static void hentLogg(Connection conn, Statement stmt){
  172. //STEP 4: Execute a query
  173. try{
  174. System.out.println("Creating statement...");
  175. stmt = conn.createStatement();
  176. String sql;
  177. sql = "SELECT Navn, OktNr FROM ovelse WHERE OktNr = '1'";
  178. ResultSet rs = stmt.executeQuery(sql);
  179.  
  180. while(rs.next()){
  181. //Retrieve by column name
  182. String navn = rs.getString("Navn");
  183. int oktnr = rs.getInt("OktNr");
  184.  
  185.  
  186. //Display values
  187. System.out.print("navn: " + navn);
  188. System.out.println(", oktnr: " + oktnr);
  189. }
  190. System.out.println("Skipped while");
  191. }
  192. catch(SQLException se){
  193. //Handle errors for JDBC
  194. System.out.println("nope");
  195. se.printStackTrace();
  196. }
  197. }
  198. public static void hentBeste(){
  199.  
  200. }
  201. // + Mange flere metoder ..
  202.  
  203. //--------------------------------------MAIN---------------------------------------------------
  204. //------------------------------ Connection-Oppsett -------------------------------------------
  205. public static void main(String[] args) {
  206. Connection conn = null;
  207. Statement stmt = null;
  208. try{
  209. //STEP 2: Register JDBC driver
  210. Class.forName("com.mysql.jdbc.Driver");
  211.  
  212. //STEP 3: Open a connection
  213. System.out.println("Connecting to database...");
  214. conn = DriverManager.getConnection(DB_URL,USER,PASS); //Connects with DB(URL, Name & Password)
  215.  
  216. //---------------------------------- Logic(for tekst-UI) ------------------------------------
  217. //Lag loop som konstant spør om hva som skal gjøres + Text-input konstant
  218. //Kjører riktig funksjon med riktige parametere avhengig av input
  219. Scanner input = new Scanner(System.in);
  220. String handling;
  221.  
  222. while (true){
  223. System.out.print("Skriv inn handling(ny/hent/beste/avslutt): ");
  224. handling = input.next();
  225. if (handling.equals("ny")){
  226. nyTrening(conn, stmt);
  227. }else if(handling.equals("hent")){
  228. hentLogg(conn, stmt);
  229. }else if(handling.equals("beste")){
  230. hentBeste();
  231. }else if(handling.equals("avslutt")){
  232. break;
  233. }else{
  234. System.out.println("Feil. Skriv inn 'ny, 'hent, 'beste' eller 'avslutt'");
  235. continue;
  236. }
  237. }
  238.  
  239. // --------------------------- Clean-up / End--------------------------------------
  240. input.close();
  241. stmt.close();
  242. conn.close();
  243. }catch(SQLException se){
  244. //Handle errors for JDBC
  245. se.printStackTrace();
  246. }catch(Exception e){
  247. //Handle errors for Class.forName
  248. e.printStackTrace();
  249. }finally{
  250. //finally block used to close resources
  251. try{
  252. if(stmt!=null)
  253. stmt.close();
  254. }catch(SQLException se2){
  255. }// nothing we can do
  256. try{
  257. if(conn!=null)
  258. conn.close();
  259. }catch(SQLException se){
  260. se.printStackTrace();
  261. }//end finally try
  262. }//end try
  263. System.out.println("So long, Suckers!");
  264. }//End Main
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement