import java.awt.*; import java.awt.event.*; import javax.swing ...

snottybugbearSoftware and s/w Development

Nov 3, 2013 (3 years and 10 months ago)

96 views

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class Stores7 extends JFrame implements ActionListener{
JPanel base;
JScrollPane scroll;
JTextArea view;
JButton closeButton, getDataButton, clearButton;
JComboBox tableCombo;
Connection con;
Statement stmt;
public static void main(String args[]){
new Stores7();
}
public Stores7(){
super("connet to Informix with JDBC");
setBounds(100,100,500,300);
view = new JTextArea("", 10, 70);
view.setFont(new Font("MonoSpaced", Font.PLAIN, 12));
scroll = new JScrollPane(view);
base = new JPanel();
tableCombo = new JComboBox();
getDataButton = new JButton("Get Data");
closeButton = new JButton("CLOSE");
clearButton = new JButton("CLEAR");
base.setLayout(new FlowLayout(FlowLayout.CENTER));
base.add(tableCombo);
base.add(getDataButton);
base.add(scroll);
base.add(clearButton);
base.add(closeButton);
getContentPane().add(base);
String dsn = "jdbc:odbc:tst_sample";
String user = "informix";
String password = "inf_passwd";
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(dsn, user, password);
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("SELECT tabname FROM systables where tabid >= 100");
while (rs.next()) {
tableCombo.addItem(rs.getString("tabname"));
}
rs.close();
stmt.close();
} catch (Exception e) {
System.out.println(e.toString());
}
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
endProcess();
System.exit(0);
}
});
getDataButton.addActionListener(this);
closeButton.addActionListener(this);
clearButton.addActionListener(this);
tableCombo.addActionListener(this);
show();
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == closeButton){
endProcess();
System.exit(0);
} else if(evt.getSource() == getDataButton){
selectData();
} else if(evt.getSource() == clearButton){
view.setText("");
}
}
public void selectData(){
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("SELECT * FROM " + tableCombo.getSelectedItem());
ResultSetMetaData rmeta = rs.getMetaData();
while (rs.next()) {
for(int i = 1; i <= rmeta.getColumnCount(); i++){
String clumnValue = rs.getString(i);
if (rs.wasNull()){
for (int j = 0; j < rmeta.getColumnDisplaySize(i); j++){
view.append(" ");
}
view.append("\t");
}else{
view.append(clumnValue + "\t");
}
}
view.append("\n");
}
rs.close();
stmt.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void endProcess(){
try{
con.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}