java在图形界面显示数据库中的数据问题?
publicJMenugetJMenu2(Stringtit1){JMenujmu=newJMenu();jmu.setText(tit1);jmit2=getJMenu...
public JMenu getJMenu2(String tit1){
JMenu jmu = new JMenu();
jmu.setText(tit1);
jmit2 = getJMenuItem("查询");
jmit2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Statement stmt = null;
ResultSet rs = null;
String[] colHeads={"编 号","账 号","密 码","性 别","学 历","Email","Q Q","生 日"};
//窗体
JFrame jf = new JFrame();
//面板
JPanel jpl = new JPanel();
jpl.setLayout(null); //设置布局方式为自定义
int i=0;
Object[][] base = new Object[1][9];
try {
Sqlsever data = new Sqlsever();//连接数据库
con = data.getDatabase(con);
stmt = con.createStatement();
rs=stmt.executeQuery("select * from member");
while(rs.next()){
base[i][0] = rs.getInt("memID");
base[i][1] = rs.getString("memNum");
base[i][2] = rs.getString("memPsw");
base[i][3] = rs.getString("memSex");
base[i][4] = rs.getString("memSch");
base[i][5] = rs.getString("memLike");
base[i][6] = rs.getString("memEmail");
base[i][7] = rs.getInt("memQQ");
base[i][8] = rs.getDate("memBtday");
i++;
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
JTable table = new JTable(base,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
table.setPreferredScrollableViewportSize(new Dimension(500, 400));
getContentPane().add(jsp, BorderLayout.NORTH);
//背景图片
JLabel pitLabel1=new JLabel(new ImageIcon("images/bg1.jpg"));
pitLabel1.setBounds(0, -10, 600, 500);
jpl.add(pitLabel1);
jf.setSize(600,500);
jf.setResizable(false); //设置窗体大小不可变
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setIconImage(new ImageIcon("images/icon.gif").getImage());
jf.setContentPane(table);
jf.setContentPane(jpl);
jf.setVisible(true);
}
});
我这是我写的代码,我想达到的效果是,点击菜单栏的“查询”,就弹出一个新的窗体显示出数据库中的数据!但是现在新的窗体中显示不出数据库中的内容!
请高手帮忙解决! 展开
JMenu jmu = new JMenu();
jmu.setText(tit1);
jmit2 = getJMenuItem("查询");
jmit2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Statement stmt = null;
ResultSet rs = null;
String[] colHeads={"编 号","账 号","密 码","性 别","学 历","Email","Q Q","生 日"};
//窗体
JFrame jf = new JFrame();
//面板
JPanel jpl = new JPanel();
jpl.setLayout(null); //设置布局方式为自定义
int i=0;
Object[][] base = new Object[1][9];
try {
Sqlsever data = new Sqlsever();//连接数据库
con = data.getDatabase(con);
stmt = con.createStatement();
rs=stmt.executeQuery("select * from member");
while(rs.next()){
base[i][0] = rs.getInt("memID");
base[i][1] = rs.getString("memNum");
base[i][2] = rs.getString("memPsw");
base[i][3] = rs.getString("memSex");
base[i][4] = rs.getString("memSch");
base[i][5] = rs.getString("memLike");
base[i][6] = rs.getString("memEmail");
base[i][7] = rs.getInt("memQQ");
base[i][8] = rs.getDate("memBtday");
i++;
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
JTable table = new JTable(base,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
table.setPreferredScrollableViewportSize(new Dimension(500, 400));
getContentPane().add(jsp, BorderLayout.NORTH);
//背景图片
JLabel pitLabel1=new JLabel(new ImageIcon("images/bg1.jpg"));
pitLabel1.setBounds(0, -10, 600, 500);
jpl.add(pitLabel1);
jf.setSize(600,500);
jf.setResizable(false); //设置窗体大小不可变
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setIconImage(new ImageIcon("images/icon.gif").getImage());
jf.setContentPane(table);
jf.setContentPane(jpl);
jf.setVisible(true);
}
});
我这是我写的代码,我想达到的效果是,点击菜单栏的“查询”,就弹出一个新的窗体显示出数据库中的数据!但是现在新的窗体中显示不出数据库中的内容!
请高手帮忙解决! 展开
2个回答
展开全部
public JMenu getJMenu2(String tit1) {
JMenu jmu = new JMenu();
jmu.setText(tit1);
jmit2 = new JMenuItem("查询");
jmit2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Statement stmt = null;
ResultSet rs = null;
final String[] colHeads = {"编 号", "账 号", "密 码", "性 别", "学 历", "爱 好", "Email", "Q Q", "生 日"};
Object[][] base = new Object[10][9];
JFrame jf = new JFrame();
JPanel jpl = new JPanel();
try {
con = Sqlsever.getDatabase(con);
stmt = con.createStatement();
int i = 0;
rs = stmt.executeQuery("select * from member");
while (rs.next()) {
base[i][0] = rs.getInt("memID");
base[i][1] = rs.getString("memNum");
base[i][2] = rs.getString("memPsw");
base[i][3] = rs.getString("memSex");
base[i][4] = rs.getString("memSch");
base[i][5] = rs.getString("memLike");
base[i][6] = rs.getString("memEmail");
base[i][7] = rs.getString("memQQ");
base[i][8] = rs.getString("memBtday");
i++;
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
JTable table = new JTable(base, colHeads);
table.setPreferredScrollableViewportSize(new Dimension(600, 100));//设置表格的大小
JScrollPane pane3 = new JScrollPane(table);
jpl.setPreferredSize(new Dimension(600, 400));
jpl.add(pane3);
JLabel pitLabel1 = new JLabel(new ImageIcon("images/bg1.jpg"));
pitLabel1.setBounds(0, -10, 600, 500);
jpl.add(pitLabel1);
jf.setContentPane(jpl);
jf.setIconImage(new ImageIcon("images/icon.gif").getImage());
jf.pack();
jf.show();
}
});
JMenu jmu = new JMenu();
jmu.setText(tit1);
jmit2 = new JMenuItem("查询");
jmit2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Statement stmt = null;
ResultSet rs = null;
final String[] colHeads = {"编 号", "账 号", "密 码", "性 别", "学 历", "爱 好", "Email", "Q Q", "生 日"};
Object[][] base = new Object[10][9];
JFrame jf = new JFrame();
JPanel jpl = new JPanel();
try {
con = Sqlsever.getDatabase(con);
stmt = con.createStatement();
int i = 0;
rs = stmt.executeQuery("select * from member");
while (rs.next()) {
base[i][0] = rs.getInt("memID");
base[i][1] = rs.getString("memNum");
base[i][2] = rs.getString("memPsw");
base[i][3] = rs.getString("memSex");
base[i][4] = rs.getString("memSch");
base[i][5] = rs.getString("memLike");
base[i][6] = rs.getString("memEmail");
base[i][7] = rs.getString("memQQ");
base[i][8] = rs.getString("memBtday");
i++;
}
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
JTable table = new JTable(base, colHeads);
table.setPreferredScrollableViewportSize(new Dimension(600, 100));//设置表格的大小
JScrollPane pane3 = new JScrollPane(table);
jpl.setPreferredSize(new Dimension(600, 400));
jpl.add(pane3);
JLabel pitLabel1 = new JLabel(new ImageIcon("images/bg1.jpg"));
pitLabel1.setBounds(0, -10, 600, 500);
jpl.add(pitLabel1);
jf.setContentPane(jpl);
jf.setIconImage(new ImageIcon("images/icon.gif").getImage());
jf.pack();
jf.show();
}
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询