Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package eg.edu.alexu.csd.oop.jdbc.main;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.sql.Connection;
  8. import java.sql.Driver;
  9. import java.sql.DriverPropertyInfo;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.Properties;
  13.  
  14. import eg.edu.alexu.csd.oop.jdbc.jdbc.JDBCDriver;
  15.  
  16. public class Main {
  17.  
  18. private static Driver driver;
  19. private static BufferedReader br;
  20. private static Connection connection;
  21. private static Statement statement;
  22. private static String URL;
  23.  
  24. public static void main(String[] args) throws SQLException, IOException {
  25. br = new BufferedReader(new InputStreamReader(System.in));
  26. driver = new JDBCDriver("users.txt", "passwords.txt");
  27. while (!getUser()) {
  28. System.err.println("Invalid user name or password");
  29. }
  30. while (!getURL()) {
  31. System.err.println("Invalid URL");
  32. }
  33. System.out.println("Enter DBName");
  34. connection = getConnection(br.readLine());
  35. System.out.println("Enter your command or exit to exit");
  36. while (true) {
  37. String command = readInput().trim();
  38. if (command.equalsIgnoreCase("exit"))
  39. break;
  40. statement = connection.createStatement();
  41. statement.execute(command);
  42. statement.close();
  43. }
  44. br.close();
  45. }
  46.  
  47. public static boolean getUser() throws SQLException, IOException {
  48. BufferedReader br = new BufferedReader(new InputStreamReader(
  49. System.in));
  50. String user, password;
  51. System.out.println("Please enter user name");
  52. user = br.readLine();
  53. System.out.println("Please enter password");
  54. password = br.readLine();
  55. DriverPropertyInfo[] info = driver.getPropertyInfo(null, null);
  56. for (int i = 0; i < info.length; i++) {
  57. if (user.equals(info[i].name) && password.equals(info[i].value)) {
  58. System.out.println("Welcome " + user);
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64.  
  65. public static boolean getURL() throws SQLException, IOException {
  66. System.out.println("Please enter URL");
  67. BufferedReader br = new BufferedReader(new InputStreamReader(
  68. System.in));
  69. URL = br.readLine();
  70. return driver.acceptsURL(URL);
  71. }
  72.  
  73. public static Properties getProperty() {
  74. Properties info = new Properties();
  75. String tmpDir = System.getProperty("java.io.tmpdir");
  76. File dbDir = new File(tmpDir + "/jdbc/" + Math.round((((float) Math
  77. .random()) * 100000)));
  78. System.out.println(dbDir.getAbsolutePath());
  79. info.put("path", dbDir.getAbsoluteFile());
  80. return info;
  81. }
  82.  
  83. public static Connection getConnection(String DBName) throws SQLException {
  84. connection = driver.connect(URL, getProperty());
  85. statement = connection.createStatement();
  86. statement.execute("CREATE DATABASE " + DBName);
  87. statement.execute("USE " + DBName);
  88. statement.close();
  89. return connection;
  90. }
  91.  
  92. public static String readInput() throws IOException {
  93. System.out.println("Double enter to execute :)");
  94. String command = new String();
  95. while (true) {
  96. String nxt = br.readLine();
  97. if (nxt.length() == 0)
  98. break;
  99. command += (nxt + " ");
  100. }
  101. return command;
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement