Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.oop.jdbc.main;
- import java.io.File;
- import java.net.URISyntaxException;
- import java.sql.Connection;
- import java.sql.Driver;
- import java.sql.DriverPropertyInfo;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- import java.util.Scanner;
- import eg.edu.alexu.csd.oop.jdbc.jdbc.JDBCDriver;
- public class Main {
- private Driver driver;
- private Scanner s;
- private Connection connection;
- private Statement statement;
- private String URL;
- public static void main(final String[] args) {
- Main JDBC = new Main();
- if (JDBC.intialize()) {
- JDBC.run();
- }
- }
- public boolean intialize() {
- s = new Scanner(System.in);
- String path;
- try {
- path = Main.class.getProtectionDomain().getCodeSource()
- .getLocation().toURI().getPath();
- driver = new JDBCDriver(path + "users.txt", path + "passwords.txt");
- } catch (URISyntaxException e) {
- System.err.println("Path not found.");
- return false;
- }
- while (!getUser()) {
- System.err.println("Invalid user name or password.");
- }
- while (!getURL()) {
- System.err.println("Invalid URL.");
- }
- return true;
- }
- public void run() {
- try {
- connection = driver.connect(URL, getProperty());
- System.out.println("Enter your command or \'exit\' to exit.");
- while (true) {
- String command = readInput().trim();
- if (command.equalsIgnoreCase("exit")) {
- break;
- }
- statement = connection.createStatement();
- statement.execute(command);
- statement.close();
- }
- } catch (SQLException e) {
- System.err.println(e.getMessage());
- }
- s.close();
- }
- private Properties getProperty() {
- Properties info = new Properties();
- String tmpDir = System.getProperty("java.io.tmpdir");
- File dbDir = new File(tmpDir);
- info.put("path", dbDir.getAbsoluteFile());
- return info;
- }
- private boolean getURL() {
- System.out.println("Please enter URL");
- URL = s.nextLine();
- try {
- boolean accept = driver.acceptsURL(URL);
- return accept;
- } catch (SQLException e) {
- return false;
- }
- }
- private boolean getUser() {
- String user, password;
- System.out.println("Please enter user name");
- user = s.nextLine();
- System.out.println("Please enter password");
- password = s.nextLine();
- DriverPropertyInfo[] info;
- try {
- info = driver.getPropertyInfo(null, null);
- for (int i = 0; i < info.length; ++i) {
- if (user.equals(info[i].name) && password.equals(
- info[i].value)) {
- System.out.println("Welcome " + user);
- return true;
- }
- }
- return false;
- } catch (SQLException e) {
- System.err.println(e.getMessage());
- return false;
- }
- }
- private String readInput() {
- System.out.println("Double enter to execute :)");
- String command = new String();
- while (true) {
- String nxt = s.nextLine().trim();
- if (nxt.length() == 0) {
- break;
- }
- command += (nxt + " ");
- }
- return command;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement