日期:2014-05-20 浏览次数:20652 次
public void initInfo(String id) { MyCon mycon = null; Statement stmt = null; ResultSet rs = null; try { mycon = new MyCon();//此处自己写的打开数据库程序,运行OK stmt = mycon.con.createStatement(); String querycommand = "SELECT * FROM Table WHERE ID='" + id + "'"; rs = stmt.executeQuery(querycommand); ResultSetMetaData rsmd = rs.getMetaData(); DefaultTableModel dataModel = new DefaultTableModel(); Object[][] values = new Object[0][0]; values = new Object[rsmd.getColumnCount()][2]; for (int i = 1; i <= rsmd.getColumnCount(); i++) { //values[i] = new Object[2]; values[i][0] = rsmd.getColumnName(i); System.out.println(rsmd.getColumnName(i));//此处OK values[i][1] = rs.getObject(rsmd.getColumnName(i));//运行到此处报错,错误代码如下 } String header[] = {"基本信息", "内容"}; dataModel.setDataVector(values, header); JTable table = new JTable(dataModel); JScrollPane pane = new JScrollPane(table); this.add(pane, BorderLayout.CENTER); } catch (Exception ex) { ex.printStackTrace(); } finally { mycon.MyClose(mycon.con, stmt, rs);//自己写的一个关闭数据库程序,运行OK } }
public JTable createTableFromSQL(Connection connection,String sql , Object[] name)throws Exception{ Statement stmt = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,// 对滚动不敏感,结果集可滚来滚去 ResultSet.CONCUR_READ_ONLY);// 只能够读取里边的内容, ResultSet rs = stmt.executeQuery(sql);//开始执行查询语句,结果存储在rs中 rs.last();// 指到定位到最后一行 int row = rs.getRow();// 获得总行数 rs.beforeFirst();// 定位到开始 ResultSetMetaData rsmd1 = rs.getMetaData();// 计算字段个数 int col = rsmd1.getColumnCount(); // 计算字段个数 Object a[][] = new Object[row][col];//确定表格存储数据的大小 JTable jTable = new JTable(a, name);//建立表格 for (int index = 0; rs.next(); index++)//使用查询返回的结果初始化表格 for (int j = 0; j < col; j++) a[index][j] = rs.getString(j + 1); return jTable; }
------解决方案--------------------
不成功就改变数据库,或采取另外的展示方案