Advertisement
Guest User

Untitled

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