package lesson17;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import java.io.File;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import javax.swing.JTabbedPane;
import java.awt.Container;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.JScrollPane;
public class Console
{
private JFrame frame=null; private JLabel labtop1=null;
private JLabel labtop2=null;
private JLabel labuse=null;
private JLabel labuse2=null;
private JLabel labsex=null;
private JLabel labbir=null;
private JLabel labsch=null;
private JLabel labadd=null;

private JPanel pan1=null;
private JPanel pan2=null;
private JPanel pan3=null; private JTextField textuse=null;
private JTextField textuse2=null;
private JTextField textsex=null;
private JTextField textbir=null;
private JTextField textsch=null;
private JTextField textadd=null;
private JButton button1=null;
private JButton button2=null;
private JTabbedPane tab=null; public Console()
{
frame=new JFrame("管理控制台");
pan1=new JPanel();
pan2=new JPanel();
pan3=new JPanel(); //信息添加面板
labtop1=new JLabel("学生管理系统");
labuse=new JLabel("姓 名");
labsex=new JLabel("性 别");
labbir=new JLabel("生 日");
labsch=new JLabel("院 系");
labadd=new JLabel("联系地址");
textuse=new JTextField();
textsex=new JTextField();
textbir=new JTextField();
textsch=new JTextField();
textadd=new JTextField();
labtop1.setBounds(250,5,200,100); labuse.setBounds(40,150,100,30);
textuse.setBounds(90,150,150,30); labsex.setBounds(320,150,100,30);
textsex.setBounds(380,150,100,30); labbir.setBounds(40,220,100,30);
textbir.setBounds(90,220,150,30); labsch.setBounds(320,220,100,30);
textsch.setBounds(380,220,100,30); labadd.setBounds(40,290,100,30);
textadd.setBounds(120,290,200,30);
button1=new JButton("添加");
button1.setBounds(240,350,100,30);
//frame.setLayout(null);
pan1.setLayout(null);

//顶头
pan1.add(labtop1);

//姓名栏
pan1.add(labuse);
pan1.add(textuse);
//性别栏
pan1.add(labsex);
pan1.add(textsex);
//生日栏
pan1.add(labbir);
pan1.add(textbir);
//院系
pan1.add(labsch);
pan1.add(textsch);
//联系地址
pan1.add(labadd);
pan1.add(textadd); pan1.add(button1);

//信息查询面板
pan2.setLayout(null);
labtop2=new JLabel("学生管理系统");
button2=new JButton("查询");
labuse2=new JLabel("姓 名");
textuse2=new JTextField();
labtop2.setBounds(250,5,200,100);
button2.setBounds(240,350,100,30);
labuse2.setBounds(200,180,100,30);
textuse2.setBounds(230,180,100,30);
pan2.add(labtop2);
pan2.add(labuse2);
pan2.add(button2);
pan2.add(textuse2); Container cont=frame.getContentPane();
tab=new JTabbedPane(JTabbedPane.TOP);
tab.addTab("信息添加",pan1);
tab.addTab("信息查询",pan2);
frame.add(tab);
frame.setSize(600,450);
frame.setLocation(300,200);
frame.setVisible(true); button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ac)
{
ConnectionDemo02 connd=new ConnectionDemo02();
Connection conn=connd.getconn();
Statement sta=null;
try
{
sta=conn.createStatement();
}
catch (SQLException e)
{
e.printStackTrace();
}

String[] comad={textuse.getText(),textsex.getText(),textbir.getText(),textsch.getText(),textadd.getText()};
if(comad[0]!=null&&comad[1]!=null&&comad[2]!=null&&comad[3]!=null&&comad[4]!=null)
{ String sqlcmod="insert into student(name,sex,birthday,school,address)"+"values('"+comad[0]+"','"+comad[1]+"','"+comad[2]+"','"+comad[3]+"','"+comad[4]+"')";
try
{
sta.executeUpdate(sqlcmod);
}
catch (SQLException e)
{
e.printStackTrace();
}

textuse.setText("");
textsex.setText("");
textbir.setText("");
textsch.setText("");
textadd.setText("");
}
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}

}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ac)
{
ConnectionDemo02 connd=new ConnectionDemo02();
Connection conn=connd.getconn();
Statement sta=null;
ResultSet res=null;
try
{
sta=conn.createStatement();
}
catch (SQLException e)
{
e.printStackTrace();
}

String comad="select id,name,sex,birthday,school,address from student";
try
{
res=sta.executeQuery(comad);
}
catch (SQLException e)
{
e.printStackTrace();
}
Vector<Student> stu=new Vector<Student>();
int i=0;
int j=0;
try
{
while(res.next())
{
int id=res.getInt("id");
String name=res.getString("name");
String sex=res.getString("sex");
String birthday=res.getString("birthday");
String school=res.getString("school");
String address=res.getString("address");
stu.add(new Student(id,name,sex,birthday,school,address));//=======>说这转换错误
System.out.print("编号:"+id+"\t");
System.out.print("姓名:"+name+"\t");
System.out.print("性别:"+sex+"\t");
System.out.print("生日:"+birthday+"\t");
System.out.print("院系:"+school+"\t");
System.out.print("联系地址:"+address+"\n");
    }
}
catch (SQLException e)
{
e.printStackTrace();
}
Vector<String> na=new Vector<String>();
na.add("编号");
na.add("姓名");
na.add("性别");
na.add("生日");
na.add("院系");
na.add("联系地址");
JFrame frame3=new JFrame("查询显示");
JTable table=new JTable(stu,na);
JScrollPane scr=new JScrollPane(table);
frame3.add(scr); frame3.setSize(600,400);
frame3.setVisible(true);
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}

}
});
}
}
 class Student
 {
 int id=0;
 String name=null;
 String sex=null;
 String birthday=null;
 String sch=null;
 String add=null;  public Student(int id,String name,String sex,String bir,String sch,String add)
 {
 this.id=id;
 this.name=name;
 this.sex=sex;
 this.birthday=bir;
 this.sch=sch;
 this.add=add;
 } }

解决方案 »

  1.   

    Vector<Student> stu=new Vector<Student>();
    stu.add(new Student(id,name,sex,birthday,school,address));
    酱紫肯定是没有错的,应该是其他地方吧。。
      

  2.   

    楼上的说法都不正确
    vector的构造方法不能放对象。
    vector()构造一个空向量,使其内部数据数组的大小为 10,其标准容量增量为零。
    vector(Colloction<? extends E> c)构造一个包含指定集合中的元素的向量,这些元素按其集合的迭代器返回元素的顺序排列。
    vector(int initialCapacity)使用指定的初始容量和等于零的容量增量构造一个空向量
    vector(init initialCapacity, int capacity)使用指定的初始容量和容量增量构造一个空的向量你可以这样,选择把student对象里的所有数据放到vector中,再把这个vector放到另一个vector中。Vector<String> v1 = new Vector<String>();
    v1.addElement(stu.getId());
    .....
    Vector<Vector<String>> v2 = new Vector<Vector<String>>();
    v2.add(v1);
      

  3.   

    Vector<Student> stu=new Vector<Student>();
    这样可以的,ls说的vector放到另一个vector,这样也太麻烦了。
      

  4.   

    写代码没看过api。出现这样的错误就不奇怪了。