Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts
Sunday, June 1, 2014
Sunday, May 25, 2014
this keyword in Java

Here is given the usage of this keyword.
From - www.DreamsCoder.com
Sunday, April 20, 2014
Socket Programming with File Handling
/*Client Socket program that accepts file name from Client and send it to Server, Server Side Socket reads the Content of a file and display them onto the Client Socket*/
/*Server Program (severprac.java)*/
import java.io.*;
import java.net.*;
public class serverprac
{
public static void main(String[] args) {
ServerSocket ss;
Socket client;
String nm;
try
{
ss = new ServerSocket(1212);
System.out.println("Server is Waitng For Client");
client = ss.accept();
System.out.println("Client is Connected");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1 = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintStream ps = new PrintStream(client.getOutputStream());
String filenm = br1.readLine();
File f = new File(".");
File[] flist = f.listFiles();
for(File n:flist)
{
nm = n.getName();
if(nm.equals(filenm))
{
System.out.println("File is Present");
FileReader fr = new FileReader(nm);
BufferedReader bfr = new BufferedReader(fr);
String lne;
while(true)
{
lne = bfr.readLine();
if (lne == null)
break;
//System.out.println(lne);
ps.println(lne);
}
}
}
ps.close();
br1.close();
br.close();
ss.close();
}//try
catch(Exception e)
{
System.out.println(e);
}
}//main
}
/*Client Program (clientprac.java)*/
import java.io.*;import java.net.*;
public class clientprac
{
public static void main(String[] args)
{
Socket s;
try
{
s = new Socket("127.0.0.1",1212);
System.out.println("Clent is Conneccted to the Server");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream ps = new PrintStream(s.getOutputStream());
System.out.println("Enter File name");
ps.println(br.readLine());
while(br1.readLine() !=null)
System.out.println(br1.readLine());
br1.close();
ps.close();
br.close();
br1.close();
s.close();
}
catch(Exception e)
{
System.out.println(""+e);
}//catch
}//main
}
Saturday, March 29, 2014
Inter - Thread communication using wait(), notify() and notify all() method in Java
/*
Write a program to illustrate inter - thread communication using wait(), notify() and notify all() method
*/
import java.io.*;
class q
{
String msg;
boolean flag;
q()
{
flag=false;
}
synchronized void get()
{
if(flag==false)
{
try
{
wait();
}//try
catch(InterruptedException e)
{
}//catch
}
flag=false;
System.out.println("Received Message"+msg);
notify();
}
synchronized void put(String x)
{
if(flag==true)
{
try
{
wait();
}
catch(InterruptedException e)
{
}//catch
}
msg = x;
flag = true;
if(msg.equalsIgnoreCase("Good Bye"))
System.exit(0);
notify();
}
}
class sender extends Thread{
q q1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
sender(q x)
{
q1 = x;
start();
}
public void run()
{
while(true)
{
System.out.println("Enter Message ");
try
{
s = br.readLine();
q1.put(s);
}
catch(IOException e)
{
}//catch
}//while
}//run
}
class receiver extends Thread
{
q q2;
receiver(q x)
{
q2 = x;
start();
}
public void run()
{
while(true)
{
q2.get();
}
}
}
class fourteen
{
public static void main(String args[])
{
q q4 = new q();
sender s = new sender(q4);
receiver r = new receiver(q4);
}
}
Write a program to illustrate inter - thread communication using wait(), notify() and notify all() method
*/
import java.io.*;
class q
{
String msg;
boolean flag;
q()
{
flag=false;
}
synchronized void get()
{
if(flag==false)
{
try
{
wait();
}//try
catch(InterruptedException e)
{
}//catch
}
flag=false;
System.out.println("Received Message"+msg);
notify();
}
synchronized void put(String x)
{
if(flag==true)
{
try
{
wait();
}
catch(InterruptedException e)
{
}//catch
}
msg = x;
flag = true;
if(msg.equalsIgnoreCase("Good Bye"))
System.exit(0);
notify();
}
}
class sender extends Thread{
q q1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
sender(q x)
{
q1 = x;
start();
}
public void run()
{
while(true)
{
System.out.println("Enter Message ");
try
{
s = br.readLine();
q1.put(s);
}
catch(IOException e)
{
}//catch
}//while
}//run
}
class receiver extends Thread
{
q q2;
receiver(q x)
{
q2 = x;
start();
}
public void run()
{
while(true)
{
q2.get();
}
}
}
class fourteen
{
public static void main(String args[])
{
q q4 = new q();
sender s = new sender(q4);
receiver r = new receiver(q4);
}
}
User Defined Exception Example in Java
/*
Write a class Student with attributes roll no, name, age and course. Initialize values through parameterized constructor. If age of student is not in between 15 and 21 then generate user-defined exception “Age Not Within The Range”.If name contains numbers or special symbols raise exception “Name not valid”
*/
import java.io.*;
class AgeNotWithinRangeException extends Exception
{
AgeNotWithinRangeException()
{
super();
}
}
class NameNotValidException extends Exception
{
NameNotValidException()
{
super();
}
}
class Student
{
private
int roll,age;
String name,course;
public
Student(int r,String nm,int ag,String co)
{
roll=r;
name=nm;
age=ag;
course=co;
System.out.println("Roll Number is "+roll);
System.out.println("Name is "+name);;
System.out.println("Age is "+age);
System.out.println("Course is "+course);
}
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int roll,age;
String name,course;
System.out.println("Enter No. :");
roll = Integer.parseInt(br.readLine());
System.out.println("Enter Name :");
name = br.readLine();
try
{
for(int i=0;i<name.length();i++)
{
char c = name.charAt(i);
if((c<65 || c>90) && (c<97 || c>122) && (c!=32))
{
throw new NameNotValidException();
}//if
}//for
}//try
catch(NameNotValidException n)
{
System.out.println("Invalid Name"+n);
}
System.out.println("Enter Age :");
age = Integer.parseInt(br.readLine());
try
{
if(age<15 || age>21)
{
throw new AgeNotWithinRangeException();
}
}//try
catch(AgeNotWithinRangeException e)
{
System.out.println("Invalid Age"+e);
}
System.out.println("Enter Course :");
course = br.readLine();
Student s = new Student(roll,name,age,course);
}
}
Write a class Student with attributes roll no, name, age and course. Initialize values through parameterized constructor. If age of student is not in between 15 and 21 then generate user-defined exception “Age Not Within The Range”.If name contains numbers or special symbols raise exception “Name not valid”
*/
import java.io.*;
class AgeNotWithinRangeException extends Exception
{
AgeNotWithinRangeException()
{
super();
}
}
class NameNotValidException extends Exception
{
NameNotValidException()
{
super();
}
}
class Student
{
private
int roll,age;
String name,course;
public
Student(int r,String nm,int ag,String co)
{
roll=r;
name=nm;
age=ag;
course=co;
System.out.println("Roll Number is "+roll);
System.out.println("Name is "+name);;
System.out.println("Age is "+age);
System.out.println("Course is "+course);
}
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int roll,age;
String name,course;
System.out.println("Enter No. :");
roll = Integer.parseInt(br.readLine());
System.out.println("Enter Name :");
name = br.readLine();
try
{
for(int i=0;i<name.length();i++)
{
char c = name.charAt(i);
if((c<65 || c>90) && (c<97 || c>122) && (c!=32))
{
throw new NameNotValidException();
}//if
}//for
}//try
catch(NameNotValidException n)
{
System.out.println("Invalid Name"+n);
}
System.out.println("Enter Age :");
age = Integer.parseInt(br.readLine());
try
{
if(age<15 || age>21)
{
throw new AgeNotWithinRangeException();
}
}//try
catch(AgeNotWithinRangeException e)
{
System.out.println("Invalid Age"+e);
}
System.out.println("Enter Course :");
course = br.readLine();
Student s = new Student(roll,name,age,course);
}
}
Thursday, July 26, 2012
Hash table demo (JAVA)
import java.util.*;
import java.io.*;
class demo_hashtable
{ public static void main(String args[]) throws IOException
{ BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
int opt;
String name;
double per , maxp;
Enumeration n;
Hashtable HT = new Hashtable();
do
{ System.out.println("\n1. Add student ");
System.out.println("2. Display students ");
System.out.println("3. Search student ");
System.out.println("4. Find Highest percentage ");
System.out.println("5. Exit ");
System.out.print("Enter your option : ");
opt = Integer.parseInt(br.readLine());
switch(opt)
{ case 1: System.out.print("Enter Name : ");
name = br.readLine();
System.out.print("Enter percentage : ");
per = Double.parseDouble(br.readLine());
HT.put(name , per);
break;
case 2: System.out.println(HT);
break;
case 3: System.out.print("Enter Name : ");
name = br.readLine();
if(HT.containsKey(name))
System.out.println("Percentage : " + HT.get(name));
else
System.out.println(name + " not present in hashtable");
break;
case 4: n = HT.keys();
per = maxp =0.0;
name = "";
while(n.hasMoreElements())
{ String str =(String)n.nextElement();
per = (Double) HT.get(str);
if( per > maxp)
{ maxp = per;
name = str;
}
}
System.out.println("Student : " + name +" Highest Percentage : "+maxp);
break;
case 5 : System.exit(0);
default : System.out.println("Wrong Option");
}//switch
}while(opt != 5);
}
}
Saturday, July 7, 2012
Greet user in JSP (JAVA)
<html>
<body>
<%! String nm;%>
<% nm = request.getParameter("t1"); %>
<h1>HELLO , <%=nm%>
</body>
</html>
Friday, June 1, 2012
Simple Mathematical Operations Program In java
class mathopr
{ public static void main(String arg[])
{ int a,b,c;
a = Integer.parseInt(arg[0]);
b = Integer.parseInt(arg[1]);
c = a+b;
System.out.println("Addition is " +c);
c = a-b;
System.out.println("Substraction is " +c);
c = a*b;
System.out.println("Multiplication is " +c);
c = a/b;
System.out.println("Division is " +c);
c = a%b;
System.out.println("Modulus is " +c);
}
}
Saturday, May 26, 2012
Creating a Table and Inserting values in table (JAVA)
import java.sql.*;
import java.io.*;
class CreateTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:coll");
Statement stmt=conn.createStatement();
stmt.executeUpdate("create table student1(rno INTEGER primary key,name VARCHAR,per FLOAT)");
System.out.println("Table created");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the roll number");
int r = Integer.parseInt(br.readLine());
System.out.println("Enter name");
String name = br.readLine();
System.out.println("Enter per ");
double per=Double.parseDouble(br.readLine());
stmt.executeUpdate("insert into student1 values("+r+",' "+name+" ',"+per+")");
System.out.println("Record Inserted..");
stmt.close();
conn.close();
}
catch(Exception e){System.out.println(e.toString());}
}
}
Wednesday, May 23, 2012
Factorial (JAVA)
class Factorial
{
public static void main(String arg[])
{
if (arg.length!=1)
System.out.print("Invalid");
else
{
int n=Integer.parseInt(arg[0]);
int fact=1;
while(n>0)
{
fact=fact * n;
n--;
}
System.out.print("Factorial="+fact);
}
}
}
Sunday, May 20, 2012
Accepting Input from user and Retrieving data from database
/*index.jsp*/
<HTML>
<HEAD>
<TITLE>Database Lookup</TITLE>
</HEAD>
<BODY>
<H1>Database Lookup</H1>
<FORM ACTION="basic.jsp" METHOD="POST">
Please enter the ID of the publisher you want to find:
<BR>
<INPUT TYPE="TEXT" NAME="id">
<BR>
<INPUT TYPE="SUBMIT" value="Submit">
</FORM>
</BODY>
<HTML>
/*basic.jsp*/
<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>
<HTML>
<HEAD>
<TITLE>Fetching Data From a Database</TITLE>
</HEAD>
<BODY>
<H1>Fetching Data From a Database</H1>
<%
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "YourName", "password");
Statement statement = connection.createStatement();
String id = request.getParameter("id");
ResultSet resultset =
statement.executeQuery("select * from Publishers where pub_id = '" + id + "'") ;
if(!resultset.next()) {
out.println("Sorry, could not find that publisher. ");
} else {
%>
<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>Name</TH>
<TH>City</TH>
<TH>State</TH>
<TH>Country</TH>
</TR>
<TR>
<TD> <%= resultset.getString(1) %> </TD>
<TD> <%= resultset.getString(2) %> </TD>
<TD> <%= resultset.getString(3) %> </TD>
<TD> <%= resultset.getString(4) %> </TD>
<TD> <%= resultset.getString(5) %> </TD>
</TR>
</TABLE>
<BR>
<%
}
%>
</BODY>
</HTML>
Fetching whole data from the Database Using JSP (JAVA)
<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>
<HTML>
<HEAD>
<TITLE>The Publishers Database Table </TITLE>
</HEAD>
<BODY>
<H1>The Publishers Database Table </H1>
<%
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "userName", "password");
Statement statement = connection.createStatement() ;
ResultSet resultset = statement.executeQuery("select * from Publishers") ;
%>
<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>Name</TH>
<TH>City</TH>
<TH>State</TH>
<TH>Country</TH>
</TR>
<% while(resultset.next()){ %>
<TR>
<TD> <%= resultset.getString(1) %></td>
<TD> <%= resultset.getString(2) %></TD>
<TD> <%= resultset.getString(3) %></TD>
<TD> <%= resultset.getString(4) %></TD>
<TD> <%= resultset.getString(5) %></TD>
</TR>
<% } %>
</TABLE>
</BODY>
</HTML>
Accesing Database Table using JSP (JAVA)
<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ; %>
<HTML>
<HEAD>
<TITLE>Accessing the Publishers Database Table</TITLE>
</HEAD>
<BODY>
<H1>Accessing the Publishers Database Table</H1>
<%
Connection connection = DriverManager.getConnection(
"jdbc:odbc:data", "UserName", "password");
Statement statement = connection.createStatement() ;
ResultSet resultset = statement.executeQuery("select pub_name from Publishers") ;
%>
<TABLE BORDER="1">
<TR>
<TH>Name</TH>
</TR>
<% while(resultset.next()){ %>
<TR>
<TD>
<%= resultset.getString(1)%>
</TD>
</TR>
<% } %>
</TABLE>
</BODY>
</HTML>
Saturday, May 19, 2012
Creating Frame using SWING( JAVA)
/* EXTENDING THE FRAME CLASS */
import javax.swing.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener
{
JButton btnExit;
public MyFrame()
{
super("My Third Window...");
btnExit = new JButton("Exit");
add(btnExit);
btnExit.addActionListener(this);
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
}
public class SwingFrame
{
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Friday, May 18, 2012
Array Factorial (JAVA)
class arrayfact
{
public static void main(String arg[])
{ int a[] = new int[arg.length];
int i, f , n;
for(i = 0 ; i < arg.length ; i++)
a[i] = Integer.parseInt(arg[i]);
for(i = 0 ; i < arg.length ; i++)
{ n = a[i];
for(f = 1 ; n > 1 ; n--)
f = f*n;
System.out.println("No : "+a[i]+" Factorial is " +f);
}
}
}
Simple Arithematic Operations in JAVA
import java.io.*;
class arith
{ public static void main(String arg[]) throws IOException
{ int a,b,c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(br.readLine());
b = Integer.parseInt(br.readLine());
c = a+b;
System.out.println("Addition is " +c);
c = a-b;
System.out.println("Substraction is " +c);
c = a*b;
System.out.println("Multiplication is " +c);
c = a/b;
System.out.println("Division is " +c);
c = a%b;
System.out.println("Modulus is " +c);
}
}
Armstrong number (JAVA)
class armstrong
{ public static void main(String args[])
{ int n , n1, rem , sum;
n = Integer.parseInt(args[0]);
n1 = n;
for(sum = 0 ; n > 0 ; n = n/10)
{ rem = n %10;
sum = sum + (rem*rem*rem);
}
if(n1 == sum)
System.out.println(n1 + " is Armstrong");
else
System.out.println(n1 + " is not Armstrong");
}
}
Thursday, May 17, 2012
A simple Login Screen in java with database Connectivty and Validation
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class loginframe extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4;
JTextField t1,t2;
JButton b1,b3;
Connection cn;
PreparedStatement pst;
ResultSet rs;
Panel p1,p2;
Container cp;
public loginframe()
{
super("Login form");
cp=getContentPane();
l1=new JLabel("Enter username");
l1.setForeground(Color.blue);
l2=new JLabel("Enter password");
l2.setForeground(Color.blue);
l3=new JLabel(" ");
t1=new JTextField(20);
t1.setFont(new Font("Comic Sans MS",Font.BOLD,15));
t2=new JTextField(20);
t2.setFont(new Font("Comic Sans MS",Font.BOLD,15));
t2=new JPasswordField(10);
p1=new Panel();
p1.setLayout(new GridLayout(3,2));
p1.add(l1);p1.add(t1);
p1.add(l2);p1.add(t2);
p1.add(l3);
b1=new JButton("Login");
b1.addActionListener(this);
b1.setForeground(Color.green);
b1.setBackground(Color.black);
b1.setFont(new Font ("Comic Sans MS",Font.BOLD ,20));
b3=new JButton("clear");
b3.addActionListener(this);
b3.setForeground(Color.green);
b3.setBackground(Color.black);
b3.setFont(new Font ("Comic Sans MS",Font.BOLD ,20));
p2=new Panel();
p2.setLayout(new FlowLayout());
p2.add(b1);p2.add(b3);
//setFont(new Font("Dialog",Font.BOLD,20));
cp.add(p1,BorderLayout.NORTH);
cp.add(p2,BorderLayout.SOUTH);
setSize(300,300);
cp.setBackground(Color.pink);
setVisible(true);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver is loaded");
cn=DriverManager.getConnection("jdbc:odbc:proj");
System.out.println("Connection established");
pst=cn.prepareStatement("select * from logintab where username=?");
}
catch(Exception e)
{
System.out.println("Exception 1");
}
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b3)
{
t1.setText(" ");
t2.setText("");
l3.setText(" ");
}
else
{
try
{
pst.setString(1,t1.getText());
rs=pst.executeQuery();
if(rs.next())
{
if(t2.getText().equals( rs.getString(2)))
{
new menu();
}
else
l3.setText("Wrong password");
}
else
l3.setText("Wrong Username");
rs.close();
}//try
catch(SQLException e)
{
System.out.println(" sql exception ");
}
}//else
}//action
protected void finalize()
{
try
{
pst.close();
cn.close();
}
catch(Exception e)
{
System.out.println("exception 3");
}
}
public static void main(String arg[])
{
new loginframe();
loginframe l=new loginframe();
l.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{ System.exit(0); }});
}
}
Simple login screen in JAVA
import java.awt.*;
import java.awt.event.*;
class login1 extends Frame implements ActionListener
{ Button bexit , bok;
TextField tuser , tpass;
Label L1 , L2;
login1()
{ super("Login Screen");
setLayout(new GridLayout(3,2));
tuser = new TextField(10);
tpass = new TextField(10);
tpass.setEchoCharacter('*');
L1 = new Label("Enter Login ");
L2 = new Label("Enter Password ");
bok = new Button("OK");
bok.addActionListener(this);
bexit = new Button("Exit");
bexit.addActionListener(this);
add(L1);add(tuser);
add(L2); add(tpass);
add(bok); add(bexit);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{ if(ae.getSource() == bexit)
System.exit(0);
}
public static void main(String arg[])
{ new login1();
}
}
Subscribe to:
Posts (Atom)