大神们帮我看看,快被jtable给搞死了

owen22222222 2013-06-28 01:14:51
新手刚接触jtable,快被搞死了,弄了几天都没出来,想点击查询, jtable上只显示查询出来的这一条数据,不知道我代码哪里有问题,每次点击,都报空指针,


import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class Test extends javax.swing.JFrame {

public Test() {
initComponents();
setLocationRelativeTo(null);//界面居中
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable(entity.getCustomerInfo(),getTitles());

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("查询");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});

jScrollPane1.setViewportView(jTable1);

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(jTextField1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jButton1))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

}

private DefaultTableModel tableModel;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

entity.searchCustomer(getName());
System.out.println(entity.searchCustomerInfo);
tableModel.addRow(entity.searchCustomerInfo);

jTable1.setModel(tableModel);
}

public Vector getTitles() {
Vector title = new Vector();
title.add("id");
title.add("name");
title.add("age");
return title;
}
Entity entity = new Entity();

public Entity getEntity() {
return entity;
}

public void setEntity(Entity entity) {
this.entity = entity;
}

public String getName() {
return new String(jTextField1.getText());
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}



这个是我调用数据库的方法
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;


/**
*
* @author Administrator
*/
public class Entity {


public static Vector rowDataCustomer = new Vector();

public static Vector getCustomerInfo() {
String sql = "select * from users_xiey";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
// Vector rowData = new Vector();
try {
conn = ConnectionUtils.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

while (rs.next()) {
Vector vcRows = new Vector();
vcRows.add(rs.getString(1));
vcRows.add(rs.getString(2));
vcRows.add(rs.getString(3));
rowDataCustomer.add(vcRows);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionUtils.close(rs);
ConnectionUtils.close(stmt);
ConnectionUtils.close(conn);
}
return rowDataCustomer;
}


public Vector searchCustomerInfo = new Vector();
public void searchCustomer(String name) {
String sql = "select * from users_xiey where name like'%" + name + "%'";
// System.out.println(sql);
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
// Vector rowData = new Vector();
try {
conn = ConnectionUtils.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

while (rs.next()) {
// Vector vcRows = new Vector();
searchCustomerInfo.add(rs.getString("id"));
searchCustomerInfo.add(rs.getString("name"));
searchCustomerInfo.add(rs.getString("age"));
//customerInfo.add(vcRows);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionUtils.close(rs);
ConnectionUtils.close(stmt);
ConnectionUtils.close(conn);
}

}

public static void main(String[] arg) {
getCustomerInfo();
}
}




这个是我的界面


...全文
157 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Uncertain_Magic 2013-06-28
  • 打赏
  • 举报
回复
你要把报的异常给贴出来,代码太长了
钢筋裤衩蛋 2013-06-28
  • 打赏
  • 举报
回复
把报空指针那部分代码站出来
陈陈-陈 2013-06-28
  • 打赏
  • 举报
回复
好长,没有看完,在学习中。。。。。。。。。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧