user1511579
user1511579

Reputation: 1537

Netbeans Java CRUD application with custom swing interface

i'm trying to build a java application with an embedded database. It will be very similiar to a CRUD application and i looked deeply in many tutorials provided for oracle and netbeans like this ones:

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

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

However, this build automatically a default interface based on just crud actions and that's not what i want. I pretend to customize by myself all the interface and when need, access the derby embedded database to execute some operations.

In the mean time i just have two .java files. The JFrame one which contains the code generated by the elements added on the design menu:

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                   
}

and the one with a class created by me:

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

    }
}

For now, what i really need is some lights in how to separate the interface from the database i mean, use a "class" or something between them so that they don't "contact" each other.

And my other question is about the tables that i have on the database. Will they need a class.java file for each of them? And what about the Entity Manager Class presented in this tutorial: http://platform.netbeans.org/tutorials/nbm-crud.html

Will i need to use it? I'm using derby database.

Sory for a so long questio, but i'm a bit lost at the moment in how to make the structure in what is supposed to be a java crud desktop application with a custom interface, and in how to make the layers ("swing","db" and the one that separates them") connect between each other.

Upvotes: 0

Views: 5705

Answers (1)

paulsm4
paulsm4

Reputation: 121799

1) your first file (what is it's name?) should have all the GUI stuff.

2) Your second file ("Aquitex.java"?) should not extend from JFrame, and should not necessarily have any GUI stuff.

3) Your main form will create an instance of "Aquitex".

4) UI event handlers in the first class will call public methods in the second.

Upvotes: 2

Related Questions