Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. package test;
  2.  
  3. import java.sql.*;
  4.  
  5. import static java.lang.System.out;
  6.  
  7. public class App {
  8.     private Connection connection;
  9.  
  10.     public static void main(String[] args) {
  11.         new App().start();
  12.     }
  13.  
  14.     private void start() {
  15.         try {
  16.             executeInsert("insert into user(host, user, password, ssl_cipher, x509_issuer, x509_subject) values('localhost', 'hello', 'md5@!#$', '', '', '')");
  17.             executeSelect("select * from user order by rand()");
  18.             executeSelect("show tables");
  19.             executeSelect("select * from help_keyword");
  20.             executeSelect("select * from time_zone order by rand()");
  21.         } finally {
  22.             closeConnection();
  23.         }
  24.     }
  25.  
  26.     private void executeSelect(String selectQuery) {
  27.         Statement statement = null;
  28.         try {
  29.             statement = getConnection().createStatement();
  30.             ResultSet res = statement.executeQuery(selectQuery);
  31.             out.println(String.format("\nQuery: \"%s\"", selectQuery));
  32.             ResultSetMetaData metaData = res.getMetaData();
  33.             int columns = metaData.getColumnCount();
  34.             out.println("Columns:");
  35.  
  36.             StringBuffer format = new StringBuffer();
  37.  
  38.             for (int i = 1; i <= columns; i++) {
  39.                 out.println(i + ". " + metaData.getColumnName(i));
  40.                 int size = metaData.getColumnDisplaySize(i);
  41.                 size = (i == 1 ? 1 : (size > 20 ? 20 : Math.abs(size)));
  42.                 format.append("%").append(size).append("s");
  43.             }
  44.             out.println("Data:");
  45.             while (res.next()) {
  46.                 Object[] args = new String[columns];
  47.                 for (int i = 1; i <= columns; i++) {
  48.                     args[i - 1] = res.getString(i);
  49.                 }
  50.                 out.println(String.format(format.toString(), args));
  51.             }
  52.         } catch (SQLException e) {
  53.             e.printStackTrace();
  54.         } finally {
  55.                 if (statement != null) {
  56.                     try {
  57.                         statement.close();
  58.                     } catch (SQLException e) {
  59.                         e.printStackTrace();
  60.                     }
  61.                 }
  62.  
  63.         }
  64.     }
  65.  
  66.     private int executeInsert(String insertQuery) {
  67.         Statement statement = null;
  68.         try {
  69.             statement = getConnection().createStatement();
  70.             return statement.executeUpdate(insertQuery);
  71.         } catch (SQLException e) {
  72.             e.printStackTrace();
  73.         } finally {
  74.                 if (statement != null) {
  75.                     try {
  76.                         statement.close();
  77.                     } catch (SQLException e) {
  78.                         e.printStackTrace();
  79.                     }
  80.                 }
  81.         }
  82.         return 0;
  83.     }
  84.  
  85.     private Connection getConnection() {
  86.         if (connection == null) {
  87.             try {
  88.                 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "developer");
  89.             } catch (SQLException e) {
  90.                 e.printStackTrace();
  91.             }
  92.         }
  93.         return connection;
  94.     }
  95.  
  96.     private void closeConnection() {
  97.         try {
  98.             connection.close();
  99.         } catch (NullPointerException e) {
  100.             e.printStackTrace();
  101.         } catch (SQLException e) {
  102.             e.printStackTrace();
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement