1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| package day1;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import com.sun.jndi.ldap.EntryChangeResponseControl;
public class AccessConnect1 extends JFrame {
private JPanel jContentPane = null;
private JComboBox jComboBox = null;
Vector dataCombo = new Vector();
private Vector columnNames = new Vector();
Vector columnData = new Vector();
private JScrollPane jScrollPane = null;
private JTable jTable = null;
public static void main(String[] args) {
AccessConnect1 cn = new AccessConnect1();
}
/**
* This is the default constructor
*/
public AccessConnect1() {
super();
initialize();
getDataListCombo();
getDataList();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(432, 200);
this.setContentPane(getJContentPane());
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
public void getDataListCombo() {
//item = (String) jComboBox.getSelectedItem();
int countRows = 0;
//System.out.println(countRows + "----" + item);
String data = "jdbc:odbc:WorldEnergy";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data, "", "");
Statement st = conn.createStatement();
ResultSet rec =
st.executeQuery("SELECT DISTINCT Country FROM Coal ORDER BY Country");
while (rec.next()) {
countRows++;
dataCombo.addElement(rec.getObject(1));
}
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: "+ s.toString()+ " " + s.getErrorCode()
+ " "
+ s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString() + e.getMessage());
}
getJComboBox();
}
public void getDataList() {
//item = (String) jComboBox.getSelectedItem();
int countRows = 0;
//System.out.println(countRows + "----" + item);
String data = "jdbc:odbc:WorldEnergy";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(data, "", "");
Statement st = conn.createStatement();
ResultSet rec =
st.executeQuery("SELECT FIPS, Country, Year FROM Coal " +
//"WHERE " + "(Country='" + item + "') " +
"ORDER BY Country");
ResultSetMetaData md = rec.getMetaData();
int columns = md.getColumnCount();
Vector columnNames = new Vector(columns);
for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i).toString());
}
while (rec.next()) {
countRows++;
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rec.getObject((i)));
}
columnData.addElement(row);
}
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: "+ s.toString()+ " " + s.getErrorCode()
+ " "
+ s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString() + e.getMessage());
}
getJTable();
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if(jComboBox == null) {
jComboBox = new JComboBox();
final DefaultComboBoxModel model = new DefaultComboBoxModel(dataCombo);
jComboBox = new JComboBox(model);
jComboBox.setBounds(185, 17, 223, 25);
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (jComboBox.getSelectedItem().equals("USA")){
//getDataList();
System.out.println(columnData.get(2));
}
}
});
}
return jComboBox;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
//DefaultTableModel td = new DefaultTableModel(columnData);
Vector cl = new Vector();
cl.addElement("Column AFC");
cl.addElement("Column Country");
cl.addElement("Column Year");
if(jTable == null) {
jTable = new JTable();
//jTable = new JTable(columnData,columnNames);
jTable = new JTable(columnData,cl);
}
return jTable;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
//getDataList();
if(jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(15, 61, 394, 99);
jScrollPane.setViewportView(getJTable());
repaint();
}
//jScrollPane.setViewportView(getJTable());
return jScrollPane;
}
} // @jve:visual-info decl-index=0 visual-constraint="12,4" |
Partager