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




Have Programming Question? Ask it here!
 
 
 


HarithaAdapa
java desktop
0 Answer(s)      4 years 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

Related Tutorials/Questions & Answers:
java--desktop appilication - JDBC
java--desktop appilication  when class.forName() is called ,,,a class is loaded...once after the class is loaded ..if we call class.forName, it will call the class which is already present.......what to do if we want to load
java desktop - Java Beginners
java desktop  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
Advertisements
java desktop - Java Beginners
java desktop  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
Java Spring Desktop application
Java Spring Desktop application  Hi i am developing a desktop java spring application..It has many Jframes forms and classes...How to close one Jframe from another..Please tell... First jframe package desktopapplication2
DESKTOP SHARING USING JAVA
DESKTOP SHARING USING JAVA  Sir i have the following code for scren capturing it is capturing in png format but when i am sending it over other m/c it is taking delay of around 16 seconds..i think it is because this line of code
Simple Java Desktop Upload application
Simple Java Desktop Upload application  I try do simple example for upload applicationtake file from c:\ put to d:\ :) PLEASE HELP
Java program to get the desktop Path
Java program to get the desktop Path  ... the desktop path of the system. In the java environment we can get the desktop path also with the system's property. For getting the desktop path we have to add
Create a Desktop Pane Container in Java
Create a Desktop Pane Container in Java   ... pane container in Java. The desktop pane container is a container, which has... under the desktop pane. Following figure shows the JDesktopPane component of Java
EasyEclipse Desktop Java
EasyEclipse Desktop Java       EasyEclipse Desktop Java For development of Desktop... - Shared platform services from Eclipse - Getting started. Java JDK
path setting for tomcat to java for Desktop PC - JDBC
path setting for tomcat to java for Desktop PC  Hi sir, I want to connect my java programms to Database which of either sql,oracle,mysql... or configuration about the Tomcat Server in Java. Kindly visit them . We hope
Java Spring Desktop application with multiple classes and jframe
Java Spring Desktop application with multiple classes and jframe  Hi i am developing a desktop java spring application..It has many Jframes forms and classes...How to close one Jframe from another..Please tell... First jframe
linux desktop on windows without using vn or any other ? - Java Magazine
linux desktop on windows without using vn or any other ?  linux desktop on windows without using vn or any other ?. I have a linux pc and windows xp pc but i am not able to connect the linux pc on windows .why? .windows
steps to create desktop application in java swing using eclipse with drag and drop options
steps to create desktop application in java swing using eclipse with drag and drop options  please help to create desktop application in java swing using eclipse with drag and drop and what are the plugins required
desktop application
desktop application  how to transfer data swing text field to browser text field to their corresponding label/caption. please reply as soon as possible. thanks vijay
How can I open my Java desktop app by clicking on its file?
How can I open my Java desktop app by clicking on its file?  How can I open my Java desktop app by clicking on the file it creates? This app creates files when selecting its "Save as" option with an extension specificaly defined
Using the Desktop class to launch a URL
Using the Desktop class to launch a URL with default browser...; Using the Desktop class to launch a URL with default browser in Java  This article describes the new Desktop API
Works when run from desktop but....
Works when run from desktop but....  I used the code provided here for a pie chart using swing in Java, and when I saved it to my desktop, it ran fine, and worked as explained, but then I tried to add the same code to NetBeans
Remote mobile desktop - MobileApplications
Remote mobile desktop  I am doing my research to implement a java mobile application to access another remote mobile phone with the authentication.This application will have to use the service provider's network and a server
Remote desktop access - JSP-Servlet
Remote desktop access  Hello everyone Thank a lot for all the reply given before i have my 4th question which is about Remote desktop accessing inside the network and also accessing the computer inside domain ...so please
Google Desktop Search
Google Desktop Search     ... capabilities, this plug-in uses the Google Desktop Search Engine instead. Usage The search can be started from either the Search menu or the Google Desktop
Remote Desktop Services
Remote Desktop Services Formerly known as Terminal Services in Windows Server 2008, the Remote Desktop Services in Windows Server 2008 R2 is Microsoft... computer over a network with the help of Remote Desktop Protocol (RDP
ssl comunication between a desktop application and a web application
ssl comunication between a desktop application and a web application  Hi Lusiano, I am trying to implement a ssl comunication between a desktop application and a web application. can you please share code with me. Please
Remote Desktop Setting in Windows 8
the network by enabling the Remote Desktop Connection in your PC. 1. To configure 'Remote Desktop Connection' on your Windows 8 installed PC, first of all... running Remote Desktop with Network Level Authentication (recommended
The JDBC API to access a desktop database like Microsoft Access over the network.
The JDBC API to access a desktop database like Microsoft Access over the network.  How can I use the JDBC API to access a desktop database like Microsoft Access over the network
Java
Java Java is one the programming language suited for the construction of many type of applications. It can be used to develop: Desktop applications... applications Applications for the embedded devices The Java programming language
Foresight Desktop Linux 0.9.4 Pre 1 has been released now
Foresight Desktop Linux 0.9.4 Pre 1 has been released now I am please to announce another release of Foresight Desktop Linux. I am also quite proud to say we..... About the Foresight Desktop Linux Forsight Linux is a distribution based
Problem in setting IP address for a JFrame Class Desktop Application
Problem in setting IP address for a JFrame Class Desktop Application  I have designed a browser for our final year project, it is working well. But when we put it in a system which has a proxy setting for internet connection
Core Java Tutorial for Beginners
Desktop applications. Core java is all the basic classes and APIs that make... Edition) is a set of library for desktop PCs and Java EE (Enterprise Edition...Core Java tutorials for beginners help you to learn the language in an easy
User-friendly Desktop Internet GIS (UDIG)
User-friendly Desktop Internet GIS (UDIG)       The User-friendly Desktop Internet GIS (uDig) is both a GeoSpatial application and a platform through which
Java Update
for desktop Java applications. Java 1.4.2 Update 2 also includes all... Java Update       In this section, you will learn java updates.  Java Update 1.4.2 Java 1.4.2
FoX Desktop 1.0 Professional has been released
FoX Desktop 1.0 Professional has been released Desktop enhancements... Desktop FoX Linux is a single-CD desktop-oriented Linux distribution based on Fedora Core, with KDE as its preferred desktop, main components recompiled
i want to create an icon on my desktop... with this i want to open my web application
i want to create an icon on my desktop... with this i want to open my web application  i want to create an icon on my desktop... with this i want to open my web application(automatically run my server... )how
How to launch my web application from my desktop without opening Netbeans IDE everytime i run the application?
How to launch my web application from my desktop without opening Netbeans IDE... is i want to create a shortcut for my webapplication in desktop so... dont want all these steps, so i knw that java application can be launched using
Applications of Java
Applications of Java Java programming language is widely used to develop web applications, games and other applications for mobile. Java developers use Java... to maintain. Java provides tools to develop portable and high-performance
Cross-platform programming with Java technology and the IBM Web Services Toolkit for Mobile Devices
;   When you?re writing a Java application for the server or desktop, you can be reasonably sure that the Java platform...Cross-platform programming with Java technology and the IBM Web
Java Video Tutorial - What is Java?
of devices run Java including desktop, cell phones, TV devices. There are some interesting facts about Java. Around 1.1 billion desktop runs Java...Java is a object oriented programming language that allows the programmers
java
java  hi all i have a problem ,i want to sotre form data in table with foreign key in java ,please help me  Hi,Are you developing desktop or JSP application?Thanks
AWT basics
, graphics, and user-interface for the desktop application using Java technology
Java Remote Machines - Java Beginners
Java Remote Machines  How to get the Physical path of virtual Directories created in IIS using the Java Desktop Applications? Any one please help... Machine using Java Desktop Applications
java swing and CSS
java swing and CSS  can css be used in java swing desktop application in different forms for better styles?plzz help
single frame coding in java
single frame coding in java  sir, i am a beginner in java.i want to do a desktop application in java in which a single frame satisfies all needs.but... in a desktop application is quite boaring
Java lock file or folder.
Java lock file or folder.  Is it possible to create a desktop application to lock or encrypt file or folder in windows by using java code?? if possible can provide some reference?? because i can't find it... thanks
Java - Swing AWT
Java  Hello friends, I am developing an desktop application in java & I want to change the default java's symbol on the top & put my own symbol there.. Can Anyone help me
Applications of Java
Applications of Java  Hi, What are the Applications of Java in development of enterprise applications? Thanks   Hi, You can use java to develop various kinds of Java applications for: a) Desktop b) Websites c
java coding
java coding  i am using netbean to my project and it is Desktop Application.i want my textfield to accept only numbers or only alphabets .........plez plez plez do help me..........i am just beginner to java codes
java jtable
java jtable  Hello Sir, I am developing a desktop application in which i have to display database records in jtable .now I want to read only the values of particular row at which the jtextfield has been set. and display
Purefect
is a free Web Desktop (a.k.a WebOS). In order to use Purefect, you have to login to your web-based desktop first. Then you can choose and run applications the same way you run applications on your desktop. Read full Description
Java Technology
Java Technology  Hi, What is the use of Java technology? How I can develop a program for hotel booking? Thanks   Hi, Java Technology... develop: Websites Desktop applications Accounting applications Travel Portal
Core Java - Java Beginners
). They are used in all classes of Java programming, from desktop applications to Java EE...Core Java  What is Java? I am looking for Core Java Training  Hi friendThe Core Java Technologies and application programming interface
 

 

 

DMCA.com