Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import static java.lang.System.out;
- public class DBEG {
- public static void main (String[] args) {
- /*
- args[0] => database name
- args[1] => table name
- args[2] => user
- args[3] => pass
- java DBEG.java OCPJP sample root XXXX
- sudo mysql -u root -p
- SET GLOBAL validate_password.policy=LOW; //default is MEDIUM
- SET GLOBAL validate_password.length=4;
- SET GLOBAL validate_password.mixed_case_count=1;
- SET GLOBAL validate_password.number_count=0;
- SET GLOBAL validate_password.special_char_count=1;
- ALTER USER 'root'@'localhost' IDENTIFIED BY 'XXXX';
- For MariaDB on Fedora 29:
- https://www.if-not-true-then-false.com/2013/install-mariadb-on-fedora-centos-rhel/
- USE OCPJP
- ALTER TABLE sample ADD PRIMARY KEY (col1);
- */
- String mySQL = new StringBuilder("jdbc:mysql://localhost:3306/").append(args[0]).toString();
- String postgreSQL = new StringBuilder("jdbc:postgresql://localhost:5432/").append(args[0]).toString();
- BufferedReader inpt = new BufferedReader(new InputStreamReader(System.in));
- ResultSetMetaData rsmd = null;
- DatabaseMetaData dbmd = null;
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- String query = null;
- try {
- con = DriverManager.getConnection(mySQL, args[2], args[3]);
- dbmd = con.getMetaData();
- stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
- stmt.closeOnCompletion();
- var warnings = con.getWarnings();
- do {
- out.print("\nEnter SQL Query: ");
- query = inpt.readLine();
- if (query.toLowerCase().equals("exit")) {
- try {
- inpt.close();
- stmt.close();
- // con.close();
- } catch (Exception e) { e.printStackTrace(); }
- finally {
- System.exit(0);
- };
- }
- else {
- rs = stmt.executeQuery(query);
- rsmd = rs.getMetaData();
- out.println("JDBC Driver Name: [" + dbmd.getDriverName() + "]");
- out.println("Database Product Name: [" + dbmd.getDatabaseProductName() + "]");
- out.println("Local Database Name (Relative): [" + dbmd.getURL().substring(dbmd.getURL().lastIndexOf("/") + 1) + "]");
- out.println("Local Database Name: [" + dbmd.getURL() + "]");
- out.println("Connection Object Catalog: " + con.getCatalog());
- out.println("Table Name: [" + rsmd.getTableName(1) + "]");
- //calculate the amount of rows
- rs.last(); int rowct = rs.getRow(); rs.beforeFirst();
- //----------------------
- out.println("Row Count in ResultSet: " + rowct);
- while(rs.next()) {
- out.println("\t[col1]:\t" + rs.getString("col1"));
- };
- while (true) {
- if (warnings == null) {
- out.println("No warnings bro.");
- break;
- }
- else
- out.println(warnings.getNextWarning());
- }
- //Insert Row Demo
- rs.first(); //1
- rs.next(); //2
- out.println("Row before insert row: " + rs.getRow());
- rs.moveToInsertRow();
- out.println("What is the index of the insert row?: " + rs.getRow());
- }
- } while (!query.toLowerCase().equals("exit"));
- } catch (SQLSyntaxErrorException sre) {
- out.println("Your SQL query is malformed.");
- }
- catch (SQLException | IOException sqie) {
- sqie.printStackTrace();
- while (true) {
- if (sqie instanceof SQLException) {
- SQLException se = ((SQLException) sqie).getNextException();
- if (se == null) {
- out.println("No Exceptions bro.");
- break;
- }
- else
- out.println(se);
- }
- }
- } finally {
- try {
- if (inpt != null)
- inpt.close();
- if (rs != null)
- rs.close();
- if (!stmt.isClosed())
- stmt.close(); //no longer needed
- if (!stmt.isClosed())
- con.close();
- } catch (SQLException | IOException e) { e.printStackTrace(); };
- };
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement