import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;
public class chaxun extends JFrame 
{
Database DB=new Database();
ResultSet rs;
Vector v=new Vector(1,1);
JLabel l1,l2,l3;
JButton b1;
JTextField t1,t2,t3;
//JScrollPane jtableresultpan=new JScrollPane();
DefaultTableModel model=new DefaultTableModel();
JTable jtableresult=new JTable(model);
public chaxun()
{
setSize(400,400);
setLayout(null);
l1=new JLabel();
l1.setText("information system");
l1.setFont(new Font("Dailog",0,20));
l1.setBounds(new Rectangle(120,20,150,40));
l1.setBorder(BorderFactory.createEtchedBorder());
l1.setForeground(Color.red);
add(l1,null);

l2=new JLabel();
l2.setText("put sno");
l2.setBorder(BorderFactory.createEtchedBorder());
l2.setBounds(new Rectangle(60,65,120,20));
add(l2,null);
l3=new JLabel();
l3.setText("put sn");
l3.setBorder(BorderFactory.createEtchedBorder());
l3.setBounds(new Rectangle(190,65,120,20));
add(l3,null);
t1=new JTextField();
t1.setBorder(BorderFactory.createEtchedBorder());
t1.setBounds(new Rectangle(60,90,120,20));
add(t1,null);
t2=new JTextField();
t2.setBorder(BorderFactory.createEtchedBorder());
t2.setBounds(new Rectangle(190,90,120,20));
add(t2,null);
jtableresult.setBounds(new Rectangle(59,119,251,201));
add(jtableresultpan,null);
model.addColumn("sno");
model.addColumn("sn");
jtableresult.setBounds(new Rectangle(60,120,250,200));
add(jtableresult,null);
b1=new JButton();
b1.setText("ok");
b1.setForeground( Color.red);
b1.setBounds(new Rectangle(330,270,50,30));
add(b1,null);
b1.addActionListener(new ActionListener()///////b1 ActionListener start
{
public void actionPerformed(ActionEvent e)
{
String str;
//boolean isFirst=true;
str="select * from xi";
if(!t1.getText().trim().equals(" "))

{
str=str+"where sno="+t1.getText().trim();
//isFirst=false;
}


rs=DB.getResult(str);
boolean isexit=false;
try
{
isexit=rs.first();
}
catch(SQLException e6)
{JOptionPane.showMessageDialog(null,"e6.toString"+e6.toString());}
if(!isexit)
{
JOptionPane.showMessageDialog(null,"shibai");
return ;

}


for(int n=model.getRowCount()-1;n>=0;n--)
model.removeRow(n);
try
{
rs.beforeFirst();
while(rs.next())
{
v.add(rs.getString("sno"));
v.add(rs.getString("sn"));
model.addRow(v);
}}
catch(SQLException e2)
{
JOptionPane.showMessageDialog(null,"shibai");
}

}
});///////////////b1 ActionListener end

setVisible(true);
}

public static void main(String[] args)
{
new chaxun();
}
}////
////请问高手我怎么不能把查询的结果显示在model 上呢?程序设计调试没有报错啊。能不能帮我修下这个代码。或者提供点真确的数据库查询并把结果显示的代码。我不甚感激

解决方案 »

  1.   

    import java.io.*;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.sql.*;
    import javax.swing.table.*;
    import java.util.*;
    public class chaxun extends JFrame 
    {
    Database DB=new Database();
    ResultSet rs;
    Vector v;
    JLabel l1,l2,l3;
    JButton b1;
    JTextField t1,t2,t3;
    JScrollPane jtableresultpan=new JScrollPane();
    DefaultTableModel model=new DefaultTableModel();
    JTable jtableresult=new JTable(model);
    public chaxun()
    {
    setSize(400,400);
    setLayout(null);
    l1=new JLabel();
    l1.setText("information system");
    l1.setFont(new Font("Dailog",0,20));
    l1.setBounds(new Rectangle(120,20,150,40));
    l1.setBorder(BorderFactory.createEtchedBorder());
    l1.setForeground(Color.red);
    add(l1,null);

    l2=new JLabel();
    l2.setText("input sno");
    l2.setBorder(BorderFactory.createEtchedBorder());
    l2.setBounds(new Rectangle(60,65,120,20));
    add(l2,null);
    l3=new JLabel();
    l3.setText("input sn");
    l3.setBorder(BorderFactory.createEtchedBorder());
    l3.setBounds(new Rectangle(190,65,120,20));
    add(l3,null);
    t1=new JTextField();
    t1.setBorder(BorderFactory.createEtchedBorder());
    t1.setBounds(new Rectangle(60,90,120,20));
    add(t1,null);
    t2=new JTextField();
    t2.setBorder(BorderFactory.createEtchedBorder());
    t2.setBounds(new Rectangle(190,90,120,20));
    add(t2,null);
    jtableresult.setBounds(new Rectangle(59,119,251,201));
    add(jtableresultpan,null);
    model.addColumn("sno");
    model.addColumn("sn");
    jtableresultpan.setBounds(new Rectangle(60,120,250,200));///////jtanleresultpan start
    add(jtableresultpan,null);
    //jtableresult.setBounds(new Rectangle(60,120,250,200));//////////jtableresultpan end
    add(jtableresult,null);
    b1=new JButton();
    b1.setText("ok");
    b1.setForeground( Color.red);
    b1.setBounds(new Rectangle(330,270,50,30));
    add(b1,null);
    b1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    String str;
    //boolean isFirst=true;
    if(t1.getText().trim().equals(""))
    {
    JOptionPane.showMessageDialog(null,"please input sno");
    return;}
    str="select * from xi where sno="+Integer.parseInt(t1.getText());



    rs=DB.getResult(str);
    boolean isexit=false;
    try
    {
    isexit=rs.first();
    }
    catch(SQLException e6)
    {JOptionPane.showMessageDialog(null,"e6.toString"+e6.toString());}
    if(!isexit)
    {
    JOptionPane.showMessageDialog(null,"no have");
    return ;

    }

    else
    {
    for(int n=model.getRowCount()-1;n>=0;n--)
    model.removeRow(n);
    try
    {
    rs.beforeFirst();
    while(rs.next())
    {
    v=new Vector(1,1);
    v.add(rs.getString(1));
    v.add(rs.getString(2));
    model.addRow(v);
    }}
    catch(SQLException e2)
    {
    JOptionPane.showMessageDialog(null,"cuowu");
    }}

    }
    });
    jtableresultpan.getViewport().add(jtableresult,null);
    setVisible(true);
    }

    public static void main(String[] args)
    {
    new chaxun();
    }
    }
    //////////////////////////现在是自己修改了点。能够出清晰的界面,但是还是出不了结果。当没有输入信息的时候。也会报错"please input sno",可以推测程序执行到了str="select * from xi where sno="+Integer.parseInt(t1.getText());但是知道执行了后面没有。希望高手指点