Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


HarithaAdapa
java time
2 Answer(s)      4 years and 3 months ago
Posted in : Java Beginners


Hi,

Iam created a desktop login application using swings.
pls observe the following code:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

class LoginDemo{
JButton SUBMIT;
JFrame f;
JLabel label1,label2;
final JTextField text1;
final JPasswordField text2;
LoginDemo(){
f=new JFrame();
f.getContentPane().setLayout(null);
label1 = new JLabel();
label1.setText("UserName:");
label1.setBounds(400,50,100,20);
text1 = new JTextField(25);
text1.setBounds(500,50,100,20);
label2 = new JLabel();
label2.setText("Password:");
label2.setBounds(400,80,100,20);
text2 = new JPasswordField(25);
text2.setBounds(500,80,100,20);
SUBMIT=new JButton("Login");
SUBMIT.setBounds(500,110,100,20);

f.add(label1);
f.add(text1);
f.add(label2);
f.add(text2);
f.add(SUBMIT);
f.setSize(1024,768);
f.setVisible(true);
SUBMIT.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String user1="";
String pass1="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:odbc:msdesktop");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM test where Empname='"+value1+"' and Emppassword='"+value2+"'");
while (res.next()) {
user1 = res.getString("Empname");
pass1 = res.getString("Emppassword");
}
if(!value1.equals(user1) && !value2.equals(pass1)) {
JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE);
text1.setText("");
text2.setText("");
}
else if(value1.equals("")){
JOptionPane.showMessageDialog(null,"Please Enter Username");
}
else if(value2.equals("")){
JOptionPane.showMessageDialog(null,"Please Enter Password");
}
else{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:odbc:msdesktop");
PreparedStatement pstmt = conn.prepareStatement("insert into logininformation(username,logindate,logintime) values(?,?,?)");
pstmt.setString(1, value1);
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
pstmt.setDate(2, sqlDate);
java.sql.Time sqlTime = new java.sql.Time(new java.util.Date().getTime());
pstmt.setTime(3,sqlTime);
/*java.sql.Timestamp sqlDate = new java.sql.Timestamp(new java.util.Date().getTime());
pstmt.setTimestamp(2, sqlDate);*/
pstmt.executeUpdate();
pstmt.close();
conn.close();
JOptionPane.showMessageDialog(null,"Login Successful");
}
catch(Exception ex){}
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
});
}
public static void main(String arg[]) {
LoginDemo frame=new LoginDemo();
}
}



and




import mysql.DataAcc;
// import package class for getting database connection
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class D1 extends JFrame implements ActionListener{
//instance variable
String str="";
/* Swing classess */
JLabel l,l1,l2,l3,l4,title,l5,l6;
JTextField t,t1,t2,t3,t4;
JButton b,b1,b2,b3,b4,b5,exit;
JTextArea ta,ta1;
//create new instance of class D1
D1() {
setSize(700,500); //set Frame Size
Container c=getContentPane(); //Objects add to Container
c.setLayout(null);
ta=new JTextArea(30,30);
title=new JLabel("Employee Information");
l1=new JLabel("Employee Number");
l2=new JLabel("Employee Name");
l=new JLabel("Employee Password");
l3=new JLabel("Employee Salary");
l4=new JLabel("Employee Address");
l5=new JLabel("Database Operation's ");
t1=new JTextField(10);
t2=new JTextField(10);
t =new JTextField(10);
t3=new JTextField(10);
t4=new JTextField(10);
b=new JButton("Reports");
b1=new JButton("Insert");
b2=new JButton("Delete");
b3=new JButton("Update");
b4=new JButton("Single");
b5=new JButton("View");
exit=new JButton("Exit");
//set bounds() palce components in a givel dimesions in the Frame
title.setBounds(200,10,150,100);
//components add to Continer
c.add(title);
l1.setBounds(100,40,150,100);
c.add(l1);
l2.setBounds(100,70,150,100);
c.add(l2);
l.setBounds(100,100,150,100);
c.add(l);
l3.setBounds(100,130,150,100);
c.add(l3);
l4.setBounds(100,160,150,100);
c.add(l4);
l5.setBounds(200,230,250,100);
c.add(l5);
t1.setBounds(250,80,150,20);
c.add(t1);
t2.setBounds(250,110,150,20);
c.add(t2);
t.setBounds(250,140,150,20);
c.add(t);
t3.setBounds(250,170,150,20);
c.add(t3);
t4.setBounds(250,210,150,20);
c.add(t4);
//add buttons to the ActionListener Interface
b .addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
exit.addActionListener(this);
b .setBounds(10,240,80,20);
c.add(b);
b1.setBounds(100,240,80,20);
c.add(b1);
b2.setBounds(190,240,80,20);
c.add(b2);
b3.setBounds(280,240,80,20);
c.add(b3);
b4.setBounds(370,240,80,20);
c.add(b4);
b5.setBounds(460,240,80,20);
c.add(b5);
exit.setBounds(550,240,80,20);
c.add(exit);
ta.setBounds(200,300,270,100);
c.add(ta);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Connection con=null;
DataAcc d=new DataAcc();
if(e.getSource()==b1) {
ta.setText(str);
/*get values from textfields*/
int sno=Integer.parseInt(t1.getText());
String sname=t2.getText();
String spwd =t.getText();
double sal=Double.parseDouble(t3.getText());
String address=t4.getText();
try {
con=d.getConnect();
PreparedStatement pst=con.prepareStatement("insert into test values(?,?,?,?,?)");
pst.setInt(1,sno);
pst.setString(2,sname);
pst.setString(3,spwd);
pst.setDouble(4,sal);
pst.setString(5,address);
int i=pst.executeUpdate();
if(i>0)
l5.setText("Insertion Operation Performed");
else
l5.setText("Insertion Operation Failed");
pst.clearParameters();
/*text field values clear after insertion performed*/
t1.setText("");
t2.setText("");
t.setText("");
t3.setText("");
t4.setText("");
}catch(Exception ae){
ae.printStackTrace();
}
}//Buuton B1 if




if(e.getSource()==b){
int sno=Integer.parseInt(t1.getText());
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:msdesktop");
PreparedStatement pst=con.prepareStatement("select * from test where Empnumber="+sno);
ResultSet rs=pst.executeQuery();
JFrame f=new JFrame();
f.getContentPane().setLayout(null);
int n1=0,n3=0;
String n="",n2="",n4="";
if(rs.next()) {
n1=rs.getInt(1);
n2=rs.getString(2);
n=rs.getString(3);
n3=rs.getInt(4);
n4=rs.getString(5);
}
f.add(new JLabel("Emp No:")).setBounds(400,50,100,30);
f.add(new JLabel(Integer.toString(n1))).setBounds(500,50,100,30);
f.add(new JLabel("Name:")).setBounds(400,100,100,30);
f.add(new JLabel(n2)).setBounds(500,100,100,30);
f.add(new JLabel("Password:")).setBounds(400,150,100,30);
f.add(new JLabel(n)).setBounds(500,150,100,30);
f.add(new JLabel("Salary:")).setBounds(400,200,100,30);
f.add(new JLabel(Integer.toString(n3))).setBounds(500,200,100,30) ;
f.add(new JLabel("Address:")).setBounds(400,250,100,30);
f.add(new JLabel(n4)).setBounds(500,250,100,30);
f.setVisible(true);
f.setSize(1000,1000);
}catch(Exception ae){
ae.printStackTrace();
}
}//Buuton B1 if





if(e.getSource()==b5) {
ta.setText(str);
try {
con=d.getConnect();
PreparedStatement pst=con.prepareStatement("select * from test");
ResultSet rs=pst.executeQuery();
while(rs.next()) {
int n1=rs.getInt(1);
String n2=rs.getString(2);
String n=rs.getString(3);
double n3=rs.getDouble(4);
String n4=rs.getString(5);
/*append all values to the text area by ta.append()*/
ta.append(""+n1+"\t"+n2+"\t"+n+"\t"+n3+"\t"+n4+"\n");
//JOptionPane.showMessageDialog(null,n1+"\n"+n2+"\n"+n3+"\n"+n4);
l5.setText("View test Table Data");
}
}catch(Exception ae){
ae.printStackTrace();
}
}//Button b5 if
if(e.getSource()==b2) {
ta.setText(str);
int sno=Integer.parseInt(t1.getText());
try {
con=d.getConnect();
PreparedStatement pst=con.prepareStatement("delete from test where Empnumber="+sno);
int i=pst.executeUpdate();
if(i>0)
l5.setText("Deletion Operation Performed");
else
l5.setText("Deletion Operation Failed");
pst.clearParameters();
t1.setText("");
t2.setText("");
t.setText("");
t3.setText("");
t4.setText("");
}catch(Exception ae){
ae.printStackTrace();
}
}//Button b2 if
if(e.getSource()==b3) {
ta.setText(str);
int sno=Integer.parseInt(t1.getText());
String sname=t2.getText();
String spwd=t.getText();
double sal=Double.parseDouble(t3.getText());
String address=t4.getText();
try {
con=d.getConnect();
//PreparedStatement pst=con.prepareStatement("update test set sname=?,spwd=?,sal=?,address=? where sno=?");
PreparedStatement pst=con.prepareStatement("update test set Empname=?,Emppassword=?,Empsal=?,Empadd=? where Empnumber=?");
pst.setString(1,sname);
pst.setString(2,spwd);
pst.setDouble(3,sal);
pst.setString(4,address);
pst.setInt(5,sno);
int i=pst.executeUpdate();
if(i>0)
l5.setText("Update Operation Performed");
else
l5.setText("Update Operation Failed");
pst.clearParameters();
t1.setText("");
t2.setText("");
t.setText("");
t3.setText("");
t4.setText("");
}catch(Exception ae){
ae.printStackTrace();
}
}//button b3 if
if(e.getSource()==b4) {
ta.setText(str);
int sno=Integer.parseInt(t1.getText());
try {
con=d.getConnect();
PreparedStatement pst=con.prepareStatement("select * from test where Empnumber="+sno);
ResultSet rs=pst.executeQuery();
if(rs.next()) {
t1.setText(""+rs.getInt(1));
t2.setText(rs.getString(2));
t.setText(rs.getString(3));
t3.setText(""+rs.getDouble(4));
t4.setText(rs.getString(5));
ta.append(t1.getText()+"\n"+t2.getText()+"\n"+t.getText()+"\n"+t3.getText()+"\n"+t4.getText());
l5.setText("Record Avilable with Serial Number : "+sno);
}
else {
l5.setText("No Record Avilable with Serial Number : "+sno);
pst.clearParameters();
t1.setText("");
t2.setText("");
t.setText("");
t3.setText("");
t4.setText("");
}
}catch(Exception ae){
ae.printStackTrace();
}
}//Button b5 if
if(e.getSource()==exit)
System.exit(0);

}
}
class Database {
public static void main(String[] args) {
D1 d=new D1();
}

}




and




package mysql;
import java.sql.*;
import java.io.*;

public class DataAcc
{
public Connection con=null;
public Connection getConnect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:msdesktop");
System.out.println("Connected");
}
catch(Exception e)
{
System.out.println(e);
}
return con;
}//getConnection

}//class




the o/p is coming properly.
when the valid user in login the name,date,time are stored in the database.

my doubt is if the valid user is login moretimes a day then minimum time among them will be stored into the database remaining will be not stored into the database and also if the valid user is logout moretimes a day then maximum time among them will be stored into the database remaining will be not stored into the database.


pls help me.

Advertisement
View Answers

December 12, 2012 at 3:02 PM


import java.util.Date;

public class Time{
        Date Intime;
    Date Outtime;
    public Time(Date intime, Date outtime) {
        super();
        Intime = intime;
        Outtime = outtime;
    }
    @Override
    public String toString() {
        return "Time [Intime=" + Intime + ", Outtime=" + Outtime + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((Intime == null) ? 0 : Intime.hashCode());
        result = prime * result + ((Outtime == null) ? 0 : Outtime.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Time other = (Time) obj;
        if (Intime == null) {
            if (other.Intime != null)
                return false;
        } else if (!Intime.equals(other.Intime))
            return false;
        if (Outtime == null) {
            if (other.Outtime != null)
                return false;
        } else if (!Outtime.equals(other.Outtime))
            return false;
        return true;
    }
    public Date getIntime() {
        return Intime;
    }
    public void setIntime(Date intime) {
        Intime = intime;
    }
    public Date getOuttime() {
        return Outtime;
    }
    public void setOuttime(Date outtime) {
        Outtime = outtime;
    }
    public static void main(String[] args){
        System.out.println("intime"+8.30);
        System.out.println("outtime is"+3.00);
        System.out.println("student is prgnt");
    }
}


December 12, 2012 at 3:02 PM


import java.util.Date;

public class Time{
        Date Intime;
    Date Outtime;
    public Time(Date intime, Date outtime) {
        super();
        Intime = intime;
        Outtime = outtime;
    }
    @Override
    public String toString() {
        return "Time [Intime=" + Intime + ", Outtime=" + Outtime + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((Intime == null) ? 0 : Intime.hashCode());
        result = prime * result + ((Outtime == null) ? 0 : Outtime.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Time other = (Time) obj;
        if (Intime == null) {
            if (other.Intime != null)
                return false;
        } else if (!Intime.equals(other.Intime))
            return false;
        if (Outtime == null) {
            if (other.Outtime != null)
                return false;
        } else if (!Outtime.equals(other.Outtime))
            return false;
        return true;
    }
    public Date getIntime() {
        return Intime;
    }
    public void setIntime(Date intime) {
        Intime = intime;
    }
    public Date getOuttime() {
        return Outtime;
    }
    public void setOuttime(Date outtime) {
        Outtime = outtime;
    }
    public static void main(String[] args){
        System.out.println("intime"+8.30);
        System.out.println("outtime is"+3.00);
        System.out.println("student is prgnt");
    }
}


Related Tutorials/Questions & Answers:
get absolute time java
get absolute time java  How to get absolute time in java
Java run time polymorphism
Java run time polymorphism  What is run-time polymorphism or dynamic method dispatch
Advertisements
Time Table in Java
Time Table in Java  Hi, Deepak i am developing a time table for a school on java,but i am confuse how to start so please give me idea about
java time - Java Beginners
java time  Hi, Iam created a desktop login application using swings. pls observe the following code: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; class LoginDemo{ JButton
date and time in awt(java)
date and time in awt(java)  sir, do you have an example of date in awt java which can be view over a textfield.   Display time in JTextField import java.util.*; import javax.swing.*; import java.awt.event.*; public
different execution time - Java Beginners
different execution time    hello, when i run the bellow code more than one time i am getting different execution time("Total time taken"), Ex. when i run first time it prints Total time taken 47 second time it prints Total
Java Execution Time Measurement(JETM)
Java Execution Time Measurement(JETM)  How can we integrate JETM in eclipse
time
time  how to find the current system time between the user login and logout using java
Java Execution Time Measurement(JETM)
Java Execution Time Measurement(JETM)  Whether JETM can be implemented in Struts framework especially in Struts2. If not,tell the alternative solution
Real time examples - Java Beginners
Real time examples  what is method overloading,method overriding,constructor overloading concept in java and explain with real time examples? .../java/master-java/method_overloading.shtml http://www.roseindia.net/java
time
time  sir, i want to show system time on a textfield in awt(java). woulk you send me an example of time which can show time in standard format like H:M:S pls help me sir   Display time in JTextField import
java run time error - JDBC
java run time error  when i m running for batch execution program in jdbc i m facing this kind of runtime error as Exception in thread "main" java.sql.SQLException: Io exception: The Network Adap ter could not establish
How much time - Java Beginners
How much time  Hi, I have some knowledge on java.I wanted to learn spring and hibernate as soon as possible. please tell me how much time it takes to get good command on this technologies.is this enough if i follow
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do? For this i already received the answer. But its converting the time in 24hr format to 12hr format
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do? For this i already received the answer. But its converting the time in 24hr format to 12hr format
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do? For this i already received the answer. But its converting the time in 24hr format to 12hr format
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do? For this i already received the answer. But its converting the time in 24hr format to 12hr format
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do? For this i already received the answer. But its converting the time in 24hr format to 12hr format
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do? For this i already received the answer. But its converting the time in 24hr format to 12hr format
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do? For this i already received the answer. But its converting the time in 24hr format to 12hr format
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do? For this i already received the answer. But its converting the time in 24hr format to 12hr format
Java Time Zone Example
Java Time Zone Example       Converting time between time zones In this section we have written a java program which will take two Time Zone IDs as its argument and then convert
time
time format hh:mm:ss  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do?   Here is a jsp example that fetch the time from the database and display
time
time display only as hh:mm.  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do?   Here is a jsp example that fetch the time from the database
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do?   Here is a jsp example that fetch the time from the database and display in different
time
time  Hi. I have retrieved time from mysql database. its in th format hh:mm:ss. but i want to display only as hh:mm. wat to do?   Here is a jsp example that fetch the time from the database and display in different
time
retrieved time from mysql database  Hi. I have retrieved time from.... wat to do?   Here is a jsp example that fetch the time from... is a jsp example that fetch the time from the database and display in different
Java compile time error. - Java Beginners
Java compile time error.  CreateProcess error=2, The system cannot..., Please specify in detail and send me code. If you are new java... : http://www.roseindia.net/java/master-java/index.shtml Thanks & Regards
how to convert time in milliseconds in to hours - Java Beginners
how to convert time in milliseconds in to hours  hello, can anyone tell me how i can convert the time in milliseconds into hours or to display it in seconds in java
how to convert time in milliseconds in to hours - Java Beginners
how to convert time in milliseconds in to hours  hello, can anyone tell me how i can convert the time in milliseconds into hours or to display it in seconds in java
Calculate process time in Java
Calculate process time in Java   ... the process time of the completion of the operation through the Java program... for the best illustration of the procedure of calculating the process time in which any
 

 

 

DMCA.com