Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. package com.leo.test;
  2.  
  3. import java.io.*;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. import java.sql.*;
  7. import java.util.*;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. public class test
  12. {
  13. public static void main (String[] args) throws SQLException
  14. {
  15. Scanner in = new Scanner(System.in);
  16. System.out.println("Please type commands");
  17. StringBuilder builder = new StringBuilder();
  18. while (in.hasNextLine())
  19. {
  20. String line = in.nextLine();
  21. if (line.trim().equalsIgnoreCase("EXIT"))
  22. {
  23. System.exit(0);
  24. }
  25. else if (line.endsWith(";"))
  26. {
  27. builder.append(line);
  28. String command = builder.toString();
  29. command = command.substring(0, command.length() - 1);
  30. query(command);
  31. }
  32. else
  33. {
  34. builder.append(line);
  35. }
  36. }
  37. }
  38.  
  39. public static void query(String command)
  40. {
  41. Properties prop = new Properties();
  42. try (InputStream in = Files.newInputStream(Paths.get("src/com/leo/test/database.properties")))
  43. {
  44. prop.load(in);
  45. String driver = prop.getProperty("jdbc.driver");
  46. String url = prop.getProperty("jdbc.url");
  47. String database = prop.getProperty("jdbc.database");
  48. String username = prop.getProperty("jdbc.username");
  49. String password = prop.getProperty("jdbc.password");
  50. MySQLDatabase mysql = new MySQLDatabase(driver, url, username, password) ;
  51. mysql.setDatabase(database);
  52. mysql.execute(command);
  53. }
  54.  
  55. catch (IOException ex) {
  56. Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
  57. }
  58. }
  59. }
  60.  
  61. class MySQLDatabase
  62. {
  63. private Connection conn = null;
  64.  
  65. public MySQLDatabase(String driver, String url, String username, String password)
  66. {
  67. try
  68. {
  69. System.setProperty("jdbc.drivers", driver);
  70. conn = DriverManager.getConnection(url, username, password);
  71. }
  72. catch (SQLException e)
  73. {
  74. e.printStackTrace();
  75. }
  76. }
  77.  
  78. public void setDatabase(String database)
  79. {
  80. try
  81. {
  82. conn.setCatalog(database);
  83. }
  84. catch (SQLException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. public void execute(String command)
  90. {
  91. try
  92. {
  93. Statement stmt = conn.createStatement();
  94. boolean hasResSet = stmt.execute(command);
  95. if (hasResSet)
  96. {
  97. ResultSet resSet = stmt.getResultSet();
  98. ResultSetMetaData metaData = resSet.getMetaData();
  99. int columnCount = metaData.getColumnCount();
  100. for (int i = 1; i <= columnCount; i++)
  101. {
  102. if (i > 1) System.out.print(", ");
  103. String columnLabel = metaData.getColumnLabel(i);
  104. System.out.print(columnLabel);
  105. }
  106. System.out.println();
  107.  
  108. while (resSet.next())
  109. {
  110. for (int i = 1; i < columnCount; i++)
  111. {
  112. if (i > 1) System.out.print(", ");
  113. String value = resSet.getString(i);
  114. System.out.print(value);
  115. }
  116. System.out.println();
  117. }
  118. }
  119. else
  120. {
  121. int updateCount = stmt.getUpdateCount();
  122. System.out.println("Rows Affected: " + updateCount);
  123. }
  124.  
  125. }
  126. catch (SQLException e) {
  127. e.printStackTrace();
  128. }
  129.  
  130. }
  131.  
  132. public void close()
  133. {
  134. try
  135. {
  136. conn.close();
  137. }
  138. catch (SQLException ex) {
  139. Logger.getLogger(MySQLDatabase.class.getName()).log(Level.SEVERE, null, ex);
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement