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.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- 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 eg.edu.alexu.csd.oop.jdbc.jdbc.JDBCDriver;
- public class Main {
- private static Driver driver;
- private static BufferedReader br;
- private static Connection connection;
- private static Statement statement;
- private static String URL;
- public static void main(String[] args) throws SQLException, IOException {
- br = new BufferedReader(new InputStreamReader(System.in));
- driver = new JDBCDriver("users.txt", "passwords.txt");
- while (!getUser()) {
- System.err.println("Invalid user name or password");
- }
- while (!getURL()) {
- System.err.println("Invalid URL");
- }
- System.out.println("Enter DBName");
- connection = getConnection(br.readLine());
- 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();
- }
- br.close();
- }
- public static boolean getUser() throws SQLException, IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- String user, password;
- System.out.println("Please enter user name");
- user = br.readLine();
- System.out.println("Please enter password");
- password = br.readLine();
- DriverPropertyInfo[] 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;
- }
- public static boolean getURL() throws SQLException, IOException {
- System.out.println("Please enter URL");
- BufferedReader br = new BufferedReader(new InputStreamReader(
- System.in));
- URL = br.readLine();
- return driver.acceptsURL(URL);
- }
- public static Properties getProperty() {
- Properties info = new Properties();
- String tmpDir = System.getProperty("java.io.tmpdir");
- File dbDir = new File(tmpDir + "/jdbc/" + Math.round((((float) Math
- .random()) * 100000)));
- System.out.println(dbDir.getAbsolutePath());
- info.put("path", dbDir.getAbsoluteFile());
- return info;
- }
- public static Connection getConnection(String DBName) throws SQLException {
- connection = driver.connect(URL, getProperty());
- statement = connection.createStatement();
- statement.execute("CREATE DATABASE " + DBName);
- statement.execute("USE " + DBName);
- statement.close();
- return connection;
- }
- public static String readInput() throws IOException {
- System.out.println("Double enter to execute :)");
- String command = new String();
- while (true) {
- String nxt = br.readLine();
- if (nxt.length() == 0)
- break;
- command += (nxt + " ");
- }
- return command;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement