Sunday 25 August 2013

Simple Chat Application

                                           SIMPLE CHAT APPLICATION
1.Download this file compile and run and keep the command prompt open.....
This is the server code....

import java.io.*;
import java.net.*;
class VikramServer
{
    public static void main (String args[]) throws Exception
    {
        try
        {
            String CStr="";
            String UStr="";
            System.out.println("********************* Server**********************");
            ServerSocket SS = new ServerSocket(8888);
            Socket OS = SS.accept();
            DataInputStream Cinp =new DataInputStream(OS.getInputStream());
            DataInputStream Uinp =new DataInputStream(System.in);
            PrintStream PS = new PrintStream(OS.getOutputStream());
            while(true)
                {
                     
                    CStr=Cinp.readLine();
                    System.out.println("Client : " +CStr);
                    if(CStr.equals("exit"))
                          break;  
                      System.out.print("Server : ");  
                      UStr=Uinp.readLine();    
                      PS.println(UStr);
                 
                }
          }catch(Exception e){System.err.println(e);};
             
    }
}

2.Download this file compile and run and keep the command prompt open.....
This is the Client code ....

import java.io.*;
import java.net.*;
class VikramClient
{
    public static void main(String args[]) throws Exception
    {
        try
        {
        String Schat="";
        String Sstr="";
        Socket SC = new Socket("localhost",8888);
        DataInputStream Uinp =new DataInputStream(System.in);
        DataInputStream Sinp =new DataInputStream(SC.getInputStream());
        PrintStream PS = new PrintStream(SC.getOutputStream());
        System.out.println("********************* Client**********************");
        while(true)
            {
                    System.out.print("Client : ");
                    Schat=Uinp.readLine();
                    if(Schat.equals("exit"))
                          break;
                      PS.println(Schat);  
                      Sstr=Sinp.readLine();
                      System.out.println("Server : "+Sstr);
            }
        }catch(Exception e){System.err.println(e);};  
    }
}

Thursday 11 July 2013

Predict the Output !!!!!

Public class Vikram
{
public static void main(String[] args)
{
http://vikram-programmingwithjava.blogspot.in/
System.out.println("Why is the URL allowed above?");
}
}
Will the Program Compile ???
If Yes what would be the Output and will http://vikram-programmingwithjava.blogspot.in/ be displayed in screen ?
If No what is the error in the program ???
Post your answers below.........

Thursday 28 March 2013

Program to find maximum and minimum number for a n*n matrix

import java.io.*;
 class test
 {
 public static void main(String[] asd)throws IOException
 {
 BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Enter the values of Rows and Columns of Matrix.");
 String num=obj.readLine();
 int r=Integer.parseInt(num);
 num=obj.readLine();
 int c=Integer.parseInt(num);
 int m[][]= new int[r][c];
 int max=0, min=0;
 System.out.println("Enter the values of the matrix not more then "+(r*c)+".");
 for(int i=0;i<r;i++)
 for(int j=0;j<c;j++)
 {
 num=obj.readLine();
 m[i][j]=Integer.parseInt(num);
 }
 System.out.println("Original matrix::");
 for(int i=0;i<r;i++)
 {
 for(int j=0;j<c;j++)
 {
 System.out.print(m[i][j]+" ");
 }
 System.out.println();
 }
 // MAXIMUM element of the matrix max=m[0][0];
 for(int i=0;i<r;i++)
 {
 for(int j=0;j<c;j++)
 {
 if(m[i][j]>max) max=m[i][j];
 }
 }
System.out.println("Maximum of the matrix is "+max+".");
//MINIMUM element of the matrix min=m[0][0];
 for(int i=0;i<r;i++)
 {
 for(int j=0;j<c;j++)
 {
 if(m[i][j]<min) min=m[i][j];
 }
 }
 System.out.println("Minimum of the matrix is "+min+".");
 }
 }

Wednesday 27 March 2013

Four different ways to create an object in java


1. Using new keyword
This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.
MyObject object = new MyObject();
2. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
3. Using clone()
The clone() can be used to create a copy of an existing object.
MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();
4. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();

Tuesday 19 March 2013

How To Create A War File In Netbeans




  1. Right click on your project name in the Explorer
  2. Select Properties
  3. In the Build | Packaging section, check "Compress War file" option.
  4. Select file/folder and select your project then u can see war file in dist folder


Friday 1 February 2013

Login with Swings


Create a Login form and Index form with netbeans ide and inside Login form you can put two labels Name and password with a jTextField1 and jPasswordField1 and two buttons Submit and Cancel

Login
Submit
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
        String s=jTextField1.getText();
        String s1=jPasswordField1.getText();
        Index i=new Index();              
        if(s.equals("Vikram")&&s1.equals("vikram"))
        {
            i.setVisible(true);
        }
        else if(!"Vicky".equals(s))
        {
            JOptionPane.showMessageDialog(this,"User Name Is Incorrect");
        }
        else
        {
           
            JOptionPane.showMessageDialog(this,"Password Is Incorrect");
        }
}
Cancel
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) 
{
    System.exit(0);
}
Index
Date can be set to the textfield using the below code :
  
  private void formWindowActivated(java.awt.event.WindowEvent evt) 
{
        Date d=new Date();
        SimpleDateFormat da=new SimpleDateFormat("dd/MM/yyyy");
        jTextField1.setText(da.format(d));
        SimpleDateFormat vi=new SimpleDateFormat("hh:mm:ss");
        jTextField2.setText(vi.format(d));
}


Jsp connection with mysql database


Create a folder named user inside your web server (Tomcat) C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps and inside the user create WEB-INF and META-INF so that you can create a file inside META-INF  named Content.xml  and you need to create a empty folder WEB-INF  and you can create and place the file test.jsp inside C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\user

Content.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/user"/>

test.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>

<html>
<head>
<title>Connection with mysql database</title>
</head>
<body>
<h1>Connection status </h1>

<%
String connectionURL = "jdbc:mysql://localhost:3306/usermaster";
Connection connection = null;
ResultSet rst=null;
Statement st=null;

try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "a");
st = connection.createStatement();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
if(request.getParameter("action") != null)
{
String un=request.getParameter("un");
String pw=request.getParameter("pwd");
rst=st.executeQuery("select * from login");
%>

<h2>Login/h2>
<table border="1" cellspacing="0" cellpadding="0">
<tr>

<td><b>User name</b></td>
<td><b>Password</b></td>
</tr>
<%

while(rst.next()){
%>
<tr>

 <td><%=rst.getString("un")%></td>
 <td> <%=rst.getString("pwd")%></td>
</tr>

<%

rst.close();
st.close();
connection.close();

%>
</table>
</body>
</html>
Please provide feedback readers.
Thank you
Vikram.S