Guest User

Untitled

a guest
Apr 14th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. import java.io.*;
  2. import java.sql.*;
  3.  
  4. public class DBConn {
  5.  
  6.     public static void main(String[] args) {
  7.         String dbTbl = null, dbDrv = null, dbUrl = null, dbUsr = "", dbPwd = "";
  8.         if(args.length > 2) {
  9.             dbTbl = args[0];
  10.             dbDrv = args[1];
  11.             dbUrl = args[2];
  12.             if(args.length > 3) {
  13.                 dbUsr = args[3];
  14.             }
  15.             if(args.length > 4) {
  16.                 dbPwd = args[4];
  17.             }
  18.         } else {
  19.             try {
  20.                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  21.                 System.out.println("Name der Tabelle eingeben (z.B. MeineTestTabelle):");
  22.                 dbTbl = in.readLine();
  23.                 System.out.println("Name des Datenbanktreibers eingeben (z.B. com.mysql.jdbc.Driver):");
  24.                 dbDrv = in.readLine();
  25.                 System.out.println("Url der Datenbank eingeben (z.B. jdbc:mysql://localhost:3306/MeineDb):");
  26.                 dbUrl = in.readLine();
  27.                 System.out.println("Benutzername (z.B. root):");
  28.                 dbUsr = in.readLine();
  29.                 System.out.println("Passwort (z.B. mysqlpwd):");
  30.                 dbPwd = in.readLine();
  31.             } catch(IOException ex) {
  32.                 System.out.println(ex);
  33.             }
  34.         }
  35.         showDbTable(dbTbl, dbDrv, dbUrl, dbUsr, dbPwd);
  36.     }
  37.    
  38.     private static void showDbTable(String dbTbl, String dbDrv, String dbUrl, String dbUsr, String dbPwd) {
  39.         if(dbTbl == null || dbTbl.length() == 0 || dbDrv == null || dbDrv.length() == 0 || dbUrl == null || dbUrl.length() == 0 ) {
  40.             System.out.println("Fehler: Parameter fehlt.");
  41.             return;
  42.         }
  43.        
  44.         Connection cn = null;
  45.         Statement st = null;
  46.         ResultSet rs = null;
  47.        
  48.         try {
  49.             // Select fitting database driver and connect:
  50.             Class.forName(dbDrv);
  51.             cn = DriverManager.getConnection(dbUrl, dbUsr, dbPwd);
  52.             st = cn.createStatement();
  53.             rs = st.executeQuery("SELECT * FROM " + dbTbl + ";");
  54.             // Get meta data:
  55.             ResultSetMetaData rsmd = rs.getMetaData();
  56.             int i, n = rsmd.getColumnCount();
  57.             // Print table content:
  58.             for(i = 0; i < n; i++)
  59.                 System.out.print("+---------------");
  60.                 System.out.println("+");
  61.                 for( i=1; i<=n; i++ )    // Attention: first column with 1 instead of 0
  62.                     System.out.print("| " + extendStringTo14(rsmd.getColumnName(i)));
  63.                 System.out.println("|");
  64.                 for(i = 0; i < n; i++)
  65.                     System.out.print("+---------------");
  66.                 System.out.println("+");
  67.                 while(rs.next()) {
  68.                     for(i = 1; i <= n; i++)  // Attention: first column with 1 instead of 0
  69.                         System.out.print("| " + extendStringTo14(rs.getString(i)));
  70.                     System.out.println("|");
  71.                 }
  72.                 for(i = 0; i < n; i++)
  73.                     System.out.print("+---------------");
  74.                 System.out.println("+");
  75.             } catch(Exception ex) {
  76.                 System.out.println(ex);
  77.             } finally {
  78.                 try { if( rs != null ) rs.close(); } catch( Exception ex ) {/* nothing to do*/}
  79.                 try { if( st != null ) st.close(); } catch( Exception ex ) {/* nothing to do*/}
  80.                 try { if( cn != null ) cn.close(); } catch( Exception ex ) {/* nothing to do*/}
  81.             }
  82.       }
  83.  
  84.       // Extend String to length of 14 characters
  85.       private static final String extendStringTo14(String s) {
  86.           if(s == null) {
  87.               s = "";
  88.           }
  89.           final String sFillStrWithWantLen = "              ";
  90.           final int iWantLen = sFillStrWithWantLen.length();
  91.           final int iActLen  = s.length();
  92.           if(iActLen < iWantLen)
  93.               return (s + sFillStrWithWantLen).substring( 0, iWantLen );
  94.           if(iActLen > 2 * iWantLen)
  95.               return s.substring(0, 2 * iWantLen);
  96.           return s;
  97.       }
  98.  
  99. }
Add Comment
Please, Sign In to add comment