Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 02-06-2004, 09:49 AM   #1
Beta Member
 
Join Date: Feb 2004
Posts: 4
Default Button handlers

hi fairly new to java. am trying to create a GUI in java that uses jdbc to access ms access.i have done this as seen below. any ideas though how i can add buttons such as show quanitity and edit as struggliling to do so
import java.net.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class ViewDB
{ public static void main(String[] args)
{ JFrame frame = new ViewDBFrame();
frame.show();
}
}

class ViewDBFrame extends JFrame
implements ActionListener
{ public ViewDBFrame()
{ setTitle("ViewDB");
setSize(800, 700);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );

Container contentPane = getContentPane();

tableNames = new JComboBox();
tableNames.addActionListener(this);

dataPanel = new JPanel();
contentPane.add(dataPanel, "Center");

nextButton = new JButton("Next");
nextButton.addActionListener(this);
JPanel p = new JPanel();
p.add(nextButton);
contentPane.add(p, "South");

fields = new ArrayList();

try
{

// Load the Driver class file

String chemin = "m:rich.mdb";
String fullConnectionString = "jdbc:odbc:Driver={Microsoft Access Driver (db1.mdb)};DBQ="
+ chemin;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// con = getConnection(fullConnectionString);
con = DriverManager.getConnection(fullConnectionString);

// Create the statement
stmt = con.createStatement();


// Metadata of access tables
md = con.getMetaData();

ResultSet mrs = md.getTables(null, null, null,
new String[] { "TABLE" });
while (mrs.next())
tableNames.addItem(mrs.getString(3));
mrs.close();
}
catch (SQLException ex)
{ System.out.println ("SQLException:");
while (ex != null)
{ System.out.println ("SQLState: "
+ ex.getSQLState());
System.out.println ("Message: "
+ ex.getMessage());
System.out.println ("Vendor: "
+ ex.getErrorCode());
ex = ex.getNextException();
System.out.println ("");
}
}


catch(Exception e)
{ JOptionPane.showMessageDialog(this, e);
}

contentPane.add(tableNames, "North");
}




private void add(Container p, Component c,
GridBagConstraints gbc, int x, int y, int w, int h)
{ gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = w;
gbc.gridheight = h;
p.add(c, gbc);
}

public void actionPerformed(ActionEvent evt)
{ if (evt.getSource() == nextButton)
{ showNextRow();
}
else if (evt.getSource() == tableNames)
{ remove(dataPanel);
dataPanel = new JPanel();
fields.clear();
dataPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 100;

try
{ String tableName
= (String)tableNames.getSelectedItem();
if (rs != null) rs.close();
rs = stmt.executeQuery("SELECT * FROM "
+ tableName);
// rs = stmt.executeQuery("DELETE * FROM "
// + tableName);
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++)
{ String columnName = rsmd.getColumnLabel(i);
int columnWidth = rsmd.getColumnDisplaySize(i);
JTextField tb = new JTextField(columnWidth);
fields.add(tb);

gbc.weightx = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
add(dataPanel, new JLabel(columnName),
gbc, 0, i - 1, 1, 1);

gbc.weightx = 100;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(dataPanel, tb, gbc, 1, i - 1, 1, 1);
}
}
catch(Exception e)
{ JOptionPane.showMessageDialog(this, e);
}
getContentPane().add(dataPanel, "Center");
doLayout();
pack();

showNextRow();
}
}

public void showNextRow()
{ if (rs == null) return;
{ try
{ if (rs.next())
{ for (int i = 1; i <= fields.size(); i++)
{ String field = rs.getString(i);
JTextField tb
= (JTextField)fields.get(i - 1);
tb.setText(field);
}
}
else
{ rs.close();
rs = null;
}
}
catch(Exception e)
{ System.out.println("Error " + e);
}
}
}

private JButton nextButton;
private JPanel dataPanel;
private JComboBox tableNames;
private ArrayList fields;

private Connection con;
private Statement stmt;
private DatabaseMetaData md;
private ResultSet rs;
}
__________________

rdaley is offline   Reply With Quote
Old 02-06-2004, 11:15 AM   #2
Site Team
 
David Lindon's Avatar
 
Join Date: Dec 2002
Posts: 15,233
Default Re: Button handlers

Just disabled smilies for you as they were showing up instead of your code
__________________
[url=http://www.LNXPS.NET]LNXPS.NET - The XPS Library]
David Lindon is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 08:36 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0