Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. /*
  2. * The MIT License
  3. *
  4. * Copyright 2016 clomez.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package SWDTN002;
  25.  
  26.  
  27. import java.sql.* ; // for standard JDBC programs
  28. import java.util.Scanner;
  29.  
  30. public class FirstContact2 {
  31.  
  32. public static Connection connect(Connection connect) throws SQLException, Exception {
  33.  
  34. String url = "jdbc:mysql://localhost:3306/test";
  35. String user = "root";
  36. String pass = "karhu4karhu4";
  37. try {
  38. Class.forName("com.mysql.jdbc.Driver");// ajurin määritys
  39. connect = DriverManager.getConnection(url, user, pass);
  40. System.out.println("Connection!");
  41. }
  42. catch (SQLException e) { // tietokantaan ei saada yhteyttä
  43. connect = null;
  44. System.out.println("error, ei yhteyttä");
  45. throw e;
  46. }
  47. catch (Exception e ) { // JDBC ajuria ei löydy
  48. System.out.println("error, ei ajuria");
  49. throw e;
  50.  
  51. }
  52. return connect;
  53. }
  54.  
  55.  
  56. public static void haeKaikki(Connection connection) {
  57. try {
  58. String sql = "SELECT * FROM Employees";
  59. PreparedStatement lause = connection.prepareStatement(sql);
  60. ResultSet tulosjoukko = lause.executeQuery();
  61.  
  62. while (tulosjoukko.next()) {
  63. System.out.println("id: " + tulosjoukko.getString("surename") + " " + tulosjoukko.getString("lastname")
  64. + "\nphone: " + tulosjoukko.getString("phone")
  65. + "\naddress" + tulosjoukko.getString("address")
  66. );
  67.  
  68. }
  69. } catch (Exception e) {
  70. System.out.println(e);
  71. }
  72.  
  73. }
  74.  
  75. public static void haeTietty() {
  76.  
  77. }
  78.  
  79. public static int kysyInput() {
  80. Scanner input = new Scanner(System.in);
  81. System.out.println( "1) Hae kaikki asiakkaat\n" +
  82. "2) Hae tietty asiakas\n" +
  83. "0) Lopeta\n" +
  84. "Valitse:"
  85. );
  86. int userInput = input.nextInt();
  87. input.nextLine();
  88. return userInput;
  89. }
  90.  
  91. public static void main(String[] args) {
  92.  
  93. Connection connect = null;
  94.  
  95. try {
  96. connect = connect(connect);
  97. } catch (Exception e) {
  98. System.out.println("ei onnistu");
  99. }
  100. Scanner input = new Scanner(System.in);
  101. int userInput;
  102.  
  103. do {
  104. userInput = kysyInput();
  105. switch (userInput) {
  106. case 1:
  107. haeKaikki(connect);
  108. break;
  109. case 2:
  110. haeTietty();
  111. break;
  112. default:
  113. break;
  114. }
  115.  
  116. } while (userInput != 0);
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement