import javax.swing.*;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


import java.sql.*;

public class myfirebird1 extends JFrame {

   public void startsql(){
      String sDbDrv="interbase.interclient.Driver";
      String sDbUrl="D:\\hs-harz\\Vorlesungen\\WS\\DBS 1\\bsp\\EMPLOYEE.GDB";
      String sTable="employee";
      String sUsr="sysdba";
      String sPwd="masterkey";

      sDbUrl="D:\\hs-harz\\Vorlesungen\\WS\\DBS 1\\bsp\\EMPLOYEE.GDB";

      sDbDrv = "org.firebirdsql.jdbc.FBDriver";
      sDbDrv = "org.firebirdsql.jdbc.FBDriver";

/*
     Class c= Class.forName("com.mysql.jdbc.Driver");
     con = DriverManager.getConnection("jdbc:mysql://"+
               ip+"/"+database+"?user="+user
                +"&password="+password);
*/
      Connection cn = null;
      Statement  st = null;
      ResultSet  rs = null;
      try {
        // Select fitting database driver and connect:
        Class.forName( sDbDrv );
        cn = DriverManager.getConnection( sDbUrl, sUsr, sPwd );
        st = cn.createStatement();
        rs = st.executeQuery( "select * from " + sTable );
        // 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( null != rs ) rs.close(); } catch( Exception ex ) {}
        try { if( null != st ) st.close(); } catch( Exception ex ) {}
        try { if( null != cn ) cn.close(); } catch( Exception ex ) {}
      }
    }     // sql


    public myfirebird1() {
        this.setVisible(true);
        setSize(300,300);
    }
    public static void main(String[] args) {
        myfirebird1 f = new myfirebird1();
        f.startsql();
    }

// Extend String to length of 14 characters
  private static final String extendStringTo14( String s )
  {
    if( null == s ) 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;
  }


  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }


}
