SHARE
TWEET

Untitled

a guest Jun 17th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package gGui.src;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.ResultSet;
  9. import java.sql.ResultSetMetaData;
  10. import java.sql.Statement;
  11.  
  12. import com.itextpdf.text.DocumentException;
  13.  
  14.  
  15.  
  16. public class DbTableShow
  17. {
  18.   public static void main( String[] argv )
  19.   {
  20.     String sDbDrv=null, sDbUrl=null, sTable=null, sUsr="", sPwd="";
  21.     if( 3 <= argv.length ) {
  22.       sDbDrv = argv[0];
  23.       sDbUrl = argv[1];
  24.       sTable = argv[2];
  25.       if( 4 <= argv.length )  sUsr = argv[3];
  26.       if( 5 <= argv.length )  sPwd = argv[4];
  27.     } else {
  28.       BufferedReader in = new BufferedReader(
  29.                         new InputStreamReader( System.in ) );
  30.      
  31.     sDbDrv = "com.mysql.jdbc.Driver";
  32.     sDbUrl = "jdbc:mysql://localhost:3306/jrs_wilhelma";   
  33.     sTable = "tier";
  34.     sUsr = "root";
  35.     sPwd = "";
  36.    
  37.  
  38.     }
  39.     if( null != sDbDrv && 0 < sDbDrv.length() &&
  40.         null != sDbUrl && 0 < sDbUrl.length() &&
  41.         null != sTable && 0 < sTable.length() ) {
  42.       Connection cn = null;
  43.       Statement  st = null;
  44.       ResultSet  rs = null;
  45.       String selectS = "name";  
  46.      
  47.  
  48.       try {
  49.         // Select fitting database driver and connect:
  50.         Class.forName( sDbDrv );
  51.         cn = DriverManager.getConnection( sDbUrl, sUsr, sPwd );
  52.         Statement updt = null;
  53.         updt = cn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
  54.                  java.sql.ResultSet.CONCUR_UPDATABLE);
  55.         st = cn.createStatement();
  56.         updt.executeUpdate( "INSERT INTO tier (name, chipid, tierart, gebaeude, geburtsjahr, fuetterung, gewicht) VALUES" +
  57. "('testABC', 132, 'elephantus', 'G03', 1990, '11:00:00', '358.00')" );
  58.         rs = st.executeQuery( "select " + selectS + " from " + sTable );
  59.         // Get meta data:
  60.         ResultSetMetaData rsmd = rs.getMetaData();
  61.         int i, n = rsmd.getColumnCount();
  62.         // Print table content:
  63.        
  64.         while( rs.next() ) {
  65.           for( i=1; i<=n; i++ )  // Attention: first column with 1 instead of 0
  66.             System.out.print( "" + extendStringTo14( rs.getString( i ) ) );
  67.           System.out.println( "" );
  68.         }
  69.  
  70.  
  71.       } catch( Exception ex ) {
  72.         System.out.println( ex );
  73.       } finally {
  74.         try { if( null != rs ) rs.close(); } catch( Exception ex ) {}
  75.         try { if( null != st ) st.close(); } catch( Exception ex ) {}
  76.         try { if( null != cn ) cn.close(); } catch( Exception ex ) {}
  77.       }
  78.     }
  79.   }
  80.  
  81.   // Extend String to length of 14 characters
  82.   private static final String extendStringTo14( String s )
  83.   {
  84.     if( null == s ) s = "";
  85.     final String sFillStrWithWantLen = "              ";
  86.     final int iWantLen = sFillStrWithWantLen.length();
  87.     final int iActLen  = s.length();
  88.     if( iActLen < iWantLen )
  89.       return (s + sFillStrWithWantLen).substring( 0, iWantLen );
  90.     if( iActLen > 2 * iWantLen )
  91.       return s.substring( 0, 2 * iWantLen );
  92.     return s;
  93.   }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top