Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.funtimecoding.java.mysql.example;
- import java.sql.*;
- import java.util.Scanner;
- public class ExampleMain {
- private static Connection connection;
- public static void main(String[] args) {
- try {
- createConnection();
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("Qe do me shtu username, shkruje shto");
- System.out.println("Qe do mi pa kush o, shkruje kush");
- System.out.println("Nese do mi kry shkruje 'nalu'");
- String nextstr = sc.next();
- if (nextstr.equals("shto")) {
- System.out.println("Username:");
- String username = sc.next();
- System.out.println("Password:");
- String password = sc.next();
- shtoUser(username, password);
- } else if (nextstr.equals("kush")) {
- System.out.println("---------------------------------------");
- printoListen();
- System.out.println("---------------------------------------");
- } else if (nextstr.equals("nalu")) {
- break;
- } else {
- System.out.println("A ki sy djalo");
- }
- }
- } catch (Exception e) {
- System.err.println(e.getMessage());
- }
- }
- private static void createConnection() throws Exception {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://localhost/test_dardan?user=root&password=");
- }
- private static void shtoUser(String username, String password) throws Exception {
- Statement statement = connection.createStatement();
- try {
- String query = "INSERT INTO Users (username, password)" +
- "VALUES ('" + username + "', '" + password + "')";
- statement.executeUpdate(query);
- } catch (Exception e) {
- System.out.println("Garant je tu e shtu per sdyti");
- }
- }
- private static void printoListen() throws Exception {
- Statement statement = connection.createStatement();
- ResultSet rezultati = statement.executeQuery("SELECT * FROM Users");
- while (rezultati.next()) {
- String username = rezultati.getString("username");
- String password = rezultati.getString("password");
- System.out.println(username + " e ka passwordin " + password);
- }
- }
- private static void krijotabelen() throws Exception {
- Statement statement = connection.createStatement();
- statement.executeUpdate("CREATE TABLE Users (" +
- "username VARCHAR(100) PRIMARY KEY," +
- "password VARCHAR(30) NOT NULL" +
- ")");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement