Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.sql.*;
- public class DBConn {
- public static void main(String[] args) {
- String dbTbl = null, dbDrv = null, dbUrl = null, dbUsr = "", dbPwd = "";
- if(args.length > 2) {
- dbTbl = args[0];
- dbDrv = args[1];
- dbUrl = args[2];
- if(args.length > 3) {
- dbUsr = args[3];
- }
- if(args.length > 4) {
- dbPwd = args[4];
- }
- } else {
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Name der Tabelle eingeben (z.B. MeineTestTabelle):");
- dbTbl = in.readLine();
- System.out.println("Name des Datenbanktreibers eingeben (z.B. com.mysql.jdbc.Driver):");
- dbDrv = in.readLine();
- System.out.println("Url der Datenbank eingeben (z.B. jdbc:mysql://localhost:3306/MeineDb):");
- dbUrl = in.readLine();
- System.out.println("Benutzername (z.B. root):");
- dbUsr = in.readLine();
- System.out.println("Passwort (z.B. mysqlpwd):");
- dbPwd = in.readLine();
- } catch(IOException ex) {
- System.out.println(ex);
- }
- }
- showDbTable(dbTbl, dbDrv, dbUrl, dbUsr, dbPwd);
- }
- private static void showDbTable(String dbTbl, String dbDrv, String dbUrl, String dbUsr, String dbPwd) {
- if(dbTbl == null || dbTbl.length() == 0 || dbDrv == null || dbDrv.length() == 0 || dbUrl == null || dbUrl.length() == 0 ) {
- System.out.println("Fehler: Parameter fehlt.");
- return;
- }
- Connection cn = null;
- Statement st = null;
- ResultSet rs = null;
- try {
- // Select fitting database driver and connect:
- Class.forName(dbDrv);
- cn = DriverManager.getConnection(dbUrl, dbUsr, dbPwd);
- st = cn.createStatement();
- rs = st.executeQuery("SELECT * FROM " + dbTbl + ";");
- // Get meta data:
- ResultSetMetaData rsmd = rs.getMetaData();
- int i, n = rsmd.getColumnCount();
- // Print table content:
- for(i = 0; i < n; i++)
- System.out.print("+---------------");
- System.out.println("+");
- for( i=1; i<=n; i++ ) // Attention: first column with 1 instead of 0
- System.out.print("| " + extendStringTo14(rsmd.getColumnName(i)));
- System.out.println("|");
- for(i = 0; i < n; i++)
- System.out.print("+---------------");
- System.out.println("+");
- while(rs.next()) {
- for(i = 1; i <= n; i++) // Attention: first column with 1 instead of 0
- System.out.print("| " + extendStringTo14(rs.getString(i)));
- System.out.println("|");
- }
- for(i = 0; i < n; i++)
- System.out.print("+---------------");
- System.out.println("+");
- } catch(Exception ex) {
- System.out.println(ex);
- } finally {
- try { if( rs != null ) rs.close(); } catch( Exception ex ) {/* nothing to do*/}
- try { if( st != null ) st.close(); } catch( Exception ex ) {/* nothing to do*/}
- try { if( cn != null ) cn.close(); } catch( Exception ex ) {/* nothing to do*/}
- }
- }
- // Extend String to length of 14 characters
- private static final String extendStringTo14(String s) {
- if(s == null) {
- s = "";
- }
- final String sFillStrWithWantLen = " ";
- final int iWantLen = sFillStrWithWantLen.length();
- final int iActLen = s.length();
- if(iActLen < iWantLen)
- return (s + sFillStrWithWantLen).substring( 0, iWantLen );
- if(iActLen > 2 * iWantLen)
- return s.substring(0, 2 * iWantLen);
- return s;
- }
- }
Add Comment
Please, Sign In to add comment