U primjeru je klasa za:
1) kreiranje forme za izracun vrijednosti i prikaz istih u text poljima (izvor oracle.com)
2) klasa za iscitavanje i prikaz rezultata sql upita u JTable
3) klasa za prikaz rezultata u DOS sucelju
Stvar je uradjena u NetBeans-ima i bliska je logici VB programera
import java.awt.Color;
import java.text.NumberFormat;
import java.util.Locale;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* SamplesJFrame.java
*
* Created on 17.10.2011., 09:05:31
*/
/**
*
* @author Zana
*/
public class SamplesJFrame extends javax.swing.JFrame {
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/Books";
Vector columnNames = new Vector();
Vector data = new Vector();
/** Creates new form SamplesJFrame */
public SamplesJFrame() {
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() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
regularPayField = new javax.swing.JLabel();
overtimePayField = new javax.swing.JLabel();
totalField = new javax.swing.JLabel();
hoursWorked = new javax.swing.JTextField();
rate = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
projectTable = new javax.swing.JTable();
jButton3 = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jLabel9 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
OpenItem = new javax.swing.JMenuItem();
ExitItem = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Hours Worked:");
jLabel2.setText("Rate/ Hour ($):");
jLabel3.setText("Regular Pay:");
jLabel4.setText("Overtime Pay:");
jLabel5.setText("Total:");
regularPayField.setText("**********");
overtimePayField.setText("**********");
totalField.setText("**********");
jButton1.setText("Compute");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Dos prikaz");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
projectTable.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(projectTable);
jButton3.setText("Table prikaz");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel5)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2)
.addComponent(jLabel4)))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(totalField, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(overtimePayField, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(regularPayField, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(rate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
.addComponent(hoursWorked, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
.addGap(42, 42, 42)
.addComponent(jButton1)
.addGap(104, 104, 104)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jButton3)
.addGap(18, 18, 18))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 397, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(hoursWorked, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(rate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel5))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(regularPayField)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(overtimePayField)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(totalField)))
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Pay Calculator", jPanel1);
jLabel9.setIcon(new javax.swing.ImageIcon("C:\\Dokumente und Einstellungen\\Zana\\Eigene Dateien\\NetBeansProjects\\PayCalculatorAplication\\newjava.gif")); // NOI18N
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(97, Short.MAX_VALUE)
.addComponent(jLabel9)
.addGap(60, 60, 60))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(jLabel9)
.addContainerGap(185, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Pictures", jPanel2);
jMenu1.setText("File");
OpenItem.setText("Open");
jMenu1.add(OpenItem);
ExitItem.setText("Exit");
ExitItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitItemActionPerformed(evt);
}
});
jMenu1.add(ExitItem);
jMenuBar1.add(jMenu1);
jMenu2.setText("Help");
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)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 548, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(76, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void ExitItemActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//Gets how many hours worked from the text field, and then parses it to type int.
int hw = Integer.parseInt(hoursWorked.getText());
//Gets the pay rate entered in the text field, and parses it to type double.
double rateEntered=Double.parseDouble(rate.getText());
//Creates two variables of type double for later use.
double regularPay = 0;
double overTimePay = 0;
//Simple if statement for calculations
if (hw>40) {
regularPay = 40 * rateEntered;
overTimePay=(hw-40)*1.5*rateEntered;
} else {
regularPay=hw*rateEntered;
overTimePay=0.0;
}
//Creates a variable of both types of pay combined.
double total = regularPay + overTimePay;
//Creates variables for number formatting
NumberFormat nfRegular = NumberFormat.getCurrencyInstance(Locale.getDefault());
NumberFormat nfOverTime = NumberFormat.getCurrencyInstance(Locale.getDefault());
NumberFormat nfTotal = NumberFormat.getCurrencyInstance(Locale.getDefault());
//Writes the totals in the correct format to the labels
regularPayField.setText(" " + nfRegular.format(regularPay));
overtimePayField.setText(" " + nfOverTime.format(overTimePay));
totalField.setText(" " + nfTotal.format(total));
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//za prikaz u dos prozoru--------------------------------------------------------
// TODO add your handling code here:
Connection connection = null; // manages connection
Statement statement = null; // query statement
ResultSet resultSet = null; // manages results
// connect to database books and query database
try
{
// load the driver class
Class.forName( DRIVER );
// establish connection to database
connection =
DriverManager.getConnection( DATABASE_URL, "root", "" );
// create Statement for querying database
statement = connection.createStatement();
// query database
resultSet = statement.executeQuery(
"SELECT authorID, firstName, lastName FROM authors" );
//za prikaz u dos prozoru--------------------------------------------------------
//za prikaz u dos prozoru--------------------------------------------------------
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println( "Authors Table of Books Database:\n" );
for ( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t", metaData.getColumnName( i ));
System.out.println();
while( resultSet.next())
{
for ( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t", resultSet.getObject( i ) );
System.out.println();
} // end while
} // end try
catch(SQLException sqlException )
{
sqlException.printStackTrace();
} // end catch
catch(ClassNotFoundException classNotFound )
{
classNotFound.printStackTrace();
} // end catch
finally // ensure resultSet, statement and connection are closed
{
try
{
resultSet.close();
statement.close();
connection.close();
} // end try
catch ( Exception exception )
{
exception.printStackTrace();
}
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Connection con = null; // manages connection
Statement statement = null; // query statement
try {
// establish connection to database
con =
DriverManager.getConnection( DATABASE_URL, "root", "" );
// create Statement for querying database
ResultSet rows;
Statement s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String select = "SELECT authorID, firstName, lastName FROM authors";
rows = s.executeQuery(select);
int i = 0;
ResultSetMetaData rsmd = rows.getMetaData();
int numColumns = rsmd.getColumnCount();
// Get the column names; column indices start from 1
for (int j=1; j<numColumns+1; j++) {
int vColIndex = 0;
String columnName = rsmd.getColumnName(j);
// projectTable.setValueAt(columnName, 1, j-1);
projectTable.getColumnModel().getColumn(j-1).setHeaderValue(columnName);
JTableHeader header = projectTable.getTableHeader();
header.setBackground(Color.yellow);
// header.set (columnName);
// projectTable.
// Get the name of the column's table name
// String tableName = rsmd.getTableName(i);
}
while (rows.next()){
String projName = rows.getString("authorID");
String status = rows.getString("firstName");
String date = rows.getString("lastName");
//String columnsv =
// String timeAllocated = rows.getString("timeAllocated");
// String timeSpent = rows.getString("timeSpent");
// String billing = rows.getString("billing");
//calling data into jtable for display
projectTable.setValueAt(projName, i, 0);
projectTable.setValueAt(status, i, 1);
projectTable.setValueAt(date, i, 2);
// projectTable.setValueAt(timeAllocated, i, 3);
// projectTable.setValueAt(timeSpent, i, 4);
// projectTable.setValueAt(billing, i, 5);
i++;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
//za prikaz u dos prozoru--------------------------------------------------------
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SamplesJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem ExitItem;
private javax.swing.JMenuItem OpenItem;
private javax.swing.JTextField hoursWorked;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel9;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JLabel overtimePayField;
private javax.swing.JTable projectTable;
private javax.swing.JTextField rate;
private javax.swing.JLabel regularPayField;
private javax.swing.JLabel totalField;
// End of variables declaration
JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
}