i'm writing a small desktop application to store details entered in to a swing ui.this application saves, edits and deletes the records.every thing is working fine but when writing searching functionality when i search the records i'm able to fill the searched record in to the text fields successfully.but if there are more than one record in result set then i will have a problem. so i want to display the resultset in a table format please help me.

here is the sample code:

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import java.sql.ResultSet;

import java.sql.SQLException;

class MyFrame extends Frame implements ActionListener

{

	Panel ButtonsPanel,MoveButtonsPanel,DMLButtonsPanel,DisplayPanel;

	TextField txtDetail1,txtDetail2,txtDetail3,txtDetail6,txtDetail5,txtDetail8,txtDetail7,txtDetail4,txtDetail9,txtDetail10;

	Button Clear,Search,Insert,Update,Delete,Exit;

	Button First,Prev,Next,Last;

	Connection con;

	Statement stmt;

	ResultSet rs;

	MyFrame()

	{

		super("Movie Database");	

		ButtonsPanel = new Panel();

		ButtonsPanel.setLayout(new GridLayout(2,1));

		First = new Button("|<");

		First.addActionListener(this);

		Prev = new Button("<");

		Prev.addActionListener(this);

		Next = new Button(">");

		Next.addActionListener(this);

		Last = new Button(">|");

		Last.addActionListener(this);

		MoveButtonsPanel = new Panel();

		MoveButtonsPanel.add(First);

		MoveButtonsPanel.add(Prev);

		MoveButtonsPanel.add(Next);

		MoveButtonsPanel.add(Last);

		ButtonsPanel.add(MoveButtonsPanel);

		Clear = new Button("Clear");

		Clear.addActionListener(this);

		Search = new Button("Search");

		Search.addActionListener(this);

		Insert = new Button("Insert");

		Insert.addActionListener(this);

		Update = new Button("Update");

		Update.addActionListener(this);

		Delete = new Button("Delete");

		Delete.addActionListener(this);

		Exit = new Button("Exit");

		Exit.addActionListener(this);

		DMLButtonsPanel = new Panel();

		DMLButtonsPanel.add(Clear);

		DMLButtonsPanel.add(Search);

		DMLButtonsPanel.add(Insert);

		DMLButtonsPanel.add(Update);

		DMLButtonsPanel.add(Delete);

		DMLButtonsPanel.add(Exit);

		ButtonsPanel.add(DMLButtonsPanel);

		txtDetail1 = new TextField(20);

		txtDetail3 = new TextField(4);

		txtDetail2 = new TextField(20);

		txtDetail4 = new TextField(20);

		txtDetail5 = new TextField(20);

		txtDetail6 = new TextField(20);

		txtDetail7 = new TextField(4);

		txtDetail8 = new TextField(4);

		txtDetail9 = new TextField(4);

		txtDetail10 = new TextField(20);

		DisplayPanel = new Panel();

		DisplayPanel.setLayout(new GridLayout(10,2));

		DisplayPanel.add(new Label("Detail1"));

		DisplayPanel.add(txtDetail1);

		DisplayPanel.add(new Label("Detail3"));

		DisplayPanel.add(txtDetail3);

		DisplayPanel.add(new Label("Detail2"));

		DisplayPanel.add(txtDetail2);

		DisplayPanel.add(new Label("Detail4"));

		DisplayPanel.add(txtDetail4);

		DisplayPanel.add(new Label("Detail5"));

		DisplayPanel.add(txtDetail5);

		DisplayPanel.add(new Label("Detail6"));

		DisplayPanel.add(txtDetail6);

		DisplayPanel.add(new Label("Detail7"));

		DisplayPanel.add(txtDetail7);

		DisplayPanel.add(new Label("Detail8"));

		DisplayPanel.add(txtDetail8);

		DisplayPanel.add(new Label("Detail9"));

		DisplayPanel.add(txtDetail9);

		DisplayPanel.add(new Label("Detail10"));

		DisplayPanel.add(txtDetail10);

		add(DisplayPanel);

		add(ButtonsPanel,BorderLayout.SOUTH);

		pack();

		setVisible(true);

		getConnected();

	}

	public void actionPerformed(ActionEvent ae)

	{

		if(ae.getSource() == First)

			firstRecord();

		else if(ae.getSource() == Prev)

			previousRecord();

		else if(ae.getSource() == Next)

			nextRecord();

		else if(ae.getSource() == Last)

			lastRecord();

		else if(ae.getSource() == Clear)

			clearDetails();	

		else if(ae.getSource() == Search)

			searchRecord();

		else if(ae.getSource() == Insert)

			insertRecord();

		else if(ae.getSource() == Update)

			updateRecord();

		else if(ae.getSource() == Delete)

			deleteRecord();

		else

			closeWindow();

	}

	public void closeWindow()

	{

		System.exit(0);

	}

	public void firstRecord()

	{

		try

		{

			rs.first();

			fillDetails();

		}catch(SQLException se)

		{

			msgbox("Error Moving to First Record...");

		}

	}

	public void previousRecord()

	{

		try

		{

			rs.previous();

			if(rs.isBeforeFirst())

				rs.first();

			fillDetails();

		}catch(SQLException se)

		{

			msgbox("Error Moving to Previous Record...");

		}

	}

	public void nextRecord()

	{

		try

		{

			rs.next();

			if(rs.isAfterLast())

				rs.last();

			fillDetails();

		}catch(SQLException se)

		{

			msgbox("Error Moving to Next Record...");

		}

	}

	public void lastRecord()

	{

		try

		{

			rs.last();

			fillDetails();

		}catch(SQLException se)

		{

			msgbox("Error Moving to Last Record...");

		}

	}

	public void fillDetails()

	{

		try

		{

			txtDetail1.setText(rs.getString(1));

			txtDetail3.setText(rs.getString(2));

			txtDetail2.setText(rs.getString(3));

			txtDetail4.setText(rs.getString(4));

			txtDetail5.setText(rs.getString(5));

			txtDetail6.setText(rs.getString(6));

			txtDetail7.setText(rs.getString(7));

			txtDetail8.setText(rs.getString(8));

			txtDetail9.setText(rs.getString(9));

			txtDetail10.setText(rs.getString(10));

		}catch(Exception e)

		{

			msgbox("Error Filling Details...");

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

		}

	}

	public void clearDetails()

	{

		try

		{

			txtDetail1.setText("");

			txtDetail3.setText("");

			txtDetail2.setText("");

			txtDetail4.setText("");

			txtDetail5.setText("");

			txtDetail6.setText("");

			txtDetail7.setText("");

			txtDetail8.setText("");

			txtDetail9.setText("");

			txtDetail10.setText("");

		}catch(Exception e)

		{

			msgbox("Error Clearing Details...");

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

		}

	}

	public void searchRecord()

	{

		try

		{

			String SqlStr;

			ResultSet res;

			SqlStr ="SELECT * from DETAIL_DATABASE where Detail10 like '%"+txtDetail1.getText()+"%'";

			res = stmt.executeQuery(SqlStr);

			System.out.println("SqlStr"+SqlStr);

			if(res!=null)

			{

				res.next();

				setTitle("Search Successfull...");

				msgbox(""+res.getString(1)+"|"+res.getString(2)+"|"+res.getString(3)+"|"+res.getString(4)+"|"+res.getString(5)+"|"+res.getString(6)+"|"+res.getString(7)+"|"+res.getString(8)+"|"+res.getString(9)+"|"+res.getString(10)+"");

			}

			else

			msgbox("Search Unsuccessfull");

		}catch(SQLException se)

		{

			System.out.println("Error " + se);

			msgbox("Error Searching Record...");

		}

	}

	public void insertRecord()

	{

		try

		{

			String SqlStr;

			int Result;

			rs.moveToInsertRow();

			rs.updateString(1,txtDetail1.getText());

			rs.updateString(2,txtDetail3.getText());

			rs.updateString(3,txtDetail2.getText());

			rs.updateString(4,txtDetail4.getText());

			rs.updateString(5,txtDetail5.getText());

			rs.updateString(6,txtDetail6.getText());

			rs.updateString(7,txtDetail7.getText());

			rs.updateString(8,txtDetail8.getText());

			rs.updateString(9,txtDetail9.getText());

			rs.updateString(10,txtDetail10.getText());

			rs.insertRow();

			rs.moveToCurrentRow();

			setTitle("Insert Successfull...");

			rs.refreshRow();

		}catch(SQLException se)

		{

			System.out.println("Error " + se);

			msgbox("Error Inserting Record...");

		}

	}	

	public void updateRecord()

	{

		try

		{

			rs.updateString(1,txtDetail1.getText());

			rs.updateString(2,txtDetail3.getText());

			rs.updateString(3,txtDetail2.getText());

			rs.updateString(4,txtDetail4.getText());

			rs.updateString(5,txtDetail5.getText());

			rs.updateString(6,txtDetail6.getText());

			rs.updateString(7,txtDetail7.getText());

			rs.updateString(8,txtDetail8.getText());

			rs.updateString(9,txtDetail9.getText());

			rs.updateString(10,txtDetail10.getText());

			rs.updateRow();

			rs.refreshRow();

			setTitle("Update Successfull...");

		}catch(SQLException ae)

		{

			msgbox("Error Updating Record...");

		}

	}

	public void deleteRecord()

	{

		try

		{

			rs.deleteRow();

			rs.last();

			rs.refreshRow();

			fillDetails();

			setTitle("Delete Successfull...");

		}catch(SQLException e)

		{

			msgbox("Delete Unsuccessfull...");

		}

	}

	public void getConnected()

	{

		try

		{

			setTitle("Getting Connection...");

			Class.forName("com.mysql.jdbc.Driver");

			con = DriverManager.getConnection("jdbc:mysql://localhost/detaildatabase","root","root");

			stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

			rs = stmt.executeQuery("select * from detail_database");

			rs.next();

			fillDetails();

			setTitle("Connected...");

		}catch(SQLException se)

		{

			msgbox("Error getting Connection...");

			System.out.println(se);

		}

		catch(Exception e)

		{

			msgbox("Check your Driver name...");

		}

	}

	public void msgbox(String msg)

	{

		new msgboxDialog(this,msg);

	}

	class msgboxDialog extends Dialog implements ActionListener

	{

		Frame Parent;

		Label lblMsg;

		Button Ok;

		Panel ButtonPanel;

		msgboxDialog(Frame Parent,String msg)

		{

			super(Parent,"Error Dialog");

			this.Parent = Parent;

			lblMsg = new Label(msg);

			Ok = new Button("Ok");

			Ok.addActionListener(this);

			ButtonPanel = new Panel();

			ButtonPanel.add(Ok);

			add(lblMsg);

			add(ButtonPanel,BorderLayout.SOUTH);

			pack();

			setVisible(true);

		}

		public void actionPerformed(ActionEvent ae)

		{

			Parent.setTitle(lblMsg.getText());

			setVisible(false);

			dispose();

		}

	}

}

class DetailDatabase

{

	public static void main(String args[])

	{

		new MyFrame();

	}

}

Recommended Answers

All 5 Replies

In a fraction of the time it took to post that you could have Googled resultset swing table and got any amount of sample code and discussions.

agreed with JamesCherrill

but doesn't allows Update just Read (advice)

ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

all topics from JTable examples works as I excepted

actually i'm pretty new to java.. i'm basically an OCA had good knowledge about sql, pl/sql.so if u can plz guide me how to in a bit clear manner it would help me a lot...

tnq

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.