急............用rs.getXXXX();方法从数据库中取出的结果集中提取数据插入表格时,奇怪有的能用getXXX();取出数据,而有的不能取出,恳请高手解决!!!
下面是我的程序:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;public class QueryNewbooks extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel jpl; private JRadioButton rb1, rb2, rb3; private JLabel lab; private JComboBox cob; private String[] columnNames = { "书名", "作者", "出版单位", "出版日期", "内容提要" }; private Object[][] rowData = new Object[1000][5]; private JButton bton; private JTable table; public QueryNewbooks() {
String[] cob_str = { "政治", "哲学", "法律", "自然科学", "社会科学", "经济", "军事",
"教育", "体育" };
setDefaultLookAndFeelDecorated(true);
setTitle("新书通报");
setSize(500,400);
Container content = getContentPane();
content.setLayout(new BorderLayout()); jpl = new JPanel(); ButtonGroup bg = new ButtonGroup(); rb1 = new JRadioButton("一周内");
rb2 = new JRadioButton("一个月内");
rb3 = new JRadioButton("三个月内"); rb1.setSelected(true); bg.add(rb1);
bg.add(rb2);
bg.add(rb3); lab = new JLabel("图书分类:"); cob = new JComboBox(cob_str); bton = new JButton("查询"); jpl.setLayout(new GridLayout(2, 3)); jpl.add(rb1);
jpl.add(rb2);
jpl.add(rb3);
jpl.add(lab);
jpl.add(cob);
jpl.add(bton); table = new JTable(rowData, columnNames);
table.setRowHeight(30);
table.setPreferredScrollableViewportSize(new Dimension(500,300));// 设置table的初始显示大小 JScrollPane sp = new JScrollPane(table); content.add(jpl, BorderLayout.NORTH);
content.add(sp, BorderLayout.CENTER); bton.addActionListener(this); setLocationRelativeTo(null);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
});
} public void actionPerformed(ActionEvent e) {
Object obj = (JButton) e.getSource();
if (obj == bton) {
try {
newBooks();
} catch (SQLException e1) {
}
}
} public void newBooks() throws SQLException {
String DriverName = "sun.jdbc.odbc.JdbcOdbcDriver";
String DBURL = "jdbc:odbc:Library";
Connection conn = null;
Statement stmt = null;
String leibie = (String) cob.getSelectedItem();
java.util.Date dateNum = new java.util.Date();
java.util.Date day,chubanriqi;
String shuming, zuozhe,chubandanwei,neirongtiyao;
String sqlString = "SELECT bookName,author,publish,pubDate,enterDate,content FROM all_books WHERE class="
+ "'" + leibie + "'";

int k = 0; whoSelected(dateNum);
for (int i = 0; i < rowData.length; i++)
for (int j = 0; j < 5; j++)
rowData[i][j] = null; // 加载驱动程序
try {
Class.forName(DriverName);
} catch (ClassNotFoundException e) {
System.out.println("无法加载JDBC驱动程序" + e);
return;
} // 建立连接
try {
conn = DriverManager.getConnection(DBURL, "", "");
stmt = conn.createStatement();
} catch (SQLException ee) {
} ResultSet rs = stmt.executeQuery(sqlString);
System.out.println(sqlString);
while(rs.next()) {
day = rs.getDate("enterDate");
if (day.after(dateNum)) {
if (k < rowData.length) { neirongtiyao = rs.getString("content");
shuming = rs.getString("bookName");
zuozhe = rs.getString("author");
chubanriqi = rs.getDate("pubDate");
                                              neirongtiyao = rs.getString("content");

rowData[k][0] = shuming;
rowData[k][1] = zuozhe;
rowData[k][2] = chubandanwei;
rowData[k][3] = chubanriqi;
rowData[k][4] = neirongtiyao;
}
k++;
}
table.repaint();
}
conn.close();
} public void whoSelected(java.util.Date dateNum) {
if (rb1.isSelected()) {
dateNum.setDate(dateNum.getDate() - 7);
}
if (rb2.isSelected()) {
dateNum.setDate(dateNum.getDate() - 30);
}
if (rb3.isSelected()) {
dateNum.setDate(dateNum.getDate() - 90);
}
} public static void main(String[] args) {
new QueryNewbooks();
}
}

解决方案 »

  1.   

    day = rs.getDate("enterDate"); neirongtiyao = rs.getString("content"); 都能取出数据,但是到shuming = rs.getString("bookName");就不行了
      

  2.   

    把Exception贴上来呀
    是不是库中没有“bookName”字段,或名字有偏差
      

  3.   

    问题出在,书的摘要content吧,主要是因为content是大字段,那么在数据库读取时,必须按照数据库中基本表或则视图的字段顺序读取,否则就出错,不知道我的理解是否正确,你先调整字段顺序在试试看