|
楼主 |
发表于 2014-5-4 15:55:57
|
显示全部楼层
自己解决了问题,发代码让各位帮忙指导下错误,测试完可以使用。
/*按昼间查询数据*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class noises extends JFrame implements ActionListener
{
private String dbURL = "jdbcdbc:SS";
private String user = "";
private String password = "";
JLabel labScore=new JLabel("请输入分贝值:");
JTextField txtScore=new JTextField(10);
JButton btnQuery=new JButton("查询");
JButton btnCancel=new JButton("取消");
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
String []str={"类别","昼间","夜间","适用区域"};
Object[][] data=new Object[4][4];
JTable table=new JTable(data,str);
JTableHeader head=table.getTableHeader();
JScrollPane jsp=new JScrollPane(table);//滚动
Connection con;
Statement sql;
ResultSet rs;
noises()
{
super("噪声标准查询");
setSize(400,300);
pan2.setLayout(new BorderLayout());
pan2.add(head,"North"); //将表头放在面板1的顶部
pan2.add(jsp,"Center"); //表格放在pan1中间
getContentPane().add(pan2,"Center");//把面板1整体加入到主窗口中
pan1.add(labScore);
pan1.add(txtScore);
pan1.add(btnQuery);//以上4条在第一个面板加入内容\
pan1.add(btnCancel);
getContentPane().add(pan1,"North");//把面板2放到窗口顶部
btnQuery.addActionListener(this);
btnCancel.addActionListener(this);//增加查询按钮的监视器
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==btnCancel){
this.dispose();
}
else if(ae.getSource()==btnQuery){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(dbURL,user,password);
String sqlStr="select * from norm where 昼间="+txtScore.getText();//SQL查询语句
Statement st=con.createStatement(); //获取Statement对象
ResultSet rs=st.executeQuery(sqlStr); //执行查询
String 类别,昼间,夜间,适用区域; //查询结果
int i=0;
int j=0;
while (rs.next()){ //遍历ResultSet
data[i][0]=rs.getString("类别"); //获取数据
data[i][1]=rs.getString("昼间");
data[i][2]=rs.getString("夜间");
data[i][3]=rs.getString("适用区域");
//System.out.println(data[i][j]); //在控制台输出数据
i++;
j++;
}
table=new JTable(data,str);
pan2.setLayout(new BorderLayout());
head=table.getTableHeader();
jsp=new JScrollPane(table);
pan2.add(head,"North");
pan2.add(jsp,"Center");
getContentPane().add(pan2,"Center");
this.validate();
con.close(); //关闭连接
}
catch(Exception ex){
ex.printStackTrace(); //输出出错信息
}
}
}
public static void main(String[] args) {
new noises();
}
} |
|