Insertion into table using Swing

Insertion into table using Swing


Posted in : Core Java Posted on : October 18, 2010 at 6:52 PM Comments : [ 0 ]

This section contains the detail about the Insertion into table using Swing in java.

Insert into table using Swing

In this section, We will insert rows into "Mysql" database using "Swing".

What is Swing?

Swing is the extension to the Awt library, includes new and improved components that enhance the look and functionality of GUIs. It's components are light weight than Awt component. Swing can be used to build Standalone GUI Application as well as  Servlets and Applets. Swing is more portable and more flexible than AWT.

inswing.java


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

// INSERT NEW RECORD

public class Inswing implements ActionListener {
	JFrame fr;
	JPanel po;

	JLabel l1, l2, l4, l11, main;
	JTextField tf1, tf2;

	JComboBox S1, gender;
	GridBagConstraints gbc;
	GridBagLayout go;
	JButton ok, exit;

	public Inswing() {
	fr = new JFrame("New Record Entry");
	Font f = new Font("Verdana", Font.BOLD, 24);

	// fr=new JFrame();
	po = new JPanel();
	fr.getContentPane().add(po);
	fr.setVisible(true);
	fr.setSize(1024, 768);

	fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	po.setBackground(Color.orange);
	go = new GridBagLayout();
	gbc = new GridBagConstraints();
	po.setLayout(go);

	main = new JLabel("		 NEW RECORD ENTRY	       ");
	main.setFont(f);

	l1 = new JLabel("Student ID :");
	tf1 = new JTextField(6);

	l2 = new JLabel("Student Name  :");
	tf2 = new JTextField(20);

	l4 = new JLabel("Gender :");
	String str1[] = { "Male", "Female" };
	gender = new JComboBox(str1);

	l11 = new JLabel("Branch  :");
	String str[] = { "Computer Science", "Electrical",
			"Electrical & Electronics", "Information Technology",
			"Mechanical" };
	S1 = new JComboBox(str);

	ok = new JButton("Accept");

	exit = new JButton("Exit");

	S1.addActionListener(this);

	gender.addActionListener(this);

	// ///////////////////////////////////////

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 0;
	gbc.gridy = 0;
	go.setConstraints(l1, gbc);
	po.add(main);

	// a

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 5;
	gbc.gridy = 5;
	go.setConstraints(l1, gbc);
	po.add(l1);

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 10;
	gbc.gridy = 5;
	go.setConstraints(tf1, gbc);
	po.add(tf1);

	// b

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 5;
	gbc.gridy = 10;
	go.setConstraints(l2, gbc);
	po.add(l2);

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 10;
	gbc.gridy = 10;
	go.setConstraints(tf2, gbc);
	po.add(tf2);

	// d

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 5;
	gbc.gridy = 20;
	go.setConstraints(l4, gbc);
	po.add(l4);

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 10;
	gbc.gridy = 20;
	go.setConstraints(gender, gbc);
	po.add(gender);

	// i

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 5;
	gbc.gridy = 60;
	go.setConstraints(l11, gbc);
	po.add(l11);

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 10;
	gbc.gridy = 60;
	go.setConstraints(S1, gbc);
	po.add(S1);

	// m

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 10;
	gbc.gridy = 90;
	go.setConstraints(ok, gbc);
	po.add(ok);

	ok.addActionListener(this);

	// n

	gbc.anchor = GridBagConstraints.WEST;
	gbc.gridx = 11;
	gbc.gridy = 90;
	go.setConstraints(exit, gbc);
	po.add(exit);

	exit.addActionListener(this);

	}

	public void actionPerformed(ActionEvent ev) {

	if (ev.getSource() == ok) {

		try {
		Connection con;
		JLabel last = new JLabel("Data inserted successfully");
		Class.forName("com.mysql.jdbc.Driver");
		con = DriverManager
				.getConnection("jdbc:mysql://192.168.10.13:3306/ankdb",
						"root", "root");

		PreparedStatement ps = con
				.prepareStatement("Insert into studentrecord values(?,?,?,?)");

		String id = tf1.getText();
		String sname = tf2.getText();

		String br = S1.getSelectedItem().toString();
		String gen = gender.getSelectedItem().toString();

		ps.setString(1, id);
		ps.setString(2, sname);

		ps.setString(3, gen);
		ps.setString(4, br);

		ps.executeUpdate();
		con.close();

		tf1.setText("");
		tf2.setText("");
		po.add(last);
		} catch (Exception e) {

			System.out.println("ERROR  " + e);

		}

		}

		if (ev.getSource() == exit) {

			fr.dispose();

		}
	}

	public static void main(String a[]) {
		new Inswing();

	}

}

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics