gpt4 book ai didi

java - Netbeans 中的结果集到 Jtable

转载 作者:行者123 更新时间:2023-12-01 13:32:59 24 4
gpt4 key购买 nike

我想在 Netbeans 中使用 rs2xml.jar 用结果集填充 jtable1。但会返回一些错误。我的代码是;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package program;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

/**
*
* @author Lacrymae_Ev
*/
public class cagbas extends javax.swing.JFrame {
private static Statement stmt;
private static Connection con;
private static final String query = "select 'aicb' as aicb,sum(dur)as dur,sum(tot)as tot from exampletable with(nolock)\n" +
"where date between '2013-07-01 00:00:00.000' and '2013-07-01 23:59:59.999'\n" +
"and id='013'";

/**
* Creates new form cagbas
*/
public cagbas() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTable1.setModel(DbUtils.resultSetToTableModel(rs));
jScrollPane1.setViewportView(jTable1);

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(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(14, Short.MAX_VALUE))
);

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

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(cagbas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(cagbas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(cagbass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(cagbas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://192.168.100.100;" + "databaseName=ExampleDB;" + "user=exampleuser;" + "password=examplepass;";
Connection con = DriverManager.getConnection(connectionUrl);
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
con.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.toString());
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new cagbas().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}

但是 jTable1.setModel(DbUtils.resultSetToTableModel(rs)); 无法识别。我通过 Netbeans 中的 Gui Designer 生成 jtable,并使用 "jTable1.setModel(DbUtils.resultSetToTableModel(rs));"

自定义创建

如何解决这个问题?

最佳答案

您遇到上下文问题。 rsinitComponents 的上下文中未定义,这意味着编译器不知道什么 rs是。

您需要定义rs作为实例变量,例如 stmt并确保您没有将其隐藏在 main

//ResultSet rs = stmt.executeQuery(query);
rs = stmt.executeQuery(query);

这是个人的事情,但我强烈鼓励您了解什么 rs2xml实际是在做之前依赖它

关于java - Netbeans 中的结果集到 Jtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21446450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com