日期:2014-05-20  浏览次数:20792 次

求帮助,无法将model中从数据库中获取的数据映射到JTable表中
/**
* author: 
* time:
* function:
*/

package com.dcxt;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Dcxt extends JFrame implements ActionListener{
JPanel jp1,jp2;
JMenuBar jmb;
JMenu jm1,jm2,jm3,jm4;
JComboBox jcb;
JMenuItem jmt1,jmt2;
JTable jt;
JScrollPane jcp;
JLabel jlb1,jlb2;
ImageIcon img;
DCModel dcm;
public Dcxt(){
jp1=new JPanel();
jmb=new JMenuBar();
jm1=new JMenu("鱼类");

jm2=new JMenu("肉类");
jm3=new JMenu("素类");
jm4=new JMenu("汤类");
String []item={"1","2","3","4","5","6","7","8","9","10"};
jcb=new JComboBox(item);



jmt1=new JMenuItem("模糊查询");
jmt1.addActionListener(this);
jmt2=new JMenuItem("价格低到高");
jm1.add(jmt1);
jm2.add(jmt2);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jmb.add(jm4);
jmb.add(jcb);

jp1.add(jmb);

jp2=new JPanel();


jlb1=new JLabel("欢迎使用点菜系统");
img=new ImageIcon("Images/tupian.jpg");
jlb2= new JLabel(img);

   
jp2.add(jlb1,BorderLayout.CENTER);
jp2.add(jlb2,new Integer(Integer.MIN_VALUE));


jp2.setOpaque(false );
jlb1.setBackground(Color.green);
jp2.add(jlb1);
this.add(jp1);
this.add(jp2);
this.setJMenuBar(jmb);
this.setTitle("汤逊湖宾馆点菜系统");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(300, 300);
this.setLocation(300, 300);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Dcxt();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jm1)
{
new Yu();
}
}
}





package com.dcxt;

import java.sql.*;
import java.util.Vector;

import javax.swing.JCheckBox;
import javax.swing.table.AbstractTableModel;

public class DCModel extends AbstractTableModel{

  Vector rowData,columnNames;
JCheckBox jcb;
Connection con=null;
PreparedStatement st=null;
  ResultSet re=null;
   
  private String url="jdbc:oracle:thin:@localhost:1521:ORCL";
private String user="scott";
private String password="tiger";
public DCModel(String sql)
{
columnNames=new Vector();
columnNames.add("菜名");
columnNames.add("菜价");
columnNames.add("选菜");


rowData=new Vector();
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
 
con=DriverManager.getConnection(url, user, password);
//创建语句(发送SQL语句得到数据库)
st=con.prepareStatement(sql);
re=st.executeQuery();
while(re.next())
{
Vector hang=new Vector();
hang.add(re.getString(1));
hang.add(re.getString(2));
jcb=new JCheckBox();
hang.add(jcb);


rowData.add(hang);
}
}catch(Exception e)
{
e.printStackTrace();
}finally{
try{
if(re!=null) re.close();
if(st!=null) st.close();
if(con!=null) con.close();
}catch(Exception e)
{