Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The MIT License
- *
- * Copyright 2016 clomez.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- package SWDTN002;
- import java.sql.* ; // for standard JDBC programs
- import java.util.Scanner;
- public class FirstContact2 {
- public static Connection connect(Connection connect) throws SQLException, Exception {
- String url = "jdbc:mysql://localhost:3306/test";
- String user = "root";
- String pass = "karhu4karhu4";
- try {
- Class.forName("com.mysql.jdbc.Driver");// ajurin määritys
- connect = DriverManager.getConnection(url, user, pass);
- System.out.println("Connection!");
- }
- catch (SQLException e) { // tietokantaan ei saada yhteyttä
- connect = null;
- System.out.println("error, ei yhteyttä");
- throw e;
- }
- catch (Exception e ) { // JDBC ajuria ei löydy
- System.out.println("error, ei ajuria");
- throw e;
- }
- return connect;
- }
- public static void haeKaikki(Connection connection) {
- try {
- String sql = "SELECT * FROM Employees";
- PreparedStatement lause = connection.prepareStatement(sql);
- ResultSet tulosjoukko = lause.executeQuery();
- while (tulosjoukko.next()) {
- System.out.println("id: " + tulosjoukko.getString("surename") + " " + tulosjoukko.getString("lastname")
- + "\nphone: " + tulosjoukko.getString("phone")
- + "\naddress" + tulosjoukko.getString("address")
- );
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- public static void haeTietty() {
- }
- public static int kysyInput() {
- Scanner input = new Scanner(System.in);
- System.out.println( "1) Hae kaikki asiakkaat\n" +
- "2) Hae tietty asiakas\n" +
- "0) Lopeta\n" +
- "Valitse:"
- );
- int userInput = input.nextInt();
- input.nextLine();
- return userInput;
- }
- public static void main(String[] args) {
- Connection connect = null;
- try {
- connect = connect(connect);
- } catch (Exception e) {
- System.out.println("ei onnistu");
- }
- Scanner input = new Scanner(System.in);
- int userInput;
- do {
- userInput = kysyInput();
- switch (userInput) {
- case 1:
- haeKaikki(connect);
- break;
- case 2:
- haeTietty();
- break;
- default:
- break;
- }
- } while (userInput != 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement