import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.JTableHeader;
public class Home extends JFrame implements ActionListener{
JScrollPane scpDemo;
JFrame Add;
JTableHeader jth;
JTable tabDemo;
JTextField text,AddName,AddNumber,AddSex,AddAge,AddPost,AddWage,AddJop;
JLabel name,number,age,sex,wage,post,jop;
JButton anShow,anSet,anSelsct,anClear,anAdd,Addyes,Addno;
String SQLname,SQLsex,SQLpost,SQLjop;
String a[]={SQLname,SQLsex,SQLpost,SQLjop};
int SQLnumber,SQLage,SQLwage;
int b[]={SQLnumber,SQLage,SQLwage};
public Home(){
super("学生管理系统");
this.setSize(400,500);
this.setLayout(null);
this.setLocation(400,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);//钉死
this.text=new JTextField();
this.text.setBounds(10, 20,200, 25);
this.anSelsct=new JButton("查询");
this.anSelsct.setBounds(230,20,70,25);
this.anShow=new JButton("刷新");
this.anShow.setBounds(310,20,70,25);
this.anAdd=new JButton("添加");
this.anAdd.setBounds(20,435,70,25);
this.anSet=new JButton("修改");
this.anSet.setBounds(100,435,70,25);
this.anClear=new JButton("删除");
this.anClear.setBounds(180,435,70,25);
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10,50,365,380);
AddName=new JTextField();
AddNumber=new JTextField();
AddAge=new JTextField();
AddSex=new JTextField();
AddPost=new JTextField();
AddWage=new JTextField();
AddJop=new JTextField();
name=new JLabel("姓名");
number=new JLabel("学号");
age=new JLabel("年龄");
sex=new JLabel("性别");
post=new JLabel("职位");
wage=new JLabel("工资");
jop=new JLabel("地点");
Addyes=new JButton("确定");
Addno=new JButton("取消");
//添加窗口的创建
Add=new JFrame();
Add.setTitle("添加");
Add.setSize(350,400);
Add.setLocation(100, 260);
Add.setLayout(new GridLayout(8,2));
Add.add(number);Add.add(AddNumber);
Add.add(name);Add.add(AddName);
Add.add(sex);Add.add(AddSex);
Add.add(age);Add.add(AddAge);
Add.add(post);Add.add(AddPost);
Add.add(wage);Add.add(AddWage);
Add.add(jop);Add.add(AddJop);
Add.add(Addyes); Add.add(Addno);
Addyes.addActionListener(this);
Addno.addActionListener(this);
anAdd.addActionListener(this);
anShow.addActionListener(this);
anSet.addActionListener(this);
anSelsct.addActionListener(this);
anClear.addActionListener(this);
this.scpDemo.getViewport().add(tabDemo);
add(this.scpDemo);
add(this.text);
add(this.anSelsct);
add(this.anShow);
add(this.anAdd);
add(this.anSet);
add(this.anClear);
this.remove(this);//刷新
this.repaint();
try{
// 获得连接
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=people","sa","123456");
// 建立查询条件
String sql="select * from nature";
PreparedStatement pstm = conn.prepareStatement(sql);
// 执行查询
ResultSet rs = pstm.executeQuery();
// 计算有多少条记录
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][7];
count = 0;
while(rs.next()){
info[count][0] = Integer.valueOf( rs.getInt("number"));
info[count][1] = rs.getString("name");
info[count][2] = rs.getString("sex");
info[count][3] = Integer.valueOf( rs.getInt("age") );
info[count][4] = rs.getString("post");
info[count][5] = Integer.valueOf( rs.getInt("wage"));
info[count][6] = rs.getString("jop");
count++;
}
// 定义表头
String[] title = {"学号","姓名","性别","年龄","职位","工资","地点"};
// 创建JTable
this.tabDemo = new JTable(info,title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
}catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
chaxun re=new chaxun();
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==(anAdd)){
Add a=new Add(this,"添加学生信息",true);
re=new chaxun();
tabDemo.setModel(re);
}
else if(e.getSource()==(anSelsct)){
String name=this.text.getText().trim();
String sql="select * from nature where name='"+name+"'";
chaxun aa=new chaxun(sql);
tabDemo.setModel(aa);
}
else if(e.getSource()==anShow){
tabDemo.setModel(re);
}
else if(e.getSource()==(anClear)){
int i=this.tabDemo.getSelectedRow();
clear aa=new clear(i);
re=new chaxun();
tabDemo.setModel(re);
}
else if(e.getSource()==anSet){
int j=this.tabDemo.getSelectedRow();
revise a=new revise(this,"修改学生信息",true,re,j);
re=new chaxun();
tabDemo.setModel(re);
}
}
}
7 个解决方案
#1
有没有报错信息
#2
报错信息 ?
#3
就直接显示连接数据源错误,点击查询会显示
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ui.Home.actionPerformed(Home.java:192)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ui.Home.actionPerformed(Home.java:192)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
#4
这是数据库错误吗?
#5
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ui.Home.actionPerformed(Home.java:192)
at ui.Home.actionPerformed(Home.java:192)
#6
空指针了,检查一下哪为空?
#7
还是不明白啊。。。。
#1
有没有报错信息
#2
报错信息 ?
#3
就直接显示连接数据源错误,点击查询会显示
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ui.Home.actionPerformed(Home.java:192)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ui.Home.actionPerformed(Home.java:192)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
#4
这是数据库错误吗?
#5
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ui.Home.actionPerformed(Home.java:192)
at ui.Home.actionPerformed(Home.java:192)
#6
空指针了,检查一下哪为空?
#7
还是不明白啊。。。。