0% found this document useful (0 votes)
16 views4 pages

Java Internal

Download as txt, pdf, or txt
Download as txt, pdf, or txt
Download as txt, pdf, or txt
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