|
-
Help urgent
HI
I'm having problems
I have a frame that has a textField, button, label.
The button is used to diplay the Jtable which has the resultSet.
My query is:
String query = "SELECT personID, firstName, surname FROM Person where firstName
= '" + jTxtTest.getText() + "'";
If I enter Gita in the textField and press the button it should display the
personID, firstName, surname where firtsname = Gita
in the Jtable and also display Gita in the label.
But it only display Gita in the Label but noting in the Jtable.
What am i doing wrong?????
public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();;
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String url="jdbc dbc:myDatabase";
String tempname = "";
int tempcnt;
JTabbedPane tabbedPane = new JTabbedPane();
Object[] data = new Object[20];
DefaultTableModel defaulttablemodel = new DefaultTableModel();
JTable jtable = new JTable(defaulttablemodel);
JPanel p1 = new JPanel();
JTabbedPane jTabbedPane1 = new JTabbedPane();
JPanel jPanel1 = new JPanel();
JTextField jTxtTest = new JTextField();
JButton jButton1 = new JButton();
JLabel jLabel1 = new JLabel();
/**Construct the frame*/
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
setup();
contentPane = (JPanel) this.getContentPane();
this.setSize(new Dimension(554, 532));
this.setTitle("Frame Title");
jPanel1.setLayout(null);
jTxtTest.setBounds(new Rectangle(53, 20, 208, 33));
jButton1.setText("jButton1");
jButton1.setBounds(new Rectangle(314, 19, 130, 36));
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jLabel1.setBounds(new Rectangle(462, 23, 82, 20));
contentPane.add(jTabbedPane1, BorderLayout.CENTER);
jTabbedPane1.add(jPanel1, "jPanel1");
jPanel1.add(jTxtTest, null);
jPanel1.add(jButton1, null);
jPanel1.add(jLabel1, null);
}
void setup()
{
setupMenuBar();
showpane1();
//contentPane.add(tabbedPane, BorderLayout.CENTER);
}
//------------------------------------------------------------ End setup
-----------------
//============================================================ Start setupMenuBar
========
void setupMenuBar()
{
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem fileExit = new MenuItem("Exit");
fileExit.addActionListener(new MenuItemHandler());
fileMenu.add(fileExit);
MenuItem filePrev = new MenuItem("Preview");
filePrev.addActionListener(new MenuItemHandler());
fileMenu.add(filePrev);
menuBar.add(fileMenu);
setMenuBar(menuBar);
}
//------------------------------------------------------------ End setupMenuBar-----------
//============================================================ Start showpane1
=========
void showpane1() // REPORTS TAB WITH JTABLE
{
p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Student
Reports 1"));
p1.setBounds(new Rectangle(28, 70, 513, 429));
try
{
Class.forName(driver);
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String query = "SELECT personID, firstName, surname FROM Person where
firstName = '" + jTxtTest.getText() + "'";
ResultSet rs = statement.executeQuery(query);
if (rs == null)
{
System.out.println("cannot execute
query");
}
ResultSetMetaData rmeta = rs.getMetaData();
int numColumns=rmeta.getColumnCount();
for(int i=1;i<=numColumns;++i)
{
if(i<=numColumns)
{
defaulttablemodel.addColumn(rmeta.getColumnName(i));
}
}
while(rs.next())
{
for(int i=1;i<=numColumns;++i)
{
if(i<=numColumns)
{
tempname = rs.getString(i);
tempcnt=i-1;
data[tempcnt] = tempname;
}
}
defaulttablemodel.addRow(data);
}
}
catch(Exception ex)
{
//
}
p1.add(new JScrollPane(jtable));
}
//------------------------------------------------------------ End showpane1
------------
//=========================================================== START MenuItemHandler
======
class MenuItemHandler implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
String s=ev.getActionCommand();
if(s=="Exit")
{
System.exit(0);
}
else if (s=="Preview")
{
///
}
}
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
jLabel1.setText(jTxtTest.getText());
jPanel1.add(p1);
contentPane.repaint();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks