Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有自定义swing接口的Netbeans Java CRUD应用程序_Java_Database_Swing_Netbeans_Derby - Fatal编程技术网

具有自定义swing接口的Netbeans Java CRUD应用程序

具有自定义swing接口的Netbeans Java CRUD应用程序,java,database,swing,netbeans,derby,Java,Database,Swing,Netbeans,Derby,我正在尝试构建一个带有嵌入式数据库的java应用程序。它将非常类似于CRUD应用程序,我深入阅读了为oracle和netbeans提供的许多教程,如下所示: 然而,这个构建会自动生成一个基于crud操作的默认接口,这不是我想要的。我假装自己定制了所有接口,并在需要时访问derby嵌入式数据库以执行一些操作 同时,我只有两个.java文件。JFrame one包含由设计菜单上添加的元素生成的代码: package interface_aquitex; import aquitex.Aquit

我正在尝试构建一个带有嵌入式数据库的java应用程序。它将非常类似于CRUD应用程序,我深入阅读了为oracle和netbeans提供的许多教程,如下所示:

然而,这个构建会自动生成一个基于crud操作的默认接口,这不是我想要的。我假装自己定制了所有接口,并在需要时访问derby嵌入式数据库以执行一些操作

同时,我只有两个.java文件。JFrame one包含由设计菜单上添加的元素生成的代码:

package interface_aquitex;

import aquitex.Aquitex;
import javax.swing.JOptionPane;

public class design_aquitex extends javax.swing.JFrame {

    /**
     * Creates new form design_aquitex
     */
    public design_aquitex() {
        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() {

        jFrame1 = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        jFrame1.setMinimumSize(new java.awt.Dimension(400, 400));

        jLabel1.setText("Nome:");

        jLabel2.setText("Idade:");

        jTextField1.setName(""); // NOI18N
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jTextField1PropertyChange(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        jButton1.setText("Inserir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("Dados");

        jButton2.setText("Listar");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrame1Layout.createSequentialGroup()
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jFrame1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(37, 37, 37)
                        .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                            .addComponent(jTextField2)))
                    .addGroup(jFrame1Layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addGap(94, 94, 94))
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrame1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addComponent(jButton1)
                .addGap(4, 4, 4)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(88, 88, 88)
                .addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap(79, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("File");

        jMenuItem1.setText("jMenuItem1");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        
    public String getjTextField1(){
        JOptionPane.showMessageDialog(null,jTextField1.getText());
        return jTextField1.getText();

    }

    public int getjTextField2(){
        int idade;
        idade = Integer.parseInt(jTextField2.getText());
        System.out.println(idade);
        return idade;
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        jFrame1.setVisible(true);
    }                                          

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Aquitex a = new Aquitex();
        a.saveRecord();
    }                                        

    private void jTextField1PropertyChange(java.beans.PropertyChangeEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {                                       
        JOptionPane.showMessageDialog(null,jTextField1.getText());
    }                                      

    /**
     * @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(design_aquitex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(design_aquitex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(design_aquitex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(design_aquitex.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new design_aquitex().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                   
}
package-interface\u-aquitex;
进口aquitex.aquitex;
导入javax.swing.JOptionPane;
公共类设计\u aquitex扩展了javax.swing.JFrame{
/**
*创建新的表单设计\u aquitex
*/
公共设计{
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
jFrame1=newjavax.swing.JFrame();
jLabel1=newjavax.swing.JLabel();
jLabel2=newjavax.swing.JLabel();
jTextField1=newjavax.swing.JTextField();
jTextField2=newjavax.swing.JTextField();
jButton1=newjavax.swing.JButton();
jLabel3=newjavax.swing.JLabel();
jButton2=newjavax.swing.JButton();
jMenuBar1=newjavax.swing.JMenuBar();
jMenu1=newjavax.swing.JMenu();
jMenuItem1=newjavax.swing.JMenuItem();
jMenu2=newjavax.swing.JMenu();
jFrame1.setMinimumSize(新的java.awt.Dimension(400400));
jLabel1.setText(“Nome:”);
jLabel2.setText(“Idade:”);
jTextField1.setName(“”;//NOI18N
jTextField1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
JTextfield1执行的操作(evt);
}
});
jTextField1.addPropertyChangeListener(新java.beans.PropertyChangeListener(){
public void propertyChange(java.beans.PropertyChangeEvent evt){
JTextfield1属性更改(evt);
}
});
jTextField1.addKeyListener(新java.awt.event.KeyAdapter(){
按下public void键(java.awt.event.KeyEvent evt){
jTextField1KeyPressed(evt);
}
});
jButton1.setText(“Inserir”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
jLabel3.setText(“护墙板”);
jButton2.setText(“Listar”);
javax.swing.GroupLayout jFrame1Layout=newjavax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,jFrame1Layout.createSequentialGroup()
.addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jFrame1Layout.createSequentialGroup()
.addGap(26,26,26)
.addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(37,37,37)
.addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
.addComponent(jTextField1,javax.swing.GroupLayout.DEFAULT\u SIZE,56,Short.MAX\u值)
.addComponent(jTextField2)))
.addGroup(jFrame1Layout.createSequentialGroup()
.addGap(57,57,57)
.addComponent(jLabel3,javax.swing.GroupLayout.PREFERRED_SIZE,64,javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,92,Short.MAX_值)
.addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(94,94,94))
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jFrame1Layout.createSequentialGroup()
.addGap(24,24,24)
.addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(5,5,5)
.addComponent(jButton1)
.addGap(4,4,4)
.addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(88,88,88)
.addGroup(jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3,javax.swing.GroupLayout.PREFERRED\u SIZE,37,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addComponent(jButton2))
package aquitex;

import interface_aquitex.design_aquitex;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;


public class Aquitex extends JFrame{

    private static PreparedStatement stmtSaveNewRecord=null;
    private static Connection dbConnection = null;



    String tabelaExp =
"CREATE table APP.Alunos ("+
   " ID          INTEGER NOT NULL"+
          "      PRIMARY KEY GENERATED ALWAYS AS IDENTITY "+
            "    (START WITH 1, INCREMENT BY 1),"+
    "FIRSTNAME   VARCHAR(30),"+
    "AGE            INT )";




    private void setDBSystemDir() {
        // Decide on the db system directory: <userhome>/.aquitex/
        String userHomeDir = System.getProperty("user.home", ".");
        String systemDir = userHomeDir + "/.aquitex";

        // Set the db system directory.
        System.setProperty("derby.system.home", systemDir);
    }

    private boolean createTables(Connection dbConnection) {
        boolean bCreatedTables = false;
        Statement statement = null;
        try {
            statement = dbConnection.createStatement();
            statement.execute(tabelaExp);
            bCreatedTables = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        return bCreatedTables;
    }

    public int saveRecord() {

        design_aquitex da = new design_aquitex();

        try {
            stmtSaveNewRecord = dbConnection.prepareStatement(
        "INSERT INTO APP.Alunos " +
        "   (FIRSTNAME, AGE) " +
        "VALUES (?, ?)",
        Statement.RETURN_GENERATED_KEYS);
        } catch (SQLException ex) {
            Logger.getLogger(Aquitex.class.getName()).log(Level.SEVERE, null, ex);
        }

    int id = -1;
    try {
        stmtSaveNewRecord.clearParameters();
        stmtSaveNewRecord.setString(1, da.getjTextField1());
        stmtSaveNewRecord.setInt(2, da.getjTextField2());
        int rowCount = stmtSaveNewRecord.executeUpdate();
        ResultSet results = stmtSaveNewRecord.getGeneratedKeys();
        if (results.next()) {
            id = results.getInt(1);
        }
    } catch(SQLException sqle) {
        sqle.printStackTrace();
    }
    return id;
} 

    public static void main(String[] args) {
        // TODO code application logic here
        String userHomeDir = System.getProperty("user.home", ".");
        String systemDir = userHomeDir + "/.aquitex";

        // Set the db system directory.
        System.setProperty("derby.system.home", systemDir);

        String strUrl = "jdbc:derby:aquitex_db;create=true";

        Properties props = new Properties();
        props.put("user", "adm_aquitex");
        props.put("password", "pass_aquitex");

        try {
            dbConnection = DriverManager.getConnection(strUrl, props);
        } catch (SQLException ex) {
            Logger.getLogger(Aquitex.class.getName()).log(Level.SEVERE, null, ex);
        }


        //Aquitex a = new Aquitex();
        //a.createTables(dbConnection);

        design_aquitex da = new design_aquitex();
        da.setVisible(true);

    }
}