Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 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(yyyy-mm-dd): ");
  27. String dato = input.next();
  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("Form(1 - 10) ");
  36. String form = input.next();
  37. //Prestasjon
  38. System.out.print("Prestasjon(1 - 10): ");
  39. String prestasjon = input.next();
  40. //notat
  41. System.out.print("Notat: ");
  42. String notat = input.next();
  43. try{
  44.  
  45.  
  46. String query = "insert into regcustomers (DEFAULT, dato, starttid, varighet, form, prestasjon, notat) values(?,?,?,?,?,?,?)";
  47. stmt.setString(1, "DEFAULT");
  48. stmt.setString(2,dato);
  49. stmt.setString(3,starttid);
  50. stmt.setString(4,varighet);
  51. stmt.setString(5,form);
  52. stmt.setString(6,prestasjon);
  53. stmt.setString(7,notat);
  54. //String sql = "INSERT INTO 'treningdb'.'treningsokt' ('OktNr`, `Dato`, `StartTid`, `Varighet`, `PersonligForm`, `Prestasjon`, `Notat`) VALUES (DEFAULT, `"+dato+"`, `"+starttid+"`, `"+varighet+"`, "+form+", "+form+", `"+notat+"`)";
  55. stmt.executeUpdate(sql);
  56.  
  57.  
  58. }catch(SQLException se){
  59. //Handle errors for JDBC
  60. System.out.println("nope");
  61. se.printStackTrace();
  62. }catch(Exception e){
  63. //Handle errors for Class
  64. e.printStackTrace();
  65. }
  66. }
  67.  
  68.  
  69. public static void hentLogg(Connection conn, Statement stmt){
  70. //STEP 4: Execute a query
  71. try{
  72. System.out.println("Creating statement...");
  73. stmt = conn.createStatement();
  74. String sql;
  75. sql = "SELECT Navn, OktNr FROM ovelse WHERE OktNr = '1'";
  76. ResultSet rs = stmt.executeQuery(sql);
  77.  
  78. while(rs.next()){
  79. //Retrieve by column name
  80. String navn = rs.getString("Navn");
  81. int oktnr = rs.getInt("OktNr");
  82.  
  83.  
  84. //Display values
  85. System.out.print("navn: " + navn);
  86. System.out.println(", oktnr: " + oktnr);
  87. }
  88. System.out.println("Skipped while");
  89. }
  90. catch(SQLException se){
  91. //Handle errors for JDBC
  92. System.out.println("nope");
  93. se.printStackTrace();
  94. }
  95. }
  96. public static void hentBeste(){
  97.  
  98. }
  99. // + Mange flere metoder ..
  100.  
  101. //--------------------------------------MAIN---------------------------------------------------
  102. //------------------------------ Connection-Oppsett -------------------------------------------
  103. public static void main(String[] args) {
  104. Connection conn = null;
  105. Statement stmt = null;
  106. try{
  107. //STEP 2: Register JDBC driver
  108. Class.forName("com.mysql.jdbc.Driver");
  109.  
  110. //STEP 3: Open a connection
  111. System.out.println("Connecting to database...");
  112. conn = DriverManager.getConnection(DB_URL,USER,PASS); //Connects with DB(URL, Name & Password)
  113.  
  114. //---------------------------------- Logic(for tekst-UI) ------------------------------------
  115. //Lag loop som konstant spør om hva som skal gjøres + Text-input konstant
  116. //Kjører riktig funksjon med riktige parametere avhengig av input
  117. Scanner input = new Scanner(System.in);
  118. String handling;
  119.  
  120. while (true){
  121. System.out.print("Skriv inn handling(ny/hent/beste/avslutt): ");
  122. handling = input.next();
  123. if (handling.equals("ny")){
  124. nyTrening(conn, stmt);
  125. }else if(handling.equals("hent")){
  126. hentLogg(conn, stmt);
  127. }else if(handling.equals("beste")){
  128. hentBeste();
  129. }else if(handling.equals("avslutt")){
  130. break;
  131. }else{
  132. System.out.println("Feil. Skriv inn 'ny, 'hent, 'beste' eller 'avslutt'");
  133. continue;
  134. }
  135. }
  136.  
  137. // --------------------------- Clean-up / End--------------------------------------
  138. input.close();
  139. stmt.close();
  140. conn.close();
  141. }catch(SQLException se){
  142. //Handle errors for JDBC
  143. se.printStackTrace();
  144. }catch(Exception e){
  145. //Handle errors for Class.forName
  146. e.printStackTrace();
  147. }finally{
  148. //finally block used to close resources
  149. try{
  150. if(stmt!=null)
  151. stmt.close();
  152. }catch(SQLException se2){
  153. }// nothing we can do
  154. try{
  155. if(conn!=null)
  156. conn.close();
  157. }catch(SQLException se){
  158. se.printStackTrace();
  159. }//end finally try
  160. }//end try
  161. System.out.println("So long, Suckers!");
  162. }//End Main
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement