繁体   English   中英

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

[英]Netbeans Java CRUD application with custom swing interface

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

http://platform.netbeans.org/tutorials/nbm-crud.html

http://netbeans.org/kb/70/java/gui-db.html

但是,这个构建会自动构建一个基于crud操作的默认界面,这不是我想要的。 我假装自己定制所有界面,并在需要时访问derby嵌入式数据库来执行一些操作。

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

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 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);

    }
}

现在,我真正需要的是如何将界面与数据库分开的一些亮点,我的意思是,在它们之间使用“类”或其他东西,以便它们不会相互“接触”。

我的另一个问题是我在数据库中的表格。 他们是否需要每个类的class.java文件? 那么本教程中介绍的实体管理器类如何: http//platform.netbeans.org/tutorials/nbm-crud.html

我需要使用它吗? 我正在使用德比数据库。

Sory有这么长时间的问题,但我现在有点迷失了如何在一个带有自定义界面的java crud桌面应用程序中构建结构,以及如何制作图层(“swing”) ,“db”和分隔它们的那个“)相互连接。

1)你的第一个文件(它的名字是什么?)应该包含所有的GUI内容。

2)你的第二个文件(“Aquitex.java”?) 应该从JFrame的扩展,并且应该一定有任何GUI的东西。

3)您的主表单将创建“Aquitex”的实例。

4)第一个类中的UI事件处理程序将在第二个类中调用公共方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM