Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Database operations in Java 1

Status
Not open for further replies.

yigit

Technical User
Jun 28, 2006
14
0
0
TR
Hi everyone,
i tried to do 2 operations on a database table; insert and delete. There are several textfields requesting name, lastname etc. When these fields are filled, it should insert the data to the appropriate columns of a row in the table. Delete should delete the current row elements in the table.The code is below, any ideas? i tried to do a seperate class just to connect and to the operations but it got a bit messed up:).

public class DBGUIMain extends JPanel {

private JTextField[] fields;

// Create a form with the specified labels, tooltips, and sizes.
public DBGUIMain(String[] labels, char[] mnemonics,
int[] widths, String[] tips) {
super(new BorderLayout());
JPanel labelPanel = new JPanel(new GridLayout(labels.length, 1));
JPanel fieldPanel = new JPanel(new GridLayout(labels.length, 1));
JPanel buttonPanel = new JPanel(new GridLayout(labels.length, 1));

add(labelPanel, BorderLayout.WEST);
add(fieldPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
fields = new JTextField[labels.length];

for (int i=0; i < labels.length; i+=1) {
fields = new JTextField();
if (i < tips.length) fields.setToolTipText(tips);
if (i < widths.length) fields.setColumns(widths);

JLabel lab = new JLabel(labels, JLabel.RIGHT);
lab.setLabelFor(fields);
if (i < mnemonics.length) lab.setDisplayedMnemonic(mnemonics);

labelPanel.add(lab);
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
p.add(fields);
fieldPanel.add(p);
}
}

public String getText(int i) {
return( fields.getText() );
}

public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
String[] labels = { "First Name", "Last Name", "Birth Date", "Last Logon Date" , "ONLINE(y/n)?"};
char[] mnemonics = { 'F', 'S', 'B', 'L' , 'O'};
int[] widths = { 15, 15, 15, 15, 5 };
String[] descs = { "First Name", "Last Name", "Birth Date", "Last Logon Date" , "ONLINE(y/n)??" };

final DBGUIMain form = new DBGUIMain(labels, mnemonics, widths, descs);

JButton insert = new JButton("INSERT");
JButton delete = new JButton("DELETE");

insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("INSERT");


}
}
);
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("DELETE");
}
}
);

JFrame f = new JFrame("Text Form Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(form, BorderLayout.NORTH);
JPanel p = new JPanel();
p.add(insert);
p.add(delete);
f.getContentPane().add(p, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}
 
i forgot to say,, sql is supposed to be used for connection
thanks
 
To get you started with JDBC database access, this might help:-

You don't say which database, but this article assumes MySQL. Shouldn't make too much difference, though.

One word of caution. Don't put database code into your GUI class. Keep visual presentation classes and data persistence classes separate, perhaps using a Java Interface between them.

One way of doing this is using the Data Access Object pattern. A quick google will get you loads of articles.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top