Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Java Internal

This document describes a login application in Java, including code for a database connection class, a login form class, and a welcome class. The login form class gets username and password from text fields, queries the database to validate the credentials, and opens a welcome window on successful login. The database connection class establishes a connection to a MySQL database.

Uploaded by

Naman jain
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Java Internal

This document describes a login application in Java, including code for a database connection class, a login form class, and a welcome class. The login form class gets username and password from text fields, queries the database to validate the credentials, and opens a welcome window on successful login. The database connection class establishes a connection to a MySQL database.

Uploaded by

Naman jain
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Hello client

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class HelloClient


{
public static void main(String[] args) throws UnknownHostException,
IOException
{

Socket s=new Socket("localhost",777);


InputStream is=s.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String msgFromServer=br.readLine();
System.out.println("Message from server is:"+msgFromServer);

br.close();
is.close();
s.close();
}

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class HelloServer


{
public static void main(String[] args) throws IOException
{

ServerSocket ss=new ServerSocket(777);


System.out.println("Server is waiting for the client");
Socket s=ss.accept();
System.out.println("Server is connected to the client");
OutputStream os=s.getOutputStream();
PrintStream ps=new PrintStream(os);
String msg="Hello from server";
ps.println(msg);

ps.close();
s.close();
ss.close();

}
------------------------------------------------------------------------------

LOgin application

DBConnection

import java.sql.*;
public class DBConnection{
public static Connection getDatabaseConnection(){
String username="root";
String password="root";
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/mydatabase";
Connection con=null;
try
{
//Step 1: Load and Register Driver class
Class.forName(driver);
try
{
con=DriverManager.getConnection(url,username,password);
}
catch(SQLException e)
{

}
}
catch(ClassNotFoundException e)
{

}
return con;
}
}

Login form

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginPage extends JFrame implements ActionListener


{
Container c;
JLabel lblUserName,lblPasswd;
JTextField txtUserName;
JPasswordField txtPasswd;
JButton btnLogin,btnCancel;

public LoginPage()
{
// TODO Auto-generated constructor stub
c=getContentPane();
c.setLayout(new FlowLayout());

lblUserName=new JLabel("User Name");


txtUserName=new JTextField(10);

lblPasswd=new JLabel("Password");
txtPasswd=new JPasswordField(10);

btnLogin=new JButton("Login");
btnCancel=new JButton("Cancel");

c.add(lblUserName);
c.add(txtUserName);
c.add(lblPasswd);
c.add(txtPasswd);
c.add(btnLogin);
c.add(btnCancel);

btnLogin.addActionListener(this);
}

public static void main(String[] args) {


LoginPage p=new LoginPage();
p.setTitle("Login Page");
p.setSize(600, 600);
p.setVisible(true);
p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
if(ae.getSource()==btnLogin) {
String un=txtUserName.getText();
String pwd=String.valueOf(txtPasswd.getPassword());

String sql=
"Select username,password from tbluser where username='"+un+"' and
password='"+pwd+"'";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;

con=DBConnection.getDatabaseConnection();
try
{
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next()) {
Welcome w=new Welcome();
w.setTitle("Welcome Page");
w.setVisible(true);
w.setSize(400, 400);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(this, "Password
Match");
}
else
{
JOptionPane.showMessageDialog(this, "Please check
username and password");
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

You might also like