swings using JDBC

how to search the data from the database using swings and JDBC?

Page 1 of 1

4 Replies - 10051 Views - Last Post: 09 May 2009 - 11:44 PM Rate Topic: -----

#1 sruthijain2003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 09

swings using JDBC

Post icon  Posted 08 May 2009 - 06:33 AM

import java.awt.*;
import java.io.*;
import javax.swing.table.*;
import javax.swing.*;
import java.sql.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.util.*;
public class first extends JFrame implements ActionListener
{
String s1="Welcome to Chennai Library";
JPanel p1=(JPanel)getContentPane();
JButton b1=new JButton("Search books by title");
JButton b2=new JButton("exit");

public first()
{
try
{	
JLabel jl1=new JLabel("			WELCOME TO  LIBRARY INFORMATION SYSTEMS					");
jl1.setFont(new Font("Comic Sans",Font.BOLD,28));
p1.add(jl1);
p1.setLayout(new FlowLayout(100,100,100));
b1.addActionListener(this);
b2.addActionListener(this);
p1.add(b1);
p1.add(b2);
myactions my=new myactions();
addWindowListener(my);
}
catch(Exception ex){}

}
class myactions extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class seconds extends JFrame
{
public seconds()
{
try
{
JPanel p2=(JPanel)getContentPane();
p2.setLayout(new FlowLayout());
JLabel jl2=new JLabel("Enter the book title");
String s2="Text";
final JTextField jt2=new JTextField(s2,25);
final JButton jb2=new JButton("Search");
final JButton jb21=new JButton("Cancel");
p2.add(jl2);
p2.add(jt2);
p2.add(jb2);
p2.add(jb21);
setSize(500,500);
setVisible(true);
setTitle("search books by title");
p2.repaint();
jb21.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb21)
{
dispose();
}
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==jb2)
{
try
{

DefaultTableModel dt=new DefaultTableModel();
JTable jt=new JTable(dt);
 
Object[] data=new Object[6];
try{

	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		
	   Connection cp=DriverManager.getConnection("jdbc:odbc:data1","","");
Statement st=cp.createStatement();

[color=#3333FF][color=#CC6600]ResultSet rs=st.executeQuery("select booktitle,author from booklist where booktitle=jt2.getText()");[/color][/color]
ResultSetMetaData rt=rs.getMetaData();

int row=rt.getColumnCount();			
for(int j=1;j<=row;j++)
{
	System.out.println(rt.getColumnName(j));

dt.addColumn(rt.getColumnName(j));
}
 
while(rs.next())
{



for(int j=0;j<row;j++)

{
 data[j]=rs.getString(j+1);
}
dt.addRow(data);
}
 JTableHeader header = jt.getTableHeader();
	header.setBackground(Color.yellow);
	JScrollPane pane = new JScrollPane(jt);
//Container cp1=getContentPane(); 
 
frame2=new JFrame("book list");
	frame2.add(pane);
	frame2.setVisible(true);
	frame2.setBounds(610,0,400,200);
}
catch(Exception e){}
}



catch(Exception exe){}

}
}
});
}
catch(Exception exe){}

}

}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
seconds s=new seconds();
}
else if(e.getSource()==b2)
{
System.exit(0);
}
}
public static void main(String args[])
{
first f=new first();
f.setTitle("home");
f.setSize(500,500);
f.setVisible(true);
}
}



i had created a table called booklist using MS-Access....in that table,i created four fields : book id, title,author and course....and i added several records to this table....also i created a java program using swings and jdbc...
after the compilation and running of the above program,i got the output. When i clicked on one button"search" after typing the data(book title) to be searched from the database, no operation was performed....and also the record containing the searched data was not displayed....
also i don't know whether the statement given below is perfectly correct or not...
ResultSet rs=st.executeQuery("select booktitle,author from booklist where booktitle=jt2.getText()");"


kindly please enlighten me and point what mistakes i had made in the above program...please help me....

Is This A Good Question/Topic? 0
  • +

Replies To: swings using JDBC

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: swings using JDBC

Posted 08 May 2009 - 03:22 PM

The title of your topic by itself requires some explanation.

Swing and JDBC has nothing to do together.

Swing is a Graphical Interface.
JDBC is to access Database.

You shouldn't integrate this 2 for any consideration.

You should have a part of your program (or even class) that handle the data and the way it is store/retreive from the database.
You should have a part of your program (or even class) that handle the GUI.

You should have a part of your program that bridges, the less coupled as possible, these 2 elements.

If you decide to switch from GUI to console program you shouldn't have to change anything in the database part.
If you decide to change from Access, to Oracle, to HSQLdb or to MySQL ior to simple flat files you shouldn't have to change the GUI part of your program.

So, do you have a Swing or a JDBC question ?
Was This Post Helpful? 0
  • +
  • -

#3 sruthijain2003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 09

Re: swings using JDBC

Posted 09 May 2009 - 05:57 AM

View Postpbl, on 8 May, 2009 - 02:22 PM, said:

The title of your topic by itself requires some explanation.

Swing and JDBC has nothing to do together.

Swing is a Graphical Interface.
JDBC is to access Database.

You shouldn't integrate this 2 for any consideration.

You should have a part of your program (or even class) that handle the data and the way it is store/retreive from the database.
You should have a part of your program (or even class) that handle the GUI.

You should have a part of your program that bridges, the less coupled as possible, these 2 elements.

If you decide to switch from GUI to console program you shouldn't have to change anything in the database part.
If you decide to change from Access, to Oracle, to HSQLdb or to MySQL ior to simple flat files you shouldn't have to change the GUI part of your program.

So, do you have a Swing or a JDBC question ?





i had used the inner classes for that above program.....is there any other way to do that?....i want the records from the database to be displayed based on the text being entered in a text box...i'm not switching anything...but using GUI as front end and MS-Acess as back end...
also i don't have ORACLE and MYSQL on my system....one more thing....please tell me more about that as i'm a beginner to JDBC.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: swings using JDBC

Posted 09 May 2009 - 08:23 AM

You appear to be looking for a book whose title is a piece of Java code ;-)

Quote

ResultSet rs=st.executeQuery("select booktitle,author from booklist where booktitle=jt2.getText()");"


ResultSet rs=st.executeQuery("select booktitle,author from booklist where booktitle='" + jt2.getText() + "'");



but you'd be better to use a PreparedStatement as it will handle escapement and quoting for you
Was This Post Helpful? 0
  • +
  • -

#5 sruthijain2003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 09

Re: swings using JDBC

Posted 09 May 2009 - 11:44 PM

View Postg00se, on 9 May, 2009 - 07:23 AM, said:

You appear to be looking for a book whose title is a piece of Java code ;-)

Quote

ResultSet rs=st.executeQuery("select booktitle,author from booklist where booktitle=jt2.getText()");"


ResultSet rs=st.executeQuery("select booktitle,author from booklist where booktitle='" + jt2.getText() + "'");



but you'd be better to use a PreparedStatement as it will handle escapement and quoting for you

prepared statement.....hmmm....thanks for ur help...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1