Advertisement
afinlay

JDBC Example

Nov 12th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import static java.lang.System.out;
  6.  
  7. public class DBEG {
  8. public static void main (String[] args) {
  9.  
  10. /*
  11. args[0] => database name
  12. args[1] => table name
  13. args[2] => user
  14. args[3] => pass
  15.  
  16. java DBEG.java OCPJP sample root XXXX
  17.  
  18. sudo mysql -u root -p
  19. SET GLOBAL validate_password.policy=LOW; //default is MEDIUM
  20. SET GLOBAL validate_password.length=4;
  21. SET GLOBAL validate_password.mixed_case_count=1;
  22. SET GLOBAL validate_password.number_count=0;
  23. SET GLOBAL validate_password.special_char_count=1;
  24. ALTER USER 'root'@'localhost' IDENTIFIED BY 'XXXX';
  25.  
  26. For MariaDB on Fedora 29:
  27. https://www.if-not-true-then-false.com/2013/install-mariadb-on-fedora-centos-rhel/
  28.  
  29. USE OCPJP
  30. ALTER TABLE sample ADD PRIMARY KEY (col1);
  31.  
  32. */
  33.  
  34. String mySQL = new StringBuilder("jdbc:mysql://localhost:3306/").append(args[0]).toString();
  35. String postgreSQL = new StringBuilder("jdbc:postgresql://localhost:5432/").append(args[0]).toString();
  36. BufferedReader inpt = new BufferedReader(new InputStreamReader(System.in));
  37. ResultSetMetaData rsmd = null;
  38. DatabaseMetaData dbmd = null;
  39. Connection con = null;
  40. Statement stmt = null;
  41. ResultSet rs = null;
  42. String query = null;
  43.  
  44. try {
  45.  
  46. con = DriverManager.getConnection(mySQL, args[2], args[3]);
  47. dbmd = con.getMetaData();
  48. stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  49. stmt.closeOnCompletion();
  50. var warnings = con.getWarnings();
  51.  
  52. do {
  53. out.print("\nEnter SQL Query: ");
  54. query = inpt.readLine();
  55.  
  56. if (query.toLowerCase().equals("exit")) {
  57. try {
  58. inpt.close();
  59. stmt.close();
  60. // con.close();
  61. } catch (Exception e) { e.printStackTrace(); }
  62. finally {
  63. System.exit(0);
  64. };
  65. }
  66.  
  67. else {
  68. rs = stmt.executeQuery(query);
  69. rsmd = rs.getMetaData();
  70. out.println("JDBC Driver Name: [" + dbmd.getDriverName() + "]");
  71. out.println("Database Product Name: [" + dbmd.getDatabaseProductName() + "]");
  72. out.println("Local Database Name (Relative): [" + dbmd.getURL().substring(dbmd.getURL().lastIndexOf("/") + 1) + "]");
  73. out.println("Local Database Name: [" + dbmd.getURL() + "]");
  74. out.println("Connection Object Catalog: " + con.getCatalog());
  75. out.println("Table Name: [" + rsmd.getTableName(1) + "]");
  76. //calculate the amount of rows
  77. rs.last(); int rowct = rs.getRow(); rs.beforeFirst();
  78. //----------------------
  79. out.println("Row Count in ResultSet: " + rowct);
  80.  
  81. while(rs.next()) {
  82. out.println("\t[col1]:\t" + rs.getString("col1"));
  83. };
  84.  
  85. while (true) {
  86. if (warnings == null) {
  87. out.println("No warnings bro.");
  88. break;
  89. }
  90. else
  91. out.println(warnings.getNextWarning());
  92. }
  93.  
  94. //Insert Row Demo
  95. rs.first(); //1
  96. rs.next(); //2
  97. out.println("Row before insert row: " + rs.getRow());
  98. rs.moveToInsertRow();
  99. out.println("What is the index of the insert row?: " + rs.getRow());
  100. }
  101. } while (!query.toLowerCase().equals("exit"));
  102.  
  103. } catch (SQLSyntaxErrorException sre) {
  104. out.println("Your SQL query is malformed.");
  105. }
  106. catch (SQLException | IOException sqie) {
  107. sqie.printStackTrace();
  108. while (true) {
  109.  
  110. if (sqie instanceof SQLException) {
  111. SQLException se = ((SQLException) sqie).getNextException();
  112. if (se == null) {
  113. out.println("No Exceptions bro.");
  114. break;
  115. }
  116. else
  117. out.println(se);
  118. }
  119. }
  120. } finally {
  121. try {
  122. if (inpt != null)
  123. inpt.close();
  124. if (rs != null)
  125. rs.close();
  126. if (!stmt.isClosed())
  127. stmt.close(); //no longer needed
  128. if (!stmt.isClosed())
  129. con.close();
  130. } catch (SQLException | IOException e) { e.printStackTrace(); };
  131. };
  132.  
  133. };
  134. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement