Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Von Java nach HTML

Empfohlene Antworten

Veröffentlicht

Ich bins wieder,

hab immer so probleme die mir keiner beantworten. Hier ist das nächte. Ich habe mit Java eine Sql Datenbank ausgelesen, und möchte über java nun das ausgelesene in eine Html Datei oder Tabelle bringen. Wie geht das???

Schöne Grüße

Chaos2k

Hallo,

im folgenden Beispiel an den richtigen Stellen die Tags der HTML Tabelle setzen und das ganze in einer Datei speichern oder direkt in einem Servlet verarbeiten.

Gruß Jaraz

---------------------------------------------------

import java.io.*;
import java.sql.*;

public class ShowSqlTable
{
private static final String extendStringTo11( String s )
{
if( null == s ) s = "";
while( 11 > s.length() ) s += " ";
return s;
}
public static void main( String[] argv )
{
String sDatabase=null, sTable=null;
if( 2 <= argv.length )
{
sDatabase = argv[0];
sTable = argv[1];
}
else
{
try {
BufferedReader in = new BufferedReader(
new InputStreamReader( System.in ) );
System.out.println( "Name der Database eingeben:" );
sDatabase = in.readLine();
System.out.println( "Name der Tabelle eingeben:" );
sTable = in.readLine();
} catch( IOException ex ) {
System.out.println( ex );
}
}
if( null != sDatabase && 0 < sDatabase.length() &&
null != sTable && 0 < sTable.length() )
{
try {
// The following two lines must be adapted for other RDBMS than MySQL:
Class.forName( "org.gjt.mm.mysql.Driver" );
Connection cn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/" + sDatabase, "", "" );
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery( "select * from " + sTable );
ResultSetMetaData rsmd = rs.getMetaData();
int i, n = rsmd.getColumnCount();
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( "| " + extendStringTo11( 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( "| " + extendStringTo11( rs.getString( i ) ) );
System.out.println( "|" );
}
for( i=0; i<n; i++ )
System.out.print( "+------------" );
System.out.println( "+" );
rs.close();
st.close();
cn.close();
} catch( ClassNotFoundException ex ) {
System.out.println( ex );
} catch( SQLException ex ) {
System.out.println( ex );
}
}
}
}[/PHP]

Danke, hast du vielleicht noch eine genaue Beschreibung, was der Code bei den Zeilen macht? Ist nähmlich nicht ganz verständlich?

Der schreibt mir aber nichts auf die Datenbank oder " sDatabase"?

Gibt es noch weitere möglichkeiten?

Hallo,

der Code ist eigentlich sehr verständlich. Ist halt nur für die Ausgabe auf System.out optimiert.

private static final String extendStringTo11( String s )
{
if( null == s ) s = "";
while( 11 > s.length() ) s += " ";
return s;
}
[/PHP]

Erweitert jeden Wert der Abfrage auf 11 Zeichen, brauchst du bei html also nicht.

[PHP]String sDatabase=null, sTable=null;
if( 2 <= argv.length )
{
sDatabase = argv[0];
sTable = argv[1];
}
else
{
try {
BufferedReader in = new BufferedReader(
new InputStreamReader( System.in ) );
System.out.println( "Name der Database eingeben:" );
sDatabase = in.readLine();
System.out.println( "Name der Tabelle eingeben:" );
sTable = in.readLine();
} catch( IOException ex ) {
System.out.println( ex );
}
}
if( null != sDatabase && 0 < sDatabase.length() &&
null != sTable && 0 < sTable.length() )
{

Hiermit kannst du Datanbank und Tabelle beim Start oder nach dem Start des Programms eingeben.

Du kannst sie natürlich auch direkt in den Code schreiben.

Der try-catch Block ist nun für die Ausgabe zuständig.

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( "| " + extendStringTo11( 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( "| " + extendStringTo11( rs.getString( i ) ) );
System.out.println( "|" );
}
for( i=0; i<n; i++ )
System.out.print( "+------------" );
System.out.println( "+" );
[/PHP]

In diesem Bereich musst du nun halt die entsprechenden Ausgaben auf die Konsole durch die <tr><td></td><td></td></tr> Tags der HTML Tabelle ersetzen.

Wenn du das nicht kannst, weiss ich nicht was du mit deinem Programm willst, bzw würde ich dir raten dich erst einmal mit HTML auseinander zu setzen.

Gruß Jaraz

Und hier nun noch nen Beispiel mit Tabelle...

import java.sql.*;

public class HtmlSQLResult {
private String sql;
private Connection con;

public HtmlSQLResult(String sql, Connection con) {
this.sql = sql;
this.con = con;
}

public String toString() { // can be called at most once
StringBuffer out = new StringBuffer();

// Uncomment the following line to display the SQL command at start of table
// out.append("Results of SQL Statement: " + sql + "<P>\n");

try {
Statement stmt = con.createStatement();

if (stmt.execute(sql)) {
// There's a ResultSet to be had
ResultSet rs = stmt.getResultSet();
out.append("<TABLE>\n");

ResultSetMetaData rsmd = rs.getMetaData();

int numcols = rsmd.getColumnCount();

// Title the table with the result set's column labels
out.append("<TR>");
for (int i = 1; i <= numcols; i++)
out.append("<TH>" + rsmd.getColumnLabel(i));
out.append("</TR>\n");

while(rs.next()) {
out.append("<TR>"); // start a new row
for(int i = 1; i <= numcols; i++) {
out.append("<TD>"); // start a new data element
Object obj = rs.getObject(i);
if (obj != null)
out.append(obj.toString());
else
out.append(" ");
}
out.append("</TR>\n");
}

// End the table
out.append("</TABLE>\n");
}
else {
// There's a count to be had
out.append("<B>Records Affected:</B> " + stmt.getUpdateCount());
}
}
catch (SQLException e) {
out.append("</TABLE><H1>ERROR:</H1> " + e.getMessage());
}

return out.toString();
}
}[/PHP]

Habs schon verstanden, war von mir falsch gepostet! Danke für den Code hat mir sehr viel Zeit gesparrt!

Gruß

Chaos2k

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.