Java!!!!!
Create a program that authenticates username and password. The program should have a clear
and submit button. If you click sumbit it will go to a new JFrame. The new Jframe contain a
menu button( that does nothing) and a logout button that exits the program.
Solution
import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class Login extends javax.swing.JFrame {
/**
* Creates new form Login
*/
public Login() {
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")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jPasswordField1 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("Hiding Data Cloud ");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel2.setText("User Name : ");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel3.setText("Password :");
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(96, 96, 96)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(69, 69, 69)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 134,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jPasswordField1))))))
.addContainerGap(112, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(jLabel1)
.addGap(53, 53, 53)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addComponent(jButton1)
.addContainerGap(82, Short.MAX_VALUE))
);
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == jButton1) {
if (jTextField1.getText().equals("") || jPasswordField1.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Please provide your details");
} else if (jTextField1.getText().equals("admin") ||
jPasswordField1.getText().equals("admin")) {
this.hide();
admin ahObj = new admin();
ahObj.setVisible(true);
} else {
try {
String getUname = jTextField1.getText();
String getPwd = jPasswordField1.getText();
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger")
;
PreparedStatement psObj = con.prepareStatement("select uname,pwd from register2 where
uname = '" + getUname + "' and pwd = '" + getPwd + "'");
ResultSet rsObj = psObj.executeQuery();
if (rsObj.next()) {
JOptionPane.showMessageDialog(null, "Login Successful");
search1 usObj = new search1();
search1.jLabel7.setText(this.jTextField1.getText());
// search1 usObj = new search1();
this.hide();
usObj.setVisible(true);
}
} catch (Exception eMsg) {
System.out.println(eMsg.getMessage());
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(Login.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathConstants;
import org.w3c.dom.Document;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author GALLEGA 7
*/
public class admin extends javax.swing.JFrame
{
/**
* Creates new form admin
*/
public admin() {
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")
//
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Location Title");
jLabel2.setText("Location Address");
jLabel3.setText("Latitude");
jLabel4.setText("Longitude");
jButton1.setText("Add Location");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Cancel");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("View Locations");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setText("Tracking");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setText("Get Values");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 95,
Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(60, 60, 60)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 92,
Short.MAX_VALUE)
.addComponent(jTextField2)
.addComponent(jTextField3)
.addComponent(jTextField4)))
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(39, 39, 39)
.addComponent(jButton2)))
.addContainerGap(134, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 109,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton5)
.addComponent(jButton3)))
.addGap(16, 16, 16))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addGroup(layout.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(23, 23, 23)
.addGroup(layout.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(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(2, 2, 2)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(26, 26, 26)
.addComponent(jButton3)
.addGap(18, 18, 18)
.addComponent(jButton4)
.addGap(26, 26, 26))
);
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource() == jButton1) {
if (jTextField1.getText().equals("") || jTextField2.getText().equals("") ||
jTextField3.getText().equals("") || jTextField4.getText().equals("") ) {
JOptionPane.showMessageDialog(null, "Please provide your details");
} else {
try {
String getTitle = jTextField1.getText();
String getAddress = jTextField2.getText();
String getLatitude = jTextField3.getText();
String getLongitude = jTextField4.getText();
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger")
;
PreparedStatement psObj = con.prepareStatement("insert into location values(?,?,?,?)");
psObj.setString(1, getTitle);
psObj.setString(2, getAddress);
psObj.setString(3, getLatitude);
psObj.setString(4, getLongitude);
int status = psObj.executeUpdate();
if (status != 0) {
JOptionPane.showMessageDialog(null, "Registered Succesfully");
this.hide();
admin hObj = new admin();
hObj.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "Error");
}
} catch (Exception eMsg) {
System.out.println(eMsg.getMessage());
}
}
} // TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(evt.getSource() == jButton2){
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
} // TODO add your handling code here:
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
viewlocdata ahObj = null;
try {
ahObj = new viewlocdata();
} catch (ClassNotFoundException ex) {
Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
}
ahObj.setVisible(true);
ahObj.setSize(500,400);// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
tracking t = null;
try {
t = new tracking();
} catch (ClassNotFoundException ex) {
Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
}
t.setVisible(true);
t.setSize(500,400);
}
/**
* @param args the command line arguments
*/
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getSource() == jButton5)
{
try
{
String getTitle = jTextField1.getText();
String getAddress = jTextField2.getText();
String postcode=getTitle+","+getAddress;
LatLng1 la=new LatLng1();
String latLongs[] = la.getLatLongPositions(postcode);
String Latitude=latLongs[0];
String Longtitude=latLongs[1];
jTextField3.setText(Latitude);
jTextField4.setText(Longtitude);
}catch(Exception ex)
{
}
} // TODO
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(admin.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE
, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new admin().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration
}
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.table.*;
public class tracking extends JFrame
{
public tracking() throws ClassNotFoundException
{
ArrayList columnNames = new ArrayList();
ArrayList data = new ArrayList();
// Connect to an MySQL Database, run query, get result set
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String userid = "system";
String password = "tiger";
String sql = "select * from tracking";
// Java SE 7 has try-with-resources
// This will ensure that the sql objects are closed when the program
// is finished with them
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection( url, userid, password );
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
{
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.add( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
ArrayList row = new ArrayList(columns);
for (int i = 1; i <= columns; i++)
{
row.add( rs.getObject(i) );
}
data.add( row );
}
}}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
// Create Vectors and copy over elements from ArrayLists to them
// Vector is deprecated but I am using them in this example to keep
// things simple - the best practice would be to create a custom defined
// class which inherits from the AbstractTableModel class
Vector columnNamesVector = new Vector();
Vector dataVector = new Vector();
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
// Create table with database data
JTable table = new JTable(dataVector, columnNamesVector)
{
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;
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
public static void main(String[] args) throws ClassNotFoundException
{
tracking t = new tracking();
t.setDefaultCloseOperation( EXIT_ON_CLOSE );
t.pack();
t.setSize(500,200);
t.setVisible(true);
}
}

Java!!!!!Create a program that authenticates username and password.pdf

  • 1.
    Java!!!!! Create a programthat authenticates username and password. The program should have a clear and submit button. If you click sumbit it will go to a new JFrame. The new Jframe contain a menu button( that does nothing) and a logout button that exits the program. Solution import java.sql.*; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.swing.*; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Administrator */ public class Login extends javax.swing.JFrame { /** * Creates new form Login */ public Login() { 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") // private void initComponents() {
  • 2.
    jLabel1 = newjavax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jPasswordField1 = new javax.swing.JPasswordField(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jLabel1.setText("Hiding Data Cloud "); jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel2.setText("User Name : "); jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel3.setText("Password :"); jButton1.setText("Login"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(96, 96, 96) .addComponent(jLabel1)) .addGroup(layout.createSequentialGroup() .addGap(69, 69, 69) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
  • 3.
    .addGroup(layout.createSequentialGroup() .addComponent(jLabel3) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1) .addComponent(jPasswordField1)))))) .addContainerGap(112,Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(35, 35, 35) .addComponent(jLabel1) .addGap(53, 53, 53) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3) .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(27, 27, 27) .addComponent(jButton1) .addContainerGap(82, Short.MAX_VALUE)) ); pack(); }// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { if (evt.getSource() == jButton1) { if (jTextField1.getText().equals("") || jPasswordField1.getText().equals("")) { JOptionPane.showMessageDialog(null, "Please provide your details"); } else if (jTextField1.getText().equals("admin") || jPasswordField1.getText().equals("admin")) { this.hide();
  • 4.
    admin ahObj =new admin(); ahObj.setVisible(true); } else { try { String getUname = jTextField1.getText(); String getPwd = jPasswordField1.getText(); Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger") ; PreparedStatement psObj = con.prepareStatement("select uname,pwd from register2 where uname = '" + getUname + "' and pwd = '" + getPwd + "'"); ResultSet rsObj = psObj.executeQuery(); if (rsObj.next()) { JOptionPane.showMessageDialog(null, "Login Successful"); search1 usObj = new search1(); search1.jLabel7.setText(this.jTextField1.getText()); // search1 usObj = new search1(); this.hide(); usObj.setVisible(true); } } catch (Exception eMsg) { System.out.println(eMsg.getMessage()); } } } }
  • 5.
    /** * @param argsthe command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* 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(Login.class.getName()).log(java.util.logging.Level.SEVERE , null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE , null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE , null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE , null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Login().setVisible(true); }
  • 6.
    }); } // Variables declaration- do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPasswordField jPasswordField1; private javax.swing.JTextField jTextField1; // End of variables declaration } import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import java.net.URLConnection; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.io.InputStreamReader; import java.io.BufferedReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathConstants; import org.w3c.dom.Document; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
  • 7.
    /** * * @author GALLEGA7 */ public class admin extends javax.swing.JFrame { /** * Creates new form admin */ public admin() { 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") // private void initComponents() { jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jTextField3 = new javax.swing.JTextField(); jTextField4 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jButton4 = new javax.swing.JButton(); jButton5 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Location Title"); jLabel2.setText("Location Address");
  • 8.
    jLabel3.setText("Latitude"); jLabel4.setText("Longitude"); jButton1.setText("Add Location"); jButton1.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Cancel"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("View Locations"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jButton4.setText("Tracking"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); jButton5.setText("Get Values"); jButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton5ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  • 9.
    .addGroup(layout.createSequentialGroup() .addGap(40, 40, 40) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(60, 60, 60) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE) .addComponent(jTextField2) .addComponent(jTextField3) .addComponent(jTextField4))) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addGap(39, 39, 39) .addComponent(jButton2))) .addContainerGap(134, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jButton5) .addComponent(jButton3))) .addGap(16, 16, 16)) ); layout.setVerticalGroup(
  • 10.
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(45, 45, 45) .addGroup(layout.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(23, 23, 23) .addGroup(layout.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(27, 27, 27) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(2, 2, 2) .addComponent(jButton5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton2) .addComponent(jButton1)) .addGap(26, 26, 26) .addComponent(jButton3) .addGap(18, 18, 18) .addComponent(jButton4) .addGap(26, 26, 26)) ); pack();
  • 11.
    }// private void jButton1ActionPerformed(java.awt.event.ActionEventevt) { if (evt.getSource() == jButton1) { if (jTextField1.getText().equals("") || jTextField2.getText().equals("") || jTextField3.getText().equals("") || jTextField4.getText().equals("") ) { JOptionPane.showMessageDialog(null, "Please provide your details"); } else { try { String getTitle = jTextField1.getText(); String getAddress = jTextField2.getText(); String getLatitude = jTextField3.getText(); String getLongitude = jTextField4.getText(); Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger") ; PreparedStatement psObj = con.prepareStatement("insert into location values(?,?,?,?)"); psObj.setString(1, getTitle); psObj.setString(2, getAddress); psObj.setString(3, getLatitude); psObj.setString(4, getLongitude); int status = psObj.executeUpdate(); if (status != 0) { JOptionPane.showMessageDialog(null, "Registered Succesfully"); this.hide(); admin hObj = new admin(); hObj.setVisible(true); } else { JOptionPane.showMessageDialog(null, "Error"); } } catch (Exception eMsg) { System.out.println(eMsg.getMessage());
  • 12.
    } } } // TODOadd your handling code here: } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { if(evt.getSource() == jButton2){ jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jTextField4.setText(""); } // TODO add your handling code here: } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { viewlocdata ahObj = null; try { ahObj = new viewlocdata(); } catch (ClassNotFoundException ex) { Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex); } ahObj.setVisible(true); ahObj.setSize(500,400);// TODO add your handling code here: } private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { tracking t = null; try { t = new tracking(); } catch (ClassNotFoundException ex) { Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex); } t.setVisible(true); t.setSize(500,400);
  • 13.
    } /** * @param argsthe command line arguments */ private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { if(evt.getSource() == jButton5) { try { String getTitle = jTextField1.getText(); String getAddress = jTextField2.getText(); String postcode=getTitle+","+getAddress; LatLng1 la=new LatLng1(); String latLongs[] = la.getLatLongPositions(postcode); String Latitude=latLongs[0]; String Longtitude=latLongs[1]; jTextField3.setText(Latitude); jTextField4.setText(Longtitude); }catch(Exception ex) {
  • 14.
    } } // TODO } publicstatic void main(String args[]) { /* Set the Nimbus look and feel */ // /* 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(admin.class.getName()).log(java.util.logging.Level.SEVERE , null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE , null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE
  • 15.
    , null, ex); }catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE , null, ex); } // /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new admin().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JButton jButton5; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; // End of variables declaration } import java.awt.*; import java.sql.*; import java.util.*; import javax.swing.*; import static javax.swing.JFrame.EXIT_ON_CLOSE;
  • 16.
    import javax.swing.table.*; public classtracking extends JFrame { public tracking() throws ClassNotFoundException { ArrayList columnNames = new ArrayList(); ArrayList data = new ArrayList(); // Connect to an MySQL Database, run query, get result set String url = "jdbc:oracle:thin:@localhost:1521:xe"; String userid = "system"; String password = "tiger"; String sql = "select * from tracking"; // Java SE 7 has try-with-resources // This will ensure that the sql objects are closed when the program // is finished with them try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection connection = DriverManager.getConnection( url, userid, password ); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql); { ResultSetMetaData md = rs.getMetaData(); int columns = md.getColumnCount(); // Get column names for (int i = 1; i <= columns; i++) { columnNames.add( md.getColumnName(i) ); } // Get row data while (rs.next()) { ArrayList row = new ArrayList(columns); for (int i = 1; i <= columns; i++) { row.add( rs.getObject(i) );
  • 17.
    } data.add( row ); } }} catch(SQLException e) { System.out.println( e.getMessage() ); } // Create Vectors and copy over elements from ArrayLists to them // Vector is deprecated but I am using them in this example to keep // things simple - the best practice would be to create a custom defined // class which inherits from the AbstractTableModel class Vector columnNamesVector = new Vector(); Vector dataVector = new Vector(); for (int i = 0; i < data.size(); i++) { ArrayList subArray = (ArrayList)data.get(i); Vector subVector = new Vector(); for (int j = 0; j < subArray.size(); j++) { subVector.add(subArray.get(j)); } dataVector.add(subVector); } for (int i = 0; i < columnNames.size(); i++ ) columnNamesVector.add(columnNames.get(i)); // Create table with database data JTable table = new JTable(dataVector, columnNamesVector) { public Class getColumnClass(int column) { for (int row = 0; row < getRowCount(); row++) { Object o = getValueAt(row, column);
  • 18.
    if (o !=null) { return o.getClass(); } } return Object.class; } }; JScrollPane scrollPane = new JScrollPane( table ); getContentPane().add( scrollPane ); JPanel buttonPanel = new JPanel(); getContentPane().add( buttonPanel, BorderLayout.SOUTH ); } public static void main(String[] args) throws ClassNotFoundException { tracking t = new tracking(); t.setDefaultCloseOperation( EXIT_ON_CLOSE ); t.pack(); t.setSize(500,200); t.setVisible(true); } }