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....

New Topic/Question
Reply




MultiQuote




|