程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> sql server-想要從數據庫查詢學生信息顯示到JTable,出現空指針錯誤怎麼辦?

sql server-想要從數據庫查詢學生信息顯示到JTable,出現空指針錯誤怎麼辦?

編輯:編程解疑
想要從數據庫查詢學生信息顯示到JTable,出現空指針錯誤怎麼辦?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;

public class StudentForm extends JFrame implements ActionListener {
private String ss;
//private Vector Vector;

private JLabel welcome = new JLabel("歡迎使用學生管理系統,");
private JLabel userName = new JLabel();
private JLabel name = new JLabel("姓名");
private JLabel sex = new JLabel("性別");
private JLabel birth = new JLabel("出生日期");
private JLabel nation = new JLabel("民族");
private JLabel clas = new JLabel("班級");
private JLabel address = new JLabel("地址");

private JTextField na = new JTextField();
private JTextField se = new JTextField();
private JTextField bi = new JTextField();
private JTextField nat = new JTextField();
private JTextField cl = new JTextField();
private JTextField ad = new JTextField();

private JButton selectClass = new JButton("查詢我的班級");
private JButton selectCourse = new JButton("查詢我的課程");
private JButton selectTest = new JButton("查詢我的考試");

private JTable message = new JTable();      


public StudentForm(String s){
    setVisible(true);
    this.setTitle("學生管理系統");
    this.setSize(500, 180);
    this.setLocation(500,200);

    this.ss = s;
    userName.setText(s+"!");
    JPanel jp1 = new JPanel(new FlowLayout());
    JPanel jp2 = new JPanel(new GridLayout(3,4,10,3));
    JPanel jp3 = new JPanel(new FlowLayout());
    JPanel jp4 =new JPanel(new FlowLayout());
    JPanel j0= new JPanel(new BorderLayout()); 

    jp1.add(welcome);
    jp1.add(userName);

    jp2.add(name);
    jp2.add(na);
    jp2.add(sex);
    jp2.add(se);
    jp2.add(birth);
    jp2.add(bi);
    jp2.add(nation);
    jp2.add(nat);
    jp2.add(clas);
    jp2.add(cl);
    jp2.add(address);
    jp2.add(ad);

    jp3.add(selectClass);
    jp3.add(selectCourse);
    jp3.add(selectTest);

    message.setCellSelectionEnabled(false);
    message.setToolTipText("test tool tip");
    message.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
    message.setShowHorizontalLines(true);
    message.setShowVerticalLines(true);
    JScrollPane scroll=new JScrollPane(message);
    j0.add(scroll,null);

    jp4.add(message);

    j0.add(jp1,"North");
    j0.add(jp2,"Center");
    j0.add(jp3,"South");
    j0.add(jp4,"East");

    setContentPane(j0);

    //布局設計完畢

    selectClass.addActionListener(this);
    selectCourse.addActionListener(this);
    selectTest.addActionListener(this);//按鈕添加監聽器

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource().equals(selectClass)){
        try
        {   
            Statement stmt = LoginForm.ct.createStatement();
            ResultSet rs = stmt.executeQuery("select 班級編號,班級名,班級人數,所屬系別  from "
                    + "[StudentManager].[dbo].[班級信息],[StudentManager].[dbo].[學生信息] "
                    + "where 學生信息.姓名='"+ss+"'and 學生信息.所屬班級=班級信息.班級編號 ");
            //ResultSet rst=stmt.executeQuery(sql);
           // Vector.removeAllElements();
            //message.fireTableStructureChanged();//刷新表格顯示結果集中的內容
            while(rs.next()){
            Vector v=new Vector();
            v.addElement(String.valueOf(rs.getString("班級編號")));
            v.addElement(String.valueOf(rs.getString("班級名")));
           // Vector.addElement(v);
            }
            //tm.fireTableStructureChanged();
            rs.close();
            stmt.close();
          }
          catch(SQLException f) {
            System.out.println("SQL異常");
            f.printStackTrace();
          }

    }

}
public static void main (String args[]){
    StudentForm s1 = new StudentForm("張苗苗");
    s1.setVisible(true);
    s1.pack();

}

}

最佳回答:


根據你的錯誤堆棧,可以將檢查范圍縮小到

public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(selectClass)){
try
{

Statement stmt = LoginForm.ct.createStatement();
ResultSet rs = stmt.executeQuery("select 班級編號,班級名,班級人數,所屬系別 from "
+ "[StudentManager].[dbo].[班級信息],[StudentManager].[dbo].[學生信息] "
+ "where 學生信息.姓名='"+ss+"'and 學生信息.所屬班級=班級信息.班級編號 ");
//ResultSet rst=stmt.executeQuery(sql);
// Vector.removeAllElements();
//message.fireTableStructureChanged();//刷新表格顯示結果集中的內容
while(rs.next()){
Vector v=new Vector();
v.addElement(String.valueOf(rs.getString("班級編號")));
v.addElement(String.valueOf(rs.getString("班級名")));
// Vector.addElement(v);
}
//tm.fireTableStructureChanged();
rs.close();
stmt.close();
}
catch(SQLException f) {
System.out.println("SQL異常");
f.printStackTrace();
}

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved