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 203 204 205 206 207
|
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
public class Ratiotest extends JFrame {
private BorderLayout layoutMain = new BorderLayout();
private JScrollPane scrollPane;
private ResultSetTableModel model;
private JPanel panelCenter = new JPanel();
private JMenuBar menuBar = new JMenuBar();
private JMenu menuFile = new JMenu();
private JMenuItem menuFileExit = new JMenuItem();
private JToolBar toolBar = new JToolBar();
private JButton buttonOpen = new JButton();
private JButton buttonClose = new JButton();
private JButton buttonHelp = new JButton();
private ImageIcon imageOpen = new ImageIcon(ratio.class.getResource("openfile.gif"));
private ImageIcon imageClose = new ImageIcon(ratio.class.getResource("closefile.gif"));
private ImageIcon imageHelp = new ImageIcon(ratio.class.getResource("help.gif"));
private JLabel jLabel1 = new JLabel();
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JLabel jLabel2 = new JLabel();
private JComboBox jComboBox1 = new JComboBox();
private ResultSet rs;
private Connection con;
private Statement stmt;
private static boolean SCROLLABLE = false;
private JTextField jTextField3 = new JTextField();
private JTable jTable1 = new JTable();
public Ratiotest() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// force loading of driver
String url = "jdbc:odbc:dbFinJan08";
String user = "";
String password = "";
con = DriverManager.getConnection(url, user,
password);
stmt = con.createStatement();
DatabaseMetaData md = con.getMetaData();
String sql = "SELECT Name\n" +
"FROM Geo INNER JOIN GeoNames ON Geo.ID = GeoNames.ID where Geo.GeoLevel=3 ;\n";
ResultSet mrs = stmt.executeQuery( sql );
while (mrs.next()){
// System.out.println(mrs.getString(1));
jComboBox1.addItem(mrs.getString(1));
}
//mrs.close();
}
catch(ClassNotFoundException e)
{
System.out.println("Error " + e);
}
catch(SQLException e)
{ System.out.println("Error " + e);
}
}
private void jbInit() throws Exception {
this.setJMenuBar( menuBar );
this.getContentPane().setLayout( layoutMain );
panelCenter.setLayout( null );
this.setSize(new Dimension(661, 509));
menuFile.setText( "File" );
menuFileExit.setText( "Exit" );
menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );
buttonOpen.setToolTipText( "Open File" );
buttonOpen.setIcon( imageOpen );
buttonClose.setToolTipText( "Close File" );
buttonClose.setIcon( imageClose );
buttonHelp.setToolTipText( "About" );
buttonHelp.setIcon( imageHelp );
jLabel1.setText("Date Début");
jLabel1.setBounds(new Rectangle(5, 20, 115, 30));
jTextField1.setBounds(new Rectangle(130, 20, 120, 30));
jTextField2.setBounds(new Rectangle(130, 65, 120, 30));
jLabel2.setText("Date Fin");
jLabel2.setBounds(new Rectangle(5, 65, 115, 30));
jComboBox1.setBounds(new Rectangle(270, 20, 120, 35));
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jComboBox1_actionPerformed(e);
}
});
jTextField3.setBounds(new Rectangle(5, 120, 125, 30));
jTextField3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// jTextField3_actionPerformed(e);
//jTextField3.setText((String)jComboBox1.getSelectedItem());
}
});
jTable1.setBounds(new Rectangle(115, 200, 320, 115));
menuFile.add( menuFileExit );
menuBar.add( menuFile );
toolBar.add( buttonOpen );
toolBar.add( buttonClose );
toolBar.add( buttonHelp );
this.getContentPane().add( toolBar, BorderLayout.NORTH );
// scrollPane.getViewport().add(jTable1, null);
panelCenter.add(jTable1, null);
panelCenter.add(jTextField3, null);
panelCenter.add(jComboBox1, null);
panelCenter.add(jLabel2, null);
panelCenter.add(jTextField2, null);
panelCenter.add(jTextField1, null);
panelCenter.add(jLabel1, null);
this.getContentPane().add(panelCenter, BorderLayout.CENTER);
}
void fileExit_ActionPerformed(ActionEvent e) {
System.exit(0);
}
public void actionPerformed(ActionEvent evt)
{ if (evt.getSource() == jComboBox1)
{ // show the selected table from the combo box
if (scrollPane != null)
getContentPane().remove(scrollPane);
try
{ String tableName
= (String)jComboBox1.getSelectedItem();
// if (rs != null) rs.close();
String query = "SELECT Sum(ClientTrack.Amount) AS SumOfAmount,Count(ClientTrack.Client) AS CountOfClients FROM ((ClientTrack INNER JOIN Loan ON ClientTrack.Loan = Loan.ID) INNER JOIN Table_essai ON Loan.Location = Table_essai.Souszone) INNER JOIN GeoNames ON Table_essai.Zone = GeoNames.ID where Name='"+tableName+"'" ;
rs = stmt.executeQuery(query);
if (SCROLLABLE)
model = new ScrollingResultSetTableModel(rs);
else
model = new CachingResultSetTableModel(rs);
JTable jTable1 = new JTable(model);
}
catch(SQLException e)
{ System.out.println("Error " + e);
}
catch(Exception e) {
System.out.print("erreur1 "+e.getMessage());//récuperer le message d'erreur en cas d'erreur
}
}
}
public static void main(String args[]){
Ratiotest rati=new Ratiotest();
rati.show();
}
private void jComboBox1_actionPerformed(ActionEvent e) {
jTextField3.setText((String)jComboBox1.getSelectedItem());
}
private void jFormattedTextField1_actionPerformed(ActionEvent e) {
}
private void jTextField3_actionPerformed(ActionEvent e) {
}
} |
Partager