大家帮我看看把,我想做出个功能,jtable已经显示出了数据库的所有数据,当点击这个查询按钮搜到一条记录,而这时jtable只显示这一条记录。我的jtable只能显示全部数据,点击查询button一点反应都没有,我贴了我的代码,大神们帮我看看是不是哪里写错了,哪里需要改一下的
private void initComponents() { //这个是我的jtable
Vector titles=new Vector();
titles.add("id");
titles.add("name");
titles.add("location");
titles.add("address");
titles.add("postcode");
titles.add("linkman");
titles.add("fixphone");
titles.add("telephone");
titles.add("re");if(i==0){ //我设定了个按钮标签,当i==0时,按钮没点击,执行getCustomerInfo();方法,提取数据库数据
entity.getCustomerInfo();
customerjTable1 = new javax.swing.JTable(entity.rowDataCustomer,titles);}else if(i==1){
// 当i==0时,按钮没点击,执行searchCustomer(getSearchCustomerName());方法,get查询的text的值,并提取数据库数据
entity.searchCustomer(getSearchCustomerName());
customerjTable1=new javax.swing.JTable(entity.searchCustomerInfo,titles);
}
} public static Vector rowDataCustomer = new Vector(); public static void getCustomerInfo() { //从数据看里取全部数据放到vector里,返回到jtable中
String sql = "select * from customer";
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));
vcRows.add(rs.getString(4));
vcRows.add(rs.getString(5));
vcRows.add(rs.getString(6));
vcRows.add(rs.getString(7));
vcRows.add(rs.getString(8));
vcRows.add(rs.getString(9));
rowDataCustomer.add(vcRows);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionUtils.close(rs);
ConnectionUtils.close(stmt);
ConnectionUtils.close(conn);
}
} public Vector searchCustomerInfo = new Vector(); //从数据看里取要查询数据放到vector里,返回到jtable中
public void searchCustomer(String name) {
String sql = "select * from customer 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("location"));
searchCustomerInfo.add(rs.getString("address"));
searchCustomerInfo.add(rs.getString("postcode"));
searchCustomerInfo.add(rs.getString("linkman"));
searchCustomerInfo.add(rs.getString("fixphone"));
searchCustomerInfo.add(rs.getString("telephone"));
searchCustomerInfo.add(rs.getString("re"));
//customerInfo.add(vcRows);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionUtils.close(rs);
ConnectionUtils.close(stmt);
ConnectionUtils.close(conn);
} }
private void initComponents() { //这个是我的jtable
Vector titles=new Vector();
titles.add("id");
titles.add("name");
titles.add("location");
titles.add("address");
titles.add("postcode");
titles.add("linkman");
titles.add("fixphone");
titles.add("telephone");
titles.add("re");if(i==0){ //我设定了个按钮标签,当i==0时,按钮没点击,执行getCustomerInfo();方法,提取数据库数据
entity.getCustomerInfo();
customerjTable1 = new javax.swing.JTable(entity.rowDataCustomer,titles);}else if(i==1){
// 当i==0时,按钮没点击,执行searchCustomer(getSearchCustomerName());方法,get查询的text的值,并提取数据库数据
entity.searchCustomer(getSearchCustomerName());
customerjTable1=new javax.swing.JTable(entity.searchCustomerInfo,titles);
}
} public static Vector rowDataCustomer = new Vector(); public static void getCustomerInfo() { //从数据看里取全部数据放到vector里,返回到jtable中
String sql = "select * from customer";
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));
vcRows.add(rs.getString(4));
vcRows.add(rs.getString(5));
vcRows.add(rs.getString(6));
vcRows.add(rs.getString(7));
vcRows.add(rs.getString(8));
vcRows.add(rs.getString(9));
rowDataCustomer.add(vcRows);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionUtils.close(rs);
ConnectionUtils.close(stmt);
ConnectionUtils.close(conn);
}
} public Vector searchCustomerInfo = new Vector(); //从数据看里取要查询数据放到vector里,返回到jtable中
public void searchCustomer(String name) {
String sql = "select * from customer 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("location"));
searchCustomerInfo.add(rs.getString("address"));
searchCustomerInfo.add(rs.getString("postcode"));
searchCustomerInfo.add(rs.getString("linkman"));
searchCustomerInfo.add(rs.getString("fixphone"));
searchCustomerInfo.add(rs.getString("telephone"));
searchCustomerInfo.add(rs.getString("re"));
//customerInfo.add(vcRows);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionUtils.close(rs);
ConnectionUtils.close(stmt);
ConnectionUtils.close(conn);
} }
每次从A中搜索到的数据放到集合B,然后把B作为table的model。
private void searchjButton1ActionPerformed(java.awt.event.ActionEvent evt) { entity.searchCustomer(getSearchCustomerName());
customerjTable1=new javax.swing.JTable(entity.searchCustomerInfo,titles);
}
但是運行後還是不行