Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Players.java
- *
- * Created on Apr 19, 2011, 12:55:43 PM
- */
- package Users;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.ResultSet;
- import javax.swing.JOptionPane;
- public class Players extends javax.swing.JFrame {
- Connection con;
- Statement stmt;
- ResultSet rs;
- int currentRow = 0;
- /** Creates new form Players */
- public Players() {
- initComponents();
- DoConnect();
- }
- public void DoConnect() {
- try {
- //Connect to the Database
- String host = "jdbc:derby://localhost:1527/Players1";
- String userName = "user";
- String userPassword = "henry";
- con = DriverManager.getConnection(host, userName, userPassword);
- // Execute SQL and loads data
- stmt = con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- String sql = "SELECT * FROM APP.PLAYERS";
- rs = stmt.executeQuery(sql);
- // Looking through data
- rs.next();
- int id_col = rs.getInt("ID");
- String id = Integer.toString(id_col);
- String first = rs.getString("FIRST_NAME");
- String last = rs.getString("Last_Name");
- String type = rs.getString("User_Type");
- // Display records
- textID.setText(id);
- textFirstName.setText(first);
- textLastName.setText(last);
- textUserType.setText(type);
- }
- catch (SQLException err) {
- JOptionPane.showMessageDialog(Players.this, err.getMessage());
- }
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jPanel1 = new javax.swing.JPanel();
- textID = new javax.swing.JTextField();
- textFirstName = new javax.swing.JTextField();
- textLastName = new javax.swing.JTextField();
- jLabel1 = new javax.swing.JLabel();
- textUserType = new javax.swing.JTextField();
- jPanel2 = new javax.swing.JPanel();
- btnNext = new javax.swing.JButton();
- btnPrevious = new javax.swing.JButton();
- btnFirst = new javax.swing.JButton();
- btnLast = new javax.swing.JButton();
- jPanel3 = new javax.swing.JPanel();
- btnUpdateRecord = new javax.swing.JButton();
- btnDeleteRecord = new javax.swing.JButton();
- btnNewRecord = new javax.swing.JButton();
- btnSaveRecord = new javax.swing.JButton();
- btnCancelRecord = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- textID.setText("textID");
- textID.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- textIDActionPerformed(evt);
- }
- });
- textFirstName.setText("textFirstName");
- textLastName.setText("textLastName");
- jLabel1.setText("User Type");
- textUserType.setText("textUserType");
- textUserType.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- textUserTypeActionPerformed(evt);
- }
- });
- org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
- jPanel1.setLayout(jPanel1Layout);
- jPanel1Layout.setHorizontalGroup(
- jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jPanel1Layout.createSequentialGroup()
- .add(textID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(31, 31, 31)
- .add(textFirstName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
- .add(textLastName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
- .add(jPanel1Layout.createSequentialGroup()
- .add(jLabel1)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(textUserType, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)))
- .addContainerGap())
- );
- jPanel1Layout.setVerticalGroup(
- jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jPanel1Layout.createSequentialGroup()
- .addContainerGap()
- .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(textFirstName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(textLastName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(textID, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
- .add(18, 18, 18)
- .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(textUserType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(jLabel1))
- .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- btnNext.setText("Next");
- btnNext.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnNextActionPerformed(evt);
- }
- });
- btnPrevious.setText("Previous");
- btnPrevious.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnPreviousActionPerformed(evt);
- }
- });
- btnFirst.setText("First");
- btnFirst.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnFirstActionPerformed(evt);
- }
- });
- btnLast.setText("Last");
- btnLast.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnLastActionPerformed(evt);
- }
- });
- org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
- jPanel2.setLayout(jPanel2Layout);
- jPanel2Layout.setHorizontalGroup(
- jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jPanel2Layout.createSequentialGroup()
- .add(btnNext)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(btnPrevious)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(btnFirst)
- .add(18, 18, 18)
- .add(btnLast)
- .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- jPanel2Layout.setVerticalGroup(
- jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jPanel2Layout.createSequentialGroup()
- .addContainerGap()
- .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(btnNext)
- .add(btnPrevious)
- .add(btnFirst)
- .add(btnLast))
- .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- btnUpdateRecord.setText("Update");
- btnUpdateRecord.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnUpdateRecordActionPerformed(evt);
- }
- });
- btnDeleteRecord.setText("Delete");
- btnDeleteRecord.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnDeleteRecordActionPerformed(evt);
- }
- });
- btnNewRecord.setText("New");
- btnNewRecord.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnNewRecordActionPerformed(evt);
- }
- });
- btnSaveRecord.setText("Save Record");
- btnSaveRecord.setEnabled(false);
- btnSaveRecord.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnSaveRecordActionPerformed(evt);
- }
- });
- btnCancelRecord.setText("Cancel Record");
- btnCancelRecord.setEnabled(false);
- btnCancelRecord.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- btnCancelRecordActionPerformed(evt);
- }
- });
- org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
- jPanel3.setLayout(jPanel3Layout);
- jPanel3Layout.setHorizontalGroup(
- jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jPanel3Layout.createSequentialGroup()
- .addContainerGap()
- .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jPanel3Layout.createSequentialGroup()
- .add(btnUpdateRecord)
- .add(30, 30, 30)
- .add(btnDeleteRecord)
- .add(31, 31, 31)
- .add(btnNewRecord)
- .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .add(jPanel3Layout.createSequentialGroup()
- .add(btnSaveRecord)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 43, Short.MAX_VALUE)
- .add(btnCancelRecord)
- .add(30, 30, 30))))
- );
- jPanel3Layout.setVerticalGroup(
- jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jPanel3Layout.createSequentialGroup()
- .add(9, 9, 9)
- .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(btnNewRecord)
- .add(btnDeleteRecord)
- .add(btnUpdateRecord))
- .add(18, 18, 18)
- .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(btnSaveRecord)
- .add(btnCancelRecord))
- .addContainerGap(27, Short.MAX_VALUE))
- );
- org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(layout.createSequentialGroup()
- .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(layout.createSequentialGroup()
- .addContainerGap()
- .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .add(layout.createSequentialGroup()
- .add(11, 11, 11)
- .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- .add(layout.createSequentialGroup()
- .addContainerGap()
- .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(layout.createSequentialGroup()
- .addContainerGap()
- .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void textIDActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void textUserTypeActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- }
- private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- if (rs.next()) {
- int id_col = rs.getInt("ID");
- String id = Integer.toString(id_col);
- String first = rs.getString("First_Name");
- String last = rs.getString("Last_Name");
- String user = rs.getString("User_Type");
- textID.setText(id);
- textFirstName.setText(first);
- textLastName.setText(last);
- textUserType.setText(user);
- }
- else {
- rs.previous();
- JOptionPane.showMessageDialog(Players.this, "End Of File");
- }
- }
- catch (SQLException err) {
- JOptionPane.showMessageDialog(Players.this, err.getMessage());
- }
- }
- private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- if (rs.previous()) {
- int id_col = rs.getInt("ID");
- String id = Integer.toString(id_col);
- String first = rs.getString("First_Name");
- String last = rs.getString("Last_Name");
- String user = rs.getString("User_Type");
- textID.setText(id);
- textFirstName.setText(first);
- textLastName.setText(last);
- textUserType.setText(user);
- }
- else {
- rs.next();
- JOptionPane.showMessageDialog(Players.this, "Start of File");
- }
- }
- catch (SQLException err) {
- JOptionPane.showMessageDialog(Players.this, err.getMessage());
- }
- }
- private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- rs.first();
- int id_col = rs.getInt("ID");
- String id = Integer.toString(id_col);
- String first = rs.getString("First_Name");
- String last = rs.getString("Last_Name");
- String user = rs.getString("User_Type");
- textID.setText(id);
- textFirstName.setText(first);
- textLastName.setText(last);
- textUserType.setText(user);
- }
- catch (SQLException err) {
- JOptionPane.showMessageDialog(Players.this, err.getMessage());
- }
- }
- private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- rs.last();
- int id_col = rs.getInt("ID");
- String id = Integer.toString(id_col);
- String first = rs.getString("First_Name");
- String last = rs.getString("Last_Name");
- String user = rs.getString("User_Type");
- textID.setText(id);
- textFirstName.setText(first);
- textLastName.setText(last);
- textUserType.setText(user);
- }
- catch (SQLException err) {
- JOptionPane.showMessageDialog(Players.this, err.getMessage());
- }
- }
- private void btnUpdateRecordActionPerformed(java.awt.event.ActionEvent evt) {
- String first = textFirstName.getText( );
- String last = textLastName.getText( );
- String user = textUserType.getText( );
- String ID = textID.getText( );
- int newID = Integer.parseInt( ID );
- try {
- rs.updateInt( "ID", newID );
- rs.updateString( "First_Name", first );
- rs.updateString( "last_Name", last );
- rs.updateString( "User_Type", user );
- rs.updateRow( );
- JOptionPane.showMessageDialog(Players.this, "Updated");
- }
- catch (SQLException err) {
- System.out.println(err.getMessage() );
- }
- }
- private void btnNewRecordActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- currentRow = rs.getRow( );
- textFirstName.setText("");
- textLastName.setText("");
- textUserType.setText("");
- textID.setText("");
- // Hide and show certain buttons
- btnFirst.setEnabled( false );
- btnPrevious.setEnabled( false ) ;
- btnNext.setEnabled( false );
- btnLast.setEnabled( false );
- btnUpdateRecord.setEnabled( false );
- btnDeleteRecord.setEnabled( false );
- btnNewRecord.setEnabled( false );
- btnSaveRecord.setEnabled( true );
- btnCancelRecord.setEnabled( true );
- }
- catch (SQLException err) {
- System.out.println(err.getMessage() );
- }
- }
- private void btnCancelRecordActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- rs.absolute( currentRow );
- textFirstName.setText( rs.getString("First_Name") );
- textLastName.setText( rs.getString("Last_Name") );
- textUserType.setText( rs.getString("User_Type") );
- textID.setText( Integer.toString( rs.getInt("ID" )) );
- // Show certain buttons
- btnFirst.setEnabled( true );
- btnPrevious.setEnabled( true ) ;
- btnNext.setEnabled( true );
- btnLast.setEnabled( true );
- btnUpdateRecord.setEnabled( true );
- btnDeleteRecord.setEnabled( true );
- btnNewRecord.setEnabled( true );
- btnSaveRecord.setEnabled( false );
- btnCancelRecord.setEnabled( false );
- }
- catch (SQLException err) {
- System.out.println(err.getMessage() );
- }
- }
- private void btnSaveRecordActionPerformed(java.awt.event.ActionEvent evt) {
- String first = textFirstName.getText( );
- String last = textLastName.getText( );
- String user = textUserType.getText( );
- String ID = textID.getText( );
- int newID = Integer.parseInt( ID );
- try {
- rs.moveToInsertRow();
- rs.updateInt("ID", newID);
- rs.updateString( "First_Name", first );
- rs.updateString( "last_Name", last );
- rs.updateString( "User_Type", user );
- rs.insertRow();
- stmt.close();
- rs.close();
- stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- String sql = "SELECT * FROM PLAYERS";
- rs = stmt.executeQuery(sql);
- rs.next();
- int id_col = rs.getInt("ID");
- String id = Integer.toString(id_col);
- String first2 = rs.getString("First_Name");
- String last2 = rs.getString("Last_Name");
- String user2 = rs.getString("User_Type");
- textID.setText(id);
- textFirstName.setText(first2);
- textLastName.setText(last2);
- textUserType.setText(user2);
- btnFirst.setEnabled( true );
- btnPrevious.setEnabled( true ) ;
- btnNext.setEnabled( true );
- btnLast.setEnabled( true );
- btnUpdateRecord.setEnabled( true );
- btnDeleteRecord.setEnabled( true );
- btnNewRecord.setEnabled( true );
- btnSaveRecord.setEnabled( false );
- btnCancelRecord.setEnabled( false );
- }
- catch (SQLException err) {
- System.out.println(err.getMessage() );
- }
- }
- private void btnDeleteRecordActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- rs.deleteRow();
- stmt.close();
- rs.close();
- stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- String sql = "SELECT * FROM PLAYERS";
- rs = stmt.executeQuery(sql);
- rs.next();
- int id_col = rs.getInt("ID");
- String id = Integer.toString(id_col);
- String first = rs.getString("First_Name");
- String last = rs.getString("Last_Name");
- String user = rs.getString("User_Type");
- textID.setText(id);
- textFirstName.setText(first);
- textLastName.setText(last);
- textUserType.setText(user);
- }
- catch (SQLException err) {
- System.out.println(err.getMessage() );
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Players().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton btnCancelRecord;
- private javax.swing.JButton btnDeleteRecord;
- private javax.swing.JButton btnFirst;
- private javax.swing.JButton btnLast;
- private javax.swing.JButton btnNewRecord;
- private javax.swing.JButton btnNext;
- private javax.swing.JButton btnPrevious;
- private javax.swing.JButton btnSaveRecord;
- private javax.swing.JButton btnUpdateRecord;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JPanel jPanel2;
- private javax.swing.JPanel jPanel3;
- private javax.swing.JTextField textFirstName;
- private javax.swing.JTextField textID;
- private javax.swing.JTextField textLastName;
- private javax.swing.JTextField textUserType;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement