Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. package tdt4145;
  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 = "admin";
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. //------------------------------- Metoder for DB-manipulering -------------------------------------
  23. public static void nyTrening(){
  24.  
  25. }
  26.  
  27.  
  28. public static void hentLogg(Connection conn, Statement stmt){
  29. //STEP 4: Execute a query
  30. try{
  31. System.out.println("Creating statement...");
  32. stmt = conn.createStatement();
  33. String sql;
  34. sql = "SELECT Navn, OktNr FROM ovelse WHERE OktNr = '16'";
  35. ResultSet rs = stmt.executeQuery(sql);
  36.  
  37. while(rs.next()){
  38. //Retrieve by column name
  39. String navn = rs.getString("Navn");
  40. int oktnr = rs.getInt("OktNr");
  41.  
  42.  
  43. //Display values
  44. System.out.print("navn: " + navn);
  45. System.out.println(", oktnr: " + oktnr);
  46. }
  47. }
  48. catch(SQLException se){
  49. //Handle errors for JDBC
  50. se.printStackTrace();
  51. }
  52. }
  53. public static void hentBeste(){
  54.  
  55. }
  56. // + Mange flere metoder ..
  57.  
  58. //--------------------------------------MAIN---------------------------------------------------
  59. //------------------------------ Connection-Oppsett -------------------------------------------
  60. public static void main(String[] args) {
  61. Connection conn = null;
  62. Statement stmt = null;
  63. try{
  64. //STEP 2: Register JDBC driver
  65. Class.forName("com.mysql.jdbc.Driver");
  66.  
  67. //STEP 3: Open a connection
  68. System.out.println("Connecting to database...");
  69. conn = DriverManager.getConnection(DB_URL,USER,PASS); //Connects with DB(URL, Name & Password)
  70.  
  71. //---------------------------------- Logic(for tekst-UI) ------------------------------------
  72. //Lag loop som konstant spør om hva som skal gjøres + Text-input konstant
  73. //Kjører riktig funksjon med riktige parametere avhengig av input
  74. Scanner input = new Scanner(System.in);
  75. String handling;
  76.  
  77. while (true){
  78. System.out.print("Skriv inn handling(ny/hent/beste/avslutt): ");
  79. handling = input.next();
  80. if (handling.equals("ny")){
  81. nyTrening();
  82. }else if(handling.equals("hent")){
  83. hentLogg(conn, stmt);
  84. }else if(handling.equals("beste")){
  85. hentBeste();
  86. }else if(handling.equals("avslutt")){
  87. break;
  88. }else{
  89. System.out.println("Feil. Skriv inn 'ny, 'hent, 'beste' eller 'avslutt'");
  90. continue;
  91. }
  92. }
  93.  
  94. // --------------------------- Clean-up / End--------------------------------------
  95. input.close();
  96. stmt.close();
  97. conn.close();
  98. }catch(SQLException se){
  99. //Handle errors for JDBC
  100. se.printStackTrace();
  101. }catch(Exception e){
  102. //Handle errors for Class.forName
  103. e.printStackTrace();
  104. }finally{
  105. //finally block used to close resources
  106. try{
  107. if(stmt!=null)
  108. stmt.close();
  109. }catch(SQLException se2){
  110. }// nothing we can do
  111. try{
  112. if(conn!=null)
  113. conn.close();
  114. }catch(SQLException se){
  115. se.printStackTrace();
  116. }//end finally try
  117. }//end try
  118. System.out.println("So long, Suckers!");
  119. }//End Main
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement