import javax.swing.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.sql.*;
class DataModel extends AbstractTableModel
{
Object[][] data ;
// Prints data when table changes:
public DataModel(Object[][] d)
{
data=d;
}
public int getColumnCount()
{
return data[0].length;
}
public void clear()
{
data=new Object[data.length][data[0].length];
fireTableDataChanged();
}
public int getRowCount()
{
return data.length;
}
public Object getValueAt(int row, int col)
{
return data[row][col];
}
public void setValueAt(Object val, int row, int col)
{
data[row][col] = val;
// Indicate the change has happened:
fireTableDataChanged();
}
public boolean isCellEditable(int row, int col)
{
return true;
}
public void setData(Object[][] dd)
{
data=dd;
fireTableDataChanged();
}
}
public class searchTable1 extends JFrame
{
int i,j;
private JButton m_button=new JButton("search");
private JLabel[] m_label={new JLabel("Title"),new JLabel("Author"),
new JLabel("publisher")};
private JTextField[] m_textField={new JTextField("书名查询",10),new JTextField("作者查询",10),
new JTextField("出版社查询",10)};
private DataModel dm;
public searchTable1(DataModel d)
{
super("search table");
dm=d;
Container container=getContentPane();
container.setLayout(new FlowLayout());
//container.setLayout(new BorderLayout());
JTable table = new JTable(dm);
container.add(new JScrollPane(table));
container.add(m_label[0]);
container.add(m_textField[0]);
container.add(m_label[1]);
container.add(m_textField[1]);
container.add(m_label[2]);
container.add(m_textField[2]);
container.add(m_button);
/* container.add(new JScrollPane(table),BorderLayout.NORTH);
container.add(m_label[0],BorderLayout.WEST);
container.add(m_textField[0],BorderLayout.WEST);
container.add(m_label[1],BorderLayout.CENTER);
container.add(m_textField[1],BorderLayout.CENTER);
container.add(m_label[2],BorderLayout.EAST);
container.add(m_textField[2],BorderLayout.EAST);
container.add(m_button,BorderLayout.SOUTH);*/
SHandler shandler=new SHandler();
m_textField[0].addActionListener(shandler);
m_textField[1].addActionListener(shandler);
m_textField[2].addActionListener(shandler);
m_button.addActionListener(shandler);
dm.setValueAt("title:", 0, 0);
dm.setValueAt("author:", 0, 1);
dm.setValueAt("publisher:", 0, 2);
dm.setValueAt("mydate:", 0, 3);
setSize(500,550);
setVisible(true);
}
private class SHandler implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
dm.clear();
dm.setValueAt("title:", 0, 0);
dm.setValueAt("author:", 0, 1);
dm.setValueAt("publisher:", 0, 2);
dm.setValueAt("mydate:", 0, 3);
int i,j;
i=1;
j=0;
try
{
String connectionURL =
"jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=d:\\ks\\db1.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(connectionURL);
Statement stmt = con.createStatement();
if(ev.getSource()==m_button)
{
if((m_textField[0].getText().trim().equals("书名查询")
&& m_textField[1].getText().trim().equals("作者查询")
&& m_textField[2].getText().trim().equals("出版社查询"))
||(m_textField[0].getText().trim().equals("")
&& m_textField[1].getText().trim().equals("")
&& m_textField[2].getText().trim().equals("")))
{
ResultSet rs =
stmt.executeQuery("select * from book");
while(rs.next())
{
dm.setValueAt(rs.getString("title"), i, j);
dm.setValueAt(rs.getString("author"), i, j+1);
dm.setValueAt(rs.getString("publisher"), i, j+2);
dm.setValueAt(rs.getString("mydate"), i, j+3);
++i;
}
}
else
{
String m_title=m_textField[0].getText().trim();
String m_author=m_textField[1].getText().trim();
String m_publisher=m_textField[2].getText().trim();
if(!(m_textField[0].getText().trim().equals("")))
{
String sqlstr=new String();
sqlstr=String.format("select * from book where title='%s'",m_title);
ResultSet rs =stmt.executeQuery(sqlstr);
while(rs.next())
{
dm.setValueAt(rs.getString("title"), i, j);
dm.setValueAt(rs.getString("author"), i, j+1);
dm.setValueAt(rs.getString("publisher"), i, j+2);
dm.setValueAt(rs.getString("mydate"), i, j+3);
i++;
}
}
else if(!(m_textField[1].getText().trim().equals("")))
{
String sqlstr=new String();
sqlstr=String.format("select * from book where author='%s'",m_author);
ResultSet rs =stmt.executeQuery(sqlstr);
while(rs.next())
{
dm.setValueAt(rs.getString("title"), i, j);
dm.setValueAt(rs.getString("author"), i, j+1);
dm.setValueAt(rs.getString("publisher"), i, j+2);
dm.setValueAt(rs.getString("mydate"), i, j+3);
i++;
}
}
else if(!(m_textField[2].getText().trim().equals("")))
{
String sqlstr=new String();
sqlstr=String.format("select * from book where publisher='%s'",m_publisher);
ResultSet rs =stmt.executeQuery(sqlstr);
while(rs.next())
{
dm.setValueAt(rs.getString("title"), i, j);
dm.setValueAt(rs.getString("author"), i, j+1);
dm.setValueAt(rs.getString("publisher"), i, j+2);
dm.setValueAt(rs.getString("mydate"), i, j+3);
i++;
}
}
}
con.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
/* public static void main(String args[])
{
Object[][] search= new Object[25][4];
DataModel dm=new DataModel(search);
searchTable1 app = new searchTable1(dm);
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}*/
}