Advertisement
Guest User

Databaza

a guest
Nov 15th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package org.funtimecoding.java.mysql.example;
  2.  
  3.  
  4. import java.sql.*;
  5. import java.util.Scanner;
  6.  
  7. public class ExampleMain {
  8.  
  9.     private static Connection connection;
  10.  
  11.     public static void main(String[] args) {
  12.         try {
  13.             createConnection();
  14.  
  15.             Scanner sc = new Scanner(System.in);
  16.             while (true) {
  17.                 System.out.println("Qe do me shtu username, shkruje shto");
  18.                 System.out.println("Qe do mi pa kush o, shkruje kush");
  19.                 System.out.println("Nese do mi kry shkruje 'nalu'");
  20.  
  21.                 String nextstr = sc.next();
  22.                 if (nextstr.equals("shto")) {
  23.                     System.out.println("Username:");
  24.                     String username = sc.next();
  25.                     System.out.println("Password:");
  26.                     String password = sc.next();
  27.  
  28.                     shtoUser(username, password);
  29.                 } else if (nextstr.equals("kush")) {
  30.                     System.out.println("---------------------------------------");
  31.                     printoListen();
  32.                     System.out.println("---------------------------------------");
  33.                 } else if (nextstr.equals("nalu")) {
  34.                     break;
  35.                 } else {
  36.                     System.out.println("A ki sy djalo");
  37.                 }
  38.  
  39.  
  40.  
  41.             }
  42.  
  43.  
  44.         } catch (Exception e) {
  45.             System.err.println(e.getMessage());
  46.         }
  47.     }
  48.  
  49.     private static void createConnection() throws Exception {
  50.         Class.forName("com.mysql.jdbc.Driver");
  51.         connection = DriverManager.getConnection("jdbc:mysql://localhost/test_dardan?user=root&password=");
  52.     }
  53.  
  54.     private static void shtoUser(String username, String password) throws Exception {
  55.         Statement statement = connection.createStatement();
  56.         try {
  57.             String query = "INSERT INTO Users (username, password)" +
  58.                     "VALUES ('" + username + "', '" + password + "')";
  59.                     statement.executeUpdate(query);
  60.         } catch (Exception e) {
  61.             System.out.println("Garant je tu e shtu per sdyti");
  62.         }
  63.     }
  64.  
  65.     private static void printoListen() throws Exception {
  66.         Statement statement = connection.createStatement();
  67.  
  68.         ResultSet rezultati = statement.executeQuery("SELECT * FROM Users");
  69.  
  70.         while (rezultati.next()) {
  71.             String username = rezultati.getString("username");
  72.             String password = rezultati.getString("password");
  73.  
  74.             System.out.println(username + " e ka passwordin " + password);
  75.         }
  76.     }
  77.  
  78.     private static void krijotabelen() throws Exception {
  79.         Statement statement = connection.createStatement();
  80.         statement.executeUpdate("CREATE TABLE Users (" +
  81.             "username VARCHAR(100) PRIMARY KEY," +
  82.             "password VARCHAR(30) NOT NULL" +
  83.             ")");
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement