WWW.ИСХОДНИКИ.РУ cpp.sources.ru
java.sources.ru web.sources.ru soft.sources.ru
jdbc.sources.ru asp.sources.ru api.sources.ru

  Форум на исходниках
  Java
  Горим - Java и таблица

СПРОСИТЬ  ОТВЕТИТЬ
профайл | регистрация | faq

Автор Тема:   Горим - Java и таблица
Nick опубликован 14-05-2001 10:55 MSK   Click Here to See the Profile for Nick   Click Here to Email Nick  
Люди добрые, подскажите, где мона взять готовый исходник на яве, чтоб табличку ристовал (любую) а то и сервер работает и клент - но все в console. А надо чтоб из базы данных все выводилось в виде таблиц (в AWT есно). Дело оч срочное.
Lunya опубликован 15-05-2001 14:19 MSK     Click Here to See the Profile for Lunya  Click Here to Email Lunya     

Примерно так:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

public class JdbcTesta extends Applet {

Label lb1 = new Label( "Userid" );
TextField tf1 = new TextField(15);
Label lb2 = new Label( "Password" );
TextField tf2 = new TextField(15);
Button bt1 = new Button("Execute Sql");
Button bt2 = new Button("Reset Sql");
Button bt3 = new Button("Reset Result Set");
Button bt4 = new Button("Reset All");
Label lb3 = new Label( "Sql Code" );
String sql = "select col_name " +
"from dbname.tbname " +
"where partition_nbr = '09' " +
"and col_name = 'ED5197782';";
TextArea ta1 = new TextArea( sql, 05, 40, TextArea.SCROLLBARS_BOTH );
Label lb4 = new Label( "Result Set" );
TextArea ta2 = new TextArea( "", 05, 40, TextArea.SCROLLBARS_BOTH );

public void init() {

Panel p0 = new Panel();
p0.setLayout( new BorderLayout() );

Panel p1 = new Panel();
p1.setLayout( new GridLayout( 2, 4) ) ;
p1.add( lb1 );
p1.add( tf1 );
p1.add( lb2 );
p1.add( tf2 ); tf2.setEchoChar('*');
bt1.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean editTF;
editTF = false;
editTF = editTFInput();
if ( editTF ) {
executeSql();
} else {
showStatus("Invalid input - Try Again ...");
} // end of if
} // end of actionPerformed method
} ); // end of anaonymous class 'ActionListener'
p1.add( bt1 );
bt2.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta1.setText("");
showStatus("Reset Sql ...");
} // end of actionPerformed method
} ); // end of anaonymous class 'ActionListener'
p1.add( bt2 );
bt3.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta2.setText("");
showStatus("Reset Result Set ...");
} // end of actionPerformed method
} ); // end of anaonymous class 'ActionListener'
p1.add( bt3 );
bt4.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta1.setText("");
ta2.setText("");
showStatus("Reset All ...");
} // end of actionPerformed method
} ); // end of anaonymous class 'ActionListener'
p1.add( bt4 );
p0.add( p1, BorderLayout.NORTH );

Panel p2 = new Panel();
p2.setLayout( new GridLayout( 4, 4 ) ) ;
p2.add( lb3 );
p2.add( ta1 );
p2.add( lb4 );
p2.add( ta2 );
p0.add ( p2, BorderLayout.CENTER );

this.add( p0 );

} // end of init

public boolean editTFInput() {
boolean editTF;
editTF = true;
if ( tf1.getText().length() != 7 ) { editTF = false; }
if ( tf2.getText().length() < 6 | | tf2.getText().length() > 8 ) {
editTF = false; }
return editTF;
} // end if method editTFInput

public void executeSql() {
try {

String userid, password;
Vector queryResults = new Vector();
int accum1 = 0;
int accum2 = 0;

showStatus("Loading JDBC Driver ..." );
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

showStatus("Connecting To jdbc:db2 :DSNP ..." );
userid = tf1.getText().trim().toUpperCase();
password = tf2.getText().trim().toUpperCase();
Connection con =
DriverManager.getConnection("jdbc:db2 :DSNT", userid, password );

showStatus("Creating SQL Statement ..." );
String sqlsel1 = ta1.getText();

showStatus("Creating Java Statement Class ..." );
Statement stmt = con.createStatement();

showStatus("Executing SQL VIA JAVA Statement ..." );
ResultSet rs = stmt.executeQuery( sqlsel1 );

showStatus("Processing Result Set VIA JAVA Statement ..." );
// result set meta data
ResultSetMetaData meta = rs.getMetaData();

// add java result set to a vector
while ( rs.next() ) {
StringBuffer strbuf = new StringBuffer();
String col1 = rs.getString( "col_name" );
strbuf.append( col1 ) ;
int col2 = 1;
accum1 = accum1 + col2;
accum2 = accum2 + col2;
String rows = strbuf.toString();
queryResults.addElement( rows );
strbuf.setLength( 0 );

} // end of while

// add vector to a text area component
Enumeration enum = queryResults.elements();
while (enum.hasMoreElements()) {
String rows = (String)enum.nextElement();
ta2.append( rows );

} // end of while

showStatus("Processing Completed ..." );

queryResults.removeAllElements(); // empty vector
rs.close(); // close ResultSet
stmt.close(); // close Statement
con.close(); // close Connection
} // end try block

catch(ClassNotFoundException e) {
showStatus("ClassNotFoundException!!!: " );
ta2.append("ClassNotFoundException!!!: " + e ) ;
}
catch(SQLWarning e) {

ta2.append("SQL_CODE !!!: " + e ) ;
}
catch(SQLException e) {
ta2.append("SQL_CLASS !!: " + e ) ;
} // end of catch block(s)

} // end of executeSql

} // end of class JdbcTesta


Для вывода в виде таблицы можно использовать Swing-компоненты от Sun, например,java.awt.swing.JTable или просто распечатать массив.

СПРОСИТЬ  ОТВЕТИТЬ
Перейти:


E-mail | WWW.ИСХОДНИКИ.RU

Powered by: Ultimate Bulletin Board, Freeware Version 5.10a
Purchase our Licensed Version- which adds many more features!
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.