import java.io.*;
import java.sql.*;
public class Run
{
public static void main( String[] argv )
{
String sDbDrv=null;
String sDbUrl=null;
String sUsr="";
String sPwd="";
try{
BufferedReader in = new BufferedReader(new InputStreamReader( System.in ) );
System.out.print("Name des Datenbanktreibers (com.mysql.jdbc.Driver oder org.postgresql.Driver): ");
sDbDrv = in.readLine();
System.out.println("Url des Datenbanktreibers (jdbc:mysql://localhost:8889/ oder jdbc:postgresql://localhost:8889/): ");
sDbUrl = in.readLine();
System.out.print("Datenbank:" );
sDbUrl = sDbUrl.concat(in.readLine());
System.out.print("Benutzername: ");
sUsr = in.readLine();
System.out.print("Passwort: ");
sPwd = in.readLine();
} catch( IOException ex ) {System.out.println( ex );}
if( null != sDbDrv && 0 < sDbDrv.length() && null != sDbUrl && 0 < sDbUrl.length() ){
Connection cn = null;
Statement st = null;
ResultSet rs = null;
boolean bExitProgramm = true;
String sReadInLine = "";
while(bExitProgramm)
{try {
// Select fitting database driver and connect:
Class.forName( sDbDrv );
cn = DriverManager.getConnection( sDbUrl, sUsr, sPwd );
st = cn.createStatement();
System.out.println( "Bitte geben sie das Query ein: ");
try {
BufferedReader in = new BufferedReader(new InputStreamReader( System.in ) );
sReadInLine=in.readLine();
if(sReadInLine.compareTo("exit")==0)
{
bExitProgramm=false;
break;
}
else
{
rs = st.executeQuery( sReadInLine );
printResult(rs);
}
} catch( IOException ex ) { bExitProgramm=false; System.out.println( ex );}
} catch( Exception ex ) {System.out.println( ex );}
finally {
bExitProgramm=false;
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 ) {}
}
}
}
}
// 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;
}
private static final void printResult(ResultSet rs) throws SQLException
{
// 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( "+" );
}
}