泛舟湖上清波郎朗
public class test1 extends JFrame implements ActionListener{JPanel jp1,jp2;JTable jt1=null;JLabel jl1=null;JTextField jtf=null;JButton jb1,jb2,jb3,jb4;JTable jt=null;JScrollPane jsp=null;//String sql;public static void main(String[] args) {test1 t1=new test1();}test1(){jp1=new JPanel();jl1=new JLabel("请输入要查询的编号");jtf=new JTextField(10);jb1=new JButton("查询");jb1.addActionListener(this);jp1.add(jl1);jp1.add(jtf);jp1.add(jb1);jp2=new JPanel();jb2=new JButton("添加");jb3=new JButton("删除");jb4=new JButton("修改");jp2.add(jb2);jp2.add(jb3);jp2.add(jb4);tablemodel t=new tablemodel();jt1=new JTable(t);jsp=new JScrollPane(jt1);this.add(jsp);this.add(jp1,"North");this.add(jp2,"South");this.setSize(400,400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);}public void actionPerformed(ActionEvent arg0) {if(arg0.getSource()==jb1){//System.out.println("王博古");String name=this.jtf.getText().trim();String sql="select * from lixianqi where name="+"'"+name+"'";tablemodel t=new tablemodel(sql);jt.setModel(t);}}}Vector rowData, columnNames;Connection connection=null;PreparedStatement ps=null;ResultSet res=null;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~public tablemodel(){columnNames=new Vector();columnNames.add("编码");columnNames.add("版本");columnNames.add("名字");rowData=new Vector();try{Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");connection=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=zhudan","sa","zhudan");ps=connection.prepareStatement("select * from lixianqi");res=ps.executeQuery();while(res.next()){Vector v=new Vector();v.add(res.getInt(1));v.add(res.getString(2));v.add(res.getString(3));rowData.add(v);}}catch(Exception e){e.printStackTrace();}finally{try {if(res!=null)res.close();if(ps!=null)ps.close();if(connection!=null)connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};public tablemodel(String sql){columnNames=new Vector();columnNames.add("编码");columnNames.add("版本");columnNames.add("名字");rowData=new Vector();try{Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");connection=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=zhudan","sa","zhudan");ps=connection.prepareStatement(sql);res=ps.executeQuery();while(res.next()){Vector ve=new Vector();ve.add(res.getInt(1));ve.add(res.getString(2));ve.add(res.getString(3));rowData.add(ve);}}catch(Exception e){e.printStackTrace();}finally{try {if(res!=null)res.close();if(ps!=null)ps.close();if(connection!=null)connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}//得到多少列public int getColumnCount() {System.out.println("a");return this.columnNames.size();}//得到多少列public int getRowCount() {System.out.println("2");return this.rowData.size();}//得到哪行哪列public Object getValueAt(int row, int colum) {System.out.println("3");return ((Vector)this.rowData.get(row)).get(colum);}public String getColumnName(int column) {// TODO Auto-generated method stubreturn (String) columnNames.get(column);}}Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExceptionat chack.test1.actionPerformed(test1.java:75)