显示
我想要的结果
意图是一个登录界面成功后跳转到查询界面,查询界面是单独的一个类,我把这个类new在了登录按钮的监听器里头。单独运行可以成功,但是整体运行就会报错,报错提醒java.lang.NullPointerException: Cannot invoke "java.util.List.clear()" because "this.records" is null。
部分主界面
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.util.ArrayList;
public class Login_Register extends JFrame{
Login_Register() {
init();
}
//登录界面初始化
public void init() {
JFrame frame = new JFrame("学生管理系统");
frame.setLayout(null);
JLabel nameStr = new JLabel("账号:");
nameStr.setBounds(250, 200, 100, 25);
frame.add(nameStr);
JLabel passwordStr = new JLabel("密码:");
passwordStr.setBounds(250, 250, 100, 25);
frame.add(passwordStr);
JTextField userID = new JTextField();
userID.setBounds(300, 200, 150, 25);
frame.add(userID);
JTextField password = new JTextField();
password.setBounds(300, 250, 150, 25);
frame.add(password);
JButton buttonlogin = new JButton("登录");
buttonlogin.setBounds(275, 300, 70, 25);
frame.add(buttonlogin);
JButton buttonregister = new JButton("注册");
buttonregister.setBounds(375, 300, 70, 25);
frame.add(buttonregister);
frame.setBounds(400, 100, 800, 640);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//为登录按钮添加监听器
buttonlogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ID = userID.getText();
String pass=password.getText();//把输入框中的用户名密码和提出来
Admin admin = new Admin(); //创建一个用户
admin.setID(ID);
admin.setPassword(pass);
//登录
Login login = new Login();
login.setAdmin(admin);
if(login.JudgeAdmin()==0) {
//弹出账号或密码错误的窗口
JOptionPane.showMessageDialog(null, "账号或密码错误", "账号或密码错误", JOptionPane.WARNING_MESSAGE);
//清除密码框中的信息
password.setText("");
//清除账号框中的信息
userID.setText("");
//System.out.println("登陆失败");
} else {
//弹出登录成功的窗口
JOptionPane.showMessageDialog(null, "登陆成功", "登陆成功", JOptionPane.NO_OPTION);
//点击确定后会跳转到主窗口
frame.setVisible(false);
//登录成功跳到查询界面
new SelectTable();
}
}
});
//为注册按钮添加监听器
buttonregister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//注册页面
frame.setVisible(false);
AdminRegister ar = new AdminRegister();
}
});
}
public static void main(String []args) {
//主程序
//登录窗口
Login_Register login_register = new Login_Register();
}
}
查询界面
import java.awt.;
import java.awt.List;
import java.awt.event.;
import javax.swing.;
import java.sql.;
import java.util.;
import javax.swing.table.;
class SelectTable extends JFrame{
JTextField txtQuery=new JTextField(10);
JButton btnQuery=new JButton("按学号查询");
JTable table=new JTable();
AbstractTableModel tm;
String titles[];
Class<? >colClasses[];
java.util.List<java.util.List<Object>>records;
public SelectTable() {
//设置界面
JPanel pnl=new JPanel();
pnl.add(txtQuery);
pnl.add(btnQuery);
getContentPane().add(pnl,BorderLayout.NORTH);
//设置表格
table.setToolTipText("显示结果");
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setCellSelectionEnabled(false);
table.setShowVerticalLines(true);
table.setShowHorizontalLines(true);
JScrollPane scrollpane=new JScrollPane(table);
getContentPane().add(scrollpane,BorderLayout.CENTER);
//事件处理
btnQuery.addActionListener(e->{
try {
showData();
}catch (Exception ex) {
ex.printStackTrace();
}
});
setSize(400,300);
setTitle("成绩查询");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
setVisible(true);
}
public void showData()throws SQLException,ClassNotFoundException{
//加载驱动程序
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库
String connString="jdbc:mysql://localhost:3306/manage?"
+ "useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
String user = "root";
String password = "zcydnx";
Connection connection=DriverManager.getConnection(connString,user,password);
//执行查询
String sql1="select* from grade where sno like?;";
PreparedStatement stmt=connection.prepareStatement(sql1);
stmt.setString(1,"%"+txtQuery.getText().trim()+"%");
ResultSet rs=stmt.executeQuery();
ResultSetMetaData meta=rs.getMetaData();
//显示结果
int colCnt=meta.getColumnCount();
System.out.println(colCnt);
titles=new String[colCnt];
colClasses=new Class[colCnt];
for(int i=0;i<colCnt;i++){
titles[i]=meta.getColumnName(i+1);
String className=meta.getColumnClassName(i+1);
Class<? >clz=String.class;
if(className!=null)
try {
clz=Class.forName(className);
}catch (Exception ex) {
ex.printStackTrace();
}
colClasses[i]=clz;
}
records.clear();
while(rs.next()) {
java.util.List<Object>one_record=new ArrayList<>();
//从结果集中取数据放入记录中
for(int i=0;i<titles.length;i++) {
Object obj=rs.getObject(i+1);
System.out.print(obj+";");
one_record.add(obj==null?null:obj.toString());
}
records.add(one_record);
System.out.println();
}
table.setModel(tm);
tm.fireTableStructureChanged();
}
public void initTableModel() {
records=new ArrayList<>();
tm=new AbstractTableModel() {
public int getColumnCount() {
return titles.length;
}
public int getRowCount() {
return records.size();
}
public Object getValueAt(int row,int column) {
if(!records.isEmpty())
return(records.get(row)).get(column);
else
return null;
}
public String getColumnName(int column) {
return titles[column];
}
public Class<? >getColumnClass(int column){
return colClasses[column];
}
public void setValueAt(Object value,int row,int column) {
//数据模型不可编辑。该方法设置为空
}
public boolean isCellEditable(int row,int column) {
return false;
}
};
}
public static void main(String[]args) {
SwingUtilities.invokeLater(()->{
SelectTable frame=new SelectTable();
frame.initTableModel();
try {
frame.showData();
}catch (Exception e) {
e.printStackTrace();
}
});
}}