Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.sql.*;
- import java.util.*;
- public class Main {
- public static Connection so = null;
- static String url = "jdbc:mysql://localhost:3306/dbsocials?serverTimezone=UTC";
- static String user = "root";
- static String pass = "admin";
- static String enter1;
- static int token_id;
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- System.out.println("MySQL JDBC Driver not found !!");
- return;
- }
- try {
- so = DriverManager.getConnection(url, user, pass);
- } catch (SQLException e) {
- System.out.println("Failed to connect to database.");
- return;
- }
- Socials();
- }
- public static void clrscr(){
- System.out.print("\033[H\033[2J");
- System.out.flush();
- }
- public static void Socials() {
- System.out.println("***Welcome to Socials***");
- System.out.println("Please select an action");
- System.out.println("1) Login to socials");
- System.out.println("2) Sign up");
- System.out.print("Input: ");
- Scanner x = new Scanner(System.in);
- int a = x.nextInt();
- switch (a) {
- case 1:
- Login();
- break;
- case 2:
- Signup();
- System.out.println("Signup successful!");
- Socials();
- break;
- default:
- clrscr();
- Socials();
- break;
- }
- }
- public static void Login()
- {
- System.out.println("Login Menu");
- Scanner x = new Scanner(System.in);
- System.out.print("Enter username: ");
- enter1 = x.next();
- System.out.print("Enter password: ");
- String enter2 = x.next();
- try{
- Statement st = so.createStatement();
- String LoginQuery = "SELECT * FROM tbl_users WHERE username = '"+enter1+"' AND passwd = '"+enter2+"'";
- ResultSet res = st.executeQuery (LoginQuery);
- if (res.absolute(1))
- {
- try
- {
- String IdQuery = "SELECT id FROM tbl_users WHERE username = '"+enter1+"'";
- ResultSet rs = st.executeQuery (IdQuery);
- if (rs.next()) {
- token_id = rs.getInt("id");
- }
- }
- catch (SQLException e)
- {
- System.out.println("Error exception e");
- }
- Dashboard();
- }
- else
- {
- System.out.println("Invalid Username or Password\n Please Try Again");
- Login();
- }
- } catch (SQLException e){
- System.out.println("Login failed");
- }
- }
- public static void Signup()
- {
- Scanner x = new Scanner(System.in);
- System.out.println("We need your credentials");
- System.out.print("Last name: ");
- String last = x.next();
- System.out.print("First name: ");
- String first = x.next();
- System.out.print("Contact no: ");
- String cont = x.next();
- System.out.print("Email address: ");
- String email = x.next();
- System.out.print("Username: ");
- String user = x.next();
- System.out.print("Password: ");
- String pass = x.next();
- try {
- Statement st = so.createStatement();
- st.executeUpdate("insert into tbl_user_details (last_name, first_name, contact_no, email_addr) value('" + last + "', '" + first + "', '" + cont + "', '" + email + "')");
- st.executeUpdate("insert into tbl_users (username, passwd, created) value('" + user + "', '" + pass + "', now())");
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.println("Sign up error");
- return;
- }
- }
- public static void Dashboard()
- {
- System.out.println("Welcome, " +enter1);
- System.out.println("***DEBUG: YOUR TOKEN ID IS " +token_id);
- System.out.println("1. Add friend");
- System.out.println("2. Write Message");
- System.out.println("3. Inbox");
- System.out.println("4. Logout");
- System.out.println("5. View Friends");
- System.out.print("Input: ");
- Scanner list = new Scanner(System.in);
- int choice = list.nextInt();
- switch (choice)
- {
- case 1:
- AddFriends();
- break;
- case 2:
- Write();
- break;
- case 3:
- Inbox();
- break;
- case 4:
- Main.main(null);
- break;
- case 5:
- FriendList();
- break;
- }
- }
- public static void AddFriends()
- {
- System.out.println("Add friends");
- System.out.println("Search for the user that you would like to add:");
- System.out.print("Input: ");
- Scanner search = new Scanner(System.in);
- String se = search.next();
- try
- {
- Statement st = so.createStatement();
- String ListQuery = "SELECT DISTINCT tbl_user_details.u_id, tbl_user_details.first_name, tbl_user_details.last_name FROM tbl_user_details WHERE last_name LIKE '"+se+"' OR first_name LIKE '"+se+"'";
- ResultSet rs = st.executeQuery (ListQuery);
- System.out.println("ID First Last");
- while (rs.next())
- {
- int id = rs.getInt("u_id");
- String firstname = rs.getString("first_name");
- String lastname = rs.getString("last_name");
- System.out.format("%s %s %s\n", id, firstname, lastname );
- }
- }
- catch (Exception e)
- {
- System.out.println("Error");
- }
- System.out.println("Input the id of the user you want to add:");
- System.out.print("> ");
- int friend_id = search.nextInt();
- try
- {
- Statement st = so.createStatement();
- String AddQuery = "INSERT INTO tbl_friends VALUES ('"+token_id+"', '"+friend_id+"')";
- String ReverseQuery = "INSERT INTO tbl_friends VALUES ('"+friend_id+"', '"+token_id+"')";
- st.executeUpdate(AddQuery);
- st.executeUpdate(ReverseQuery);
- System.out.println("You successfully added this user!");
- }
- catch (SQLException e)
- {
- System.out.println("Error");
- }
- Dashboard();
- }
- public static void Write()
- {
- System.out.println("Write a message");
- System.out.println("Search for the user that you would like to send a message to:");
- System.out.print("> ");
- Scanner search = new Scanner(System.in);
- String se = search.next();
- try
- {
- Statement st = so.createStatement();
- String ListQuery = "SELECT DISTINCT tbl_user_details.u_id, tbl_user_details.first_name, tbl_user_details.last_name FROM tbl_user_details WHERE last_name LIKE '"+se+"' OR first_name LIKE '"+se+"'";
- ResultSet rs = st.executeQuery (ListQuery);
- System.out.println("ID First Last");
- while (rs.next())
- {
- int id = rs.getInt("u_id");
- String firstname = rs.getString("first_name");
- String lastname = rs.getString("last_name");
- System.out.format("%s %s %s\n", id, firstname, lastname );
- }
- }
- catch (Exception e)
- {
- System.out.println("Error");
- }
- System.out.println("Input the id of the user you want to send a message to:");
- System.out.print("> ");
- int id = search.nextInt();
- System.out.println("Enter the message that you want to send");
- System.out.print("> ");
- String msg = search.next();
- try
- {
- Statement st = so.createStatement();
- String MsgQuery = "INSERT INTO tbl_inbox VALUES ('"+id+"', '"+msg+"', '"+enter1+"', now())";
- st.executeUpdate(MsgQuery);
- System.out.println("Message sent!");
- }
- catch (SQLException e)
- {
- System.out.println("Error");
- }
- Dashboard();
- }
- public static void Inbox()
- {
- System.out.println("Inbox");
- try
- {
- Statement st = so.createStatement();
- String ViewInbox = "SELECT tbl_inbox.msg, tbl_inbox.date, tbl_inbox.sender FROM tbl_inbox JOIN tbl_user_details ON tbl_user_details.u_id = tbl_inbox.id WHERE tbl_inbox.id = '"+token_id+"'";
- ResultSet res = st.executeQuery(ViewInbox);
- System.out.println("Sender Date Message");
- while(res.next())
- {
- String msg = res.getString("msg");
- String date = res.getString("date");
- String sender = res.getString("sender");
- System.out.format("%s %s %s\n", sender, date, msg);
- }
- }
- catch (SQLException e)
- {
- System.out.println("Error");
- }
- System.out.println("Press 1 to exit");
- Scanner sc = new Scanner(System.in);
- String any = sc.next();
- Dashboard();
- }
- public static void FriendList()
- {
- System.out.println("Friend List");
- try {
- Statement st = so.createStatement();
- String ListQuery = "SELECT tbl_user_details.first_name, tbl_user_details.last_name FROM tbl_user_details JOIN tbl_friends ON tbl_user_details.u_id = tbl_friends.friend_id WHERE tbl_friends.id = '"+token_id+"'";
- ResultSet res = st.executeQuery(ListQuery);
- while (res.next())
- {
- String symb = "*";
- String firstname = res.getString("first_name");
- String lastname = res.getString("last_name");
- System.out.format("%s %s %s\n", symb, firstname, lastname );
- }
- st.close();
- }
- catch (SQLException e)
- {
- System.out.println("Error displaying list");
- }
- System.out.println("Press 1 to exit");
- Scanner sc = new Scanner(System.in);
- String any = sc.next();
- Dashboard();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement