import java.sql.Connection;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.Date;
public class Examp extends javax.swing.JFrame {
DBCon DB=new DBCon();
ValidationCheck vc=new ValidationCheck();
Vector tmp=new Vector();
Vector data=new Vector();
Vector heading=new Vector();
Connection con=null;
Statement st=null;
String Branch;
String TransactionType;
String PC;
String Cust;
public Examp() throws Exception {
heading.addElement("Branch");
heading.addElement("Transaction Type");
heading.addElement("Principal Company");
heading.addElement("Customer");
heading.addElement("Date");
initComponents();
SimpleDateFormat dateformat=new SimpleDateFormat("dd/MM/yyyy");
Date date=new Date();
DateText.setText(dateformat.format(date));
clearData();
}
public void insertIntoDB() throws Exception
{
con=DB.dbConnection();
st=con.createStatement();
st.executeUpdate("Insert into Transactions values('"+BranchText.getText()+ "','"+TransactionTypeCombo.getSelectedItem()+"','"+PCCombo.getSelectedItem()+"','"+CustCombo.getSelectedItem()+"','"+DateText.getText()+"')");
}
public boolean validateData()
{
Branch=BranchText.getText();
TransactionType=(String)TransactionTypeCombo.getSelectedItem();
PC=(String)PCCombo.getSelectedItem();
Cust=(String)CustCombo.getSelectedItem();
if(vc.checkIfNull(Branch)==false)
{
ErrorLabel.setText("Please Enter Branch");
return false;
}
else if(vc.checkFullName(Branch)==false)
{
ErrorLabel.setText("Branch Name is Incorrect");
return false;
}
else if(vc.checkComboNull(TransactionType)==false)
{
ErrorLabel.setText("Please Select Transaction Type");
return false;
}
else if(vc.checkComboNull(PC)==false)
{
ErrorLabel.setText("Please Select Principal Company");
return false;
}
else if(vc.checkComboNull(Cust)==false)
{
ErrorLabel.setText("Please Select Customer");
return false;
}
else
{
ErrorLabel.setText("");
SuccessLabel.setText("Entered Successfully");
SuccessLabel.setVisible(true);
return true;
}
}
public void clearData()
{
BranchText.setText("");
TransactionTypeCombo.setSelectedItem("Select");
PCCombo.setSelectedItem("Select");
CustCombo.setSelectedItem("Select");
ErrorLabel.setText("");
SuccessLabel.setText("");
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
TransactionPanel = new javax.swing.JPanel();
BranchLabel = new javax.swing.JLabel();
BranchText = new javax.swing.JTextField();
TransactionTypeLabel = new javax.swing.JLabel();
PCLabel = new javax.swing.JLabel();
PCCombo = new javax.swing.JComboBox();
CustLabel = new javax.swing.JLabel();
TransactionTypeCombo = new javax.swing.JComboBox();
CustCombo = new javax.swing.JComboBox();
DateLabel = new javax.swing.JLabel();
DateText = new javax.swing.JTextField();
SubmitButton = new javax.swing.JButton();
ClearButton = new javax.swing.JButton();
ErrorLabel = new javax.swing.JLabel();
SuccessLabel = new javax.swing.JLabel();
ViewButton = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
TransactionTable = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Transaction");
TransactionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Transaction"));
BranchLabel.setText("Branch");
TransactionTypeLabel.setText("Transaction Type");
PCLabel.setText("Principal Company");
PCCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "Zuari", "Birla" }));
CustLabel.setText("Customer");
TransactionTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "GoDown", "Direct Delivery" }));
CustCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "M/S Asociates", "ABC Merchandale" }));
DateLabel.setText("Date");
SubmitButton.setText("Submit");
SubmitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SubmitButtonActionPerformed(evt);
}
});
ClearButton.setText("Clear");
ClearButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ClearButtonActionPerformed(evt);
}
});
ErrorLabel.setForeground(new java.awt.Color(255, 0, 0));
ErrorLabel.setText("Error:");
SuccessLabel.setForeground(new java.awt.Color(0, 153, 0));
SuccessLabel.setText("Success");
javax.swing.GroupLayout TransactionPanelLayout = new javax.swing.GroupLayout(TransactionPanel);
TransactionPanel.setLayout(TransactionPanelLayout);
TransactionPanelLayout.setHorizontalGroup(
TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BranchLabel)
.addComponent(TransactionTypeLabel)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(SubmitButton)
.addComponent(PCLabel))
.addComponent(CustLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(BranchText, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
.addComponent(TransactionTypeCombo, 0, 130, Short.MAX_VALUE)
.addComponent(PCCombo, 0, 130, Short.MAX_VALUE)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(ClearButton)
.addComponent(DateText, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(CustCombo, 0, 127, Short.MAX_VALUE))))
.addComponent(DateLabel)))
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TransactionPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(SuccessLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
TransactionPanelLayout.setVerticalGroup(
TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(TransactionPanelLayout.createSequentialGroup()
.addGap(9, 9, 9)
.addComponent(ErrorLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BranchLabel)
.addComponent(BranchText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(TransactionTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(TransactionTypeLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PCCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(PCLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(CustLabel)
.addComponent(CustCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(DateLabel)
.addComponent(DateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(TransactionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(SubmitButton)
.addComponent(ClearButton))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(SuccessLabel)
.addContainerGap(13, Short.MAX_VALUE))
);
ViewButton.setText("View");
ViewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ViewButtonActionPerformed(evt);
}
});
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Transaction View"));
TransactionTable.setModel(new javax.swing.table.DefaultTableModel(
data,heading
));
jScrollPane1.setViewportView(TransactionTable);
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()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(TransactionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(58, 58, 58)
.addComponent(ViewButton)
.addContainerGap(171, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(142, 142, 142)
.addComponent(ViewButton))
.addGroup(layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(TransactionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void SubmitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitButtonActionPerformed
if(evt.getSource()==SubmitButton)
{
try{
if(validateData()==true)
{
insertIntoDB();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}//GEN-LAST:event_SubmitButtonActionPerformed
public Vector getTransac() throws Exception
{
ResultSet rs=null;
rs=st.executeQuery("Select * from Transactions");
while(rs.next())
{
tmp.addElement(rs.getString(1));
tmp.addElement(rs.getString(2));
tmp.addElement(rs.getString(3));
tmp.addElement(rs.getString(4));
tmp.addElement(rs.getString(5));
data.addElement(tmp);
}
/* if(con!=null)
{
con.close();
}*/
return data;
}
private void ClearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearButtonActionPerformed
clearData();
}//GEN-LAST:event_ClearButtonActionPerformed
private void ViewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ViewButtonActionPerformed
try{
Vector data1=new Vector();
data1=getTransac();
}
catch(Exception e)
{
System.out.println(e);
}
}//GEN-LAST:event_ViewButtonActionPerformed
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
new Examp().setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel BranchLabel;
private javax.swing.JTextField BranchText;
private javax.swing.JButton ClearButton;
private javax.swing.JComboBox CustCombo;
private javax.swing.JLabel CustLabel;
private javax.swing.JLabel DateLabel;
private javax.swing.JTextField DateText;
private javax.swing.JLabel ErrorLabel;
private javax.swing.JComboBox PCCombo;
private javax.swing.JLabel PCLabel;
private javax.swing.JButton SubmitButton;
private javax.swing.JLabel SuccessLabel;
private javax.swing.JPanel TransactionPanel;
private javax.swing.JTable TransactionTable;
private javax.swing.JComboBox TransactionTypeCombo;
private javax.swing.JLabel TransactionTypeLabel;
public javax.swing.JButton ViewButton;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}
//This is the class for Database Connection(with MS SQL Database)
import java.sql.*;
public class DBCon {
public Connection dbConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
return DriverManager.getConnection("Jdbc:odbc:MyDSN");
}
}
//This is the class to validate the fields...
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class ValidationCheck {
public boolean checkFloat(String in) {
//To Check whether the number is correct or not
try {
//Integer.parseInt(in);
Float.parseFloat(in);
} catch (NumberFormatException ex) {
return false;
}
return true;
}
public boolean checkIfNull(String inp)
{
if((inp.matches(""))||(inp.equals(null)))
{
return false;
}
else
{
return true;
}
}
public boolean checkDateDDMMYY(String date)
{
//To Check Date in DD/MM/YY
if(date.matches("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d"))
{
return true;
}
else
{
return false;
}
}
public boolean checkDateMMDDYY(String date)
{
//To Check Date in MM/DD/YY
if(date.matches("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d"))
{
return true;
}
else
{
return false;
}
}
public boolean checkEmail(String email)
{
//To Check E-Mail
if(email.matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[_A-Za-z0-9-]+)"))
{
return true;
}
else
{
return false;
}
}
public boolean checkComboNull(String comb)
{
if((comb.matches("Select")))
{
return false;
}
else
{
return true;
}
}
public boolean containsOnlyNumbers(String str) {
//It can't contain only numbers if it's null or empty...
if (str == null || str.length() == 0)
return false;
for (int i = 0; i < str.length(); i++) {
//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
public boolean checkPinCode(String PIN)
{
if(PIN.matches("\\d{6}"))
{
return true;
}
else
{
return false;
}
}
public boolean checkPhone(String phone)
{
if(phone.matches("\\d{10}"))
{
return true;
}
else
{
return false;
}
}
public boolean checkName(String name)
{
if(name.matches("[a-zA-Z]*" ))
{
return true;
}
else
{
return false;
}
}
public boolean dateValidator(String dateString)
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
try
{
cal.setTime(sdf.parse(dateString));
if(dateString.equals(sdf.format(cal.getTime())))
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
return false;
}
}
public boolean checkFullName(String fullname)
{
if(fullname.matches("[a-zA-z]+([ '-./][a-zA-Z]+)*"))
{
return true;
}
else
{
return false;
}
}
public boolean checkURL(String urlname)
{
if(urlname.matches("(http[s]?
{
return true;
}
else
{
return false;
}
}
}
Please Reply......
This post has been edited by tinoymalayil: 16 July 2009 - 09:31 PM

New Topic/Question
Reply




MultiQuote






|