我正在努力将项目从gambas代码语言翻译成Java。
实际上,JTable
的第一列(显示所有查询结果)需要根据单元格中读取的文本使用不同颜色(黄绿青绿色)进行背景绘制,问题是当我尝试获取文本(能够为4种类型的文本切换颜色)或使用任何其他功能,如=> rs.getString或rs.getNString或rs.getBlob,它将永远不会抛出错误 - > java.sql.SQLException: After end of result set
。
我一直在寻找这个错误2天,除了我不能写两次循环之外我找不到任何有用的东西 - >而(rs.next)。
这是类Database(db):
public class BaseDeDatos{
Connection conn;
java.sql.Statement statement = null;
java.sql.PreparedStatement pst = null;
java.sql.ResultSet rs = null;
ResultSetMetaData rsmd = null;
int columnsNumber = 0;
int rowsNumber = 0;
public BaseDeDatos() throws SQLException {
this.conn = DriverManager.getConnection("my driver");
}
public Connection getConn() {
return conn;
}
}
这是进入MainView类:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String sql ="";
bd.getConn();
bd.statement = bd.conn.prepareStatement(sql);
bd.rs = bd.statement.executeQuery(sql);
// Removing Previous Data
while (jTable.getRowCount() > 0) {
((DefaultTableModel) jTable.getModel()).removeRow(0);
}
bd.rsmd = bd.rs.getMetaData();
bd.columnsNumber = bd.rs.getMetaData().getColumnCount();
jTable.setModel(resultSetToTableModel(bd.rs));
// this line needs this -> import static net.proteanit.sql.DbUtils.resultSetToTableModel;
System.out.println(bd.rs); // CHECKING if the ResultSet Has it's Info Inside
String rsString = null;
bd.rs.getString("1st COLUMN"); // THIS LINE THROWS THE ERROR java.sql.SQLException: After end of result set.
while (bd.rs.next()) { // Doesen't matter if i delete the last line the program never goes inside this while (I'dont know why).
System.out.println("HELLOW");
String em =bd.rs.getString("Estado");
rsString = em.replace("Validado", ",");
System.out.println(rsString);
for(int j = 1; j < bd.columnsNumber; j++){
System.out.print(bd.rs.getString(j)+" ");
em = bd.rs.getString("Estado");
rsString = em.replace("Validado", ",");
}
System.out.println("\n");
}
setCustomDbColumns();
// In this method i set up the width of the columns on my jTable nothing more special.
} catch (SQLException ex) {
Logger.getLogger(MainView.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex);
}finally{
try{
if (bd.rs != null)
bd.rs.close();
if (bd.statement != null)
bd.statement.close();
/*if (bd.conn != null)
bd.conn.close();*/
}catch(SQLException e){}
}
错误图
答案 0 :(得分:2)
你可以实现类似的东西
public void changeTable(JTable table,int column_index) {
table.getColumnModel().getColumn(column_index).setCellRenderer(new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
final Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (!isSelected) {
int age = Integer.parseInt(table.getValueAt(row, 1).toString());
// you can set your own implementation here. As an example I
// have checked if the age is over 20 or not. According to the
// age return from the table column you get different background colors.
int req_age = 20;
// in your case req_age can be rs.getString("column_name"); or something
// you need to check with..
if (age < req_age) {
c.setBackground(new Color(255, 101, 18));
} else {
c.setBackground(new Color(0, 204, 0));
}
}
return c;
}
});
}
答案 1 :(得分:1)
,它将永远无法抛出错误 - &gt; java.sql.SQLException:结果集结束后。
jTable.setModel(resultSetToTableModel(bd.rs));
bd.rs.getString("1st COLUMN"); // THIS LINE THROWS THE ERROR
是的,resutlSetToModel(...)方法从ResultSet中读取数据,因此您无法再访问ResultSet中的数据。
如果要访问数据,可以从TableModel获取数据。
TableModel model = table.getModel();
Object data = model.getValueAt(0, 0);