有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java关于DefaultTableModel和Jform表实现的问题

以下是我创建的代码,用于尝试在GUI中创建一个包含房间预订的jTable

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package displayrooms;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mads
 */
public class DisplayRooms extends javax.swing.JFrame
{
/**
 * Creates new form Displayrooms
 */



public DisplayRooms() throws ClassNotFoundException, SQLException
{
    initComponents();


    Class.forName("oracle.jdbc.driver.OracleDriver");
    String stringCon = "jdbc:oracle:thin:@datdb.cphbusiness.dk:1521:dat;user;cphsh241;password;cphsh241";

    Connection con = DriverManager.getConnection(stringCon);
    java.sql.Statement state = con.createStatement();

    ResultSet rs = state.executeQuery("SELECT * FROM Room_Booking");

    ResultSetMetaData rsmetadata = rs.getMetaData();

    int columns = rsmetadata.getColumnCount();

    DefaultTableModel dtm = new DefaultTableModel();


    Vector columns_name;
    columns_name = new Vector();
    Vector data_rows;
    data_rows = new Vector();

    for (int i = 1; i < columns; i++)
    {
        columns_name.addElement(rsmetadata.getColumnName(i));
    }
    dtm.setColumnIdentifiers(columns_name);

    while (rs.next())
    {
        data_rows = new Vector();
        for (int j = 1; j < columns; j++)
        {
            data_rows.addElement(rs.getString(j));
        }
        dtm.addRow(data_rows);
    }


    jTable1.setModel(dtm);


}

/**
 * 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()
{

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

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Rooms");

    jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][]
        {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
        },
        new String []
        {
            "Title 1", "Title 2", "Title 3", "Title 4"
        }
    ));
    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()
            .addGap(173, 173, 173)
            .addComponent(jLabel1)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(15, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, 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(DisplayRooms.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex)
    {
        java.util.logging.Logger.getLogger(DisplayRooms.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex)
    {
        java.util.logging.Logger.getLogger(DisplayRooms.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex)
    {
        java.util.logging.Logger.getLogger(DisplayRooms.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
                new DisplayRooms().setVisible(true);
            } catch (ClassNotFoundException | SQLException ex)
            {
                Logger.getLogger(DisplayRooms.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}
// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration                   

}

问题1

但当我运行它时,我会收到一个错误,上面写着:

“错误:无法找到或加载主类displayrooms.displayrooms。” Java结果:1“

我尝试过谷歌搜索和查看youtube视频,但所有的解决方案似乎都不适用于我的项目

有人知道是什么导致了这个问题吗

我还有一个后续问题,如果项目应该能够运行,但我需要先运行它


共 (0) 个答案