Sunday, August 30, 2009

JDBC Project

This is an application for a Hospital website. It contains a form where the Patient's details can be entered and it will be stored in a database(Sql). However there r many errors in this project. I'm not able to execute this program in my computer coz I dont have SQL installed in my system. If u can solve this, please mail the edited code to vishwas.24@gmail.com

Here is the code:

import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class LifelineHospitals implements ActionListener
{
Label Heading,lpName,lpGender,lpAge,lpDOB,lpOccupation,lpGuardian,error;

TextField pnameField,paddressField,pageField,pgenderField,pdobField,pguardianField;

Button Submit,Exit;
Prepared Statement stat;
Statement stmt;
ResultSet rs;
Font F;
Panel P1;
Frame F1;

/*Constructor*/
public LifelineHospitals
{
try
{
//Type 4 driver
Class.forName("com.microsoft.Sqlserver.jdbc.SQLServerDriver");
//Connection to the Data Source

con=DriverManager.getConnection("jdbc:Sqlserver://Sqlserver1;databaseName=Library'user=user1;password=password#1234");

//Create a Statement object

stmt=con.createStatement();
}
Catch(Exception e)
{
System.out.println("Error:"+e);
}
}
public void Compshow()
{
F1=new Frame("LifelineHospitals");
P1=new Panel();
Heading=new Label("Patients Details");
lpName=new Label("Patient's name");
lpAge=new Label("Age");
lpDOB=new Label("DOb");
lpGender=new Label("Gender");
lpGuardian=new Label("Guardian");
lpOccupation=new Label("Occupation");

pnameField=new TextField(50);
pgenderField=new TextField(8);
pageField=new TextField(3);
pdobField=new TextField(10);
poccupationField=new TextField(20);
pguardianField=new TextField(25);

Submit=new Button("Submit");
Exit=new Button("exit");

P1.setLayout(null);
Heading.setBounds(250,35,200,40);
P1.add(Heading);
lpName.setBounds(75,90,200,25);
pnameField.setBounds(400,90,100,25);
P1.add(pName);
P1.add(pnameField);

lpGender.setBounds(75,120,200,30);
pgenderField.setBounds(400,150,150,25);
P1.add(lpGender);
P1.add(pgenderField);

lpAge.setBounds(75,150,200,30);
pageField.setBounds(400,150,150,25);
P1.add(lpAge);
P1.add(pageFild);

lpDOB.setBounds(75,180,200,30);
lpdobField.setBounds(400,180,250,25);
P1.add(lpDOB);
P1.add(pdobField);

lpOccupation.setBounds(75,210,200,30);
poccupationField.setBounds(400,210,200,25);
P1.add(lpGuardian);
P1.add(pguardianField);

Submit.setBounds(175,350,100,30);
Exit.setBounds(325,350,100,30);
P1.add(Submit);
P1.add(Exit);
F1.add(P1);
F1.setSize(680,500);
F1.setVisible(true);
Submit.addActionListerner(this);
Exit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Exit");
{
/*Terminate the windows form*/
System.exit(0);
}
if(ae.getActionCommand()=="Submit");
{
//Insert a row into the Publisher Table
try
{
stat=con.prepareStatement("INSERT INTO PUBLISHERS VALUES(?,?,?,?,?,?)");
string pName=pnameField.getText();
string pGender=pgenderField.getText();
string pAge=pageField.getText();
string pDOB=pdobField.getText();
string pOccupation=poccupationField.getText();
string pGuardian=pguardianField.getText();

stat.setString(1,pName);
stat.setString(2,pGender);
stat.setString(3,pAge);
stat.setString(4,pDOB);
stat.setString(5,pOccupation);
stat.setString(6,pGuardian);
stat.executeUpdate();
pnameField.setText("");
pgenderField.setText("");
pageFiels.setText("");
pdobField.setText("");
poccupationField.setText("");
pguardianField.setText("");
}
Catch(Exception e)
{
System.ouyt.println("Error:"+e);
error.setText("The statement cannot be inserted");
}
}
} /*public void class is closed*/

public static void main(string args[])
{
LifelineHospitals p=new LifelineHospitals;
p.Compshow();
}
}

Sunday, August 9, 2009

Comparing two numbers - java code

import java.io.*;
class Comparing{
public static void main(String[] args) {
int a=24, b=25;
if (a == b){
System.out.println("Both are equal");
}
else if(a>b){
System.out.println("a is greater than b");
}
else{
System.out.println("b is greater than a");
}
}
}

To find the largest number - java code

import java.io.*;
class largernumber
{
public static void main(String[] args) {
int x=800, y=7000, z=80000;
if (x>y){
if (x>z){
System.out.println("x is greater");
}
else{
if(z>y){
System.out.println("z is greater");
}
else{
System.out.println("y is greater");
}
}
}
else{
if (y>z){
System.out.println("y is greater");
}
}
}
}

Friday, August 7, 2009

Leap year - java code

import java.io.*;
class Leapyear
{
public static void main(String[] args) throws IOException
{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the year");
int year=Integer.parseInt(object.readLine());
System.out.println("The year entered is:"+year);
if (year%4==0){
System.out.println("The given year is a leap year");
}
else{
System.out.println("This is not a leap year");
}
}
}

Thursday, August 6, 2009

Palindrome number

//To check if the number is a Palindrome
import java.io.*;

public class Palindrome {
public static void main(String [] args){
try{
BufferedReader object = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int rev=0;
System.out.println("Number: ");
System.out.println(" "+ num);
for (int i=0; i<=num; i++){
int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
System.out.println("After reversing the number: "+ " ");
System.out.println(" "+ rev);
if(n == rev){
System.out.print("Number is palindrome!");
}
else{
System.out.println("Number is not palindrome!");
}
}
catch(Exception e){
System.out.println("Out of range!");
}
}
}

Tuesday, August 4, 2009

Simple Programs - Java Code

//FACTORIAL OF A NUMBER:
import java.io.*;
class Factorial1
{
public static void main(String[] args)
{
try
{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
int a= Integer.parseInt(object.readLine());
double fact= 1;
System.out.println("Factorial of " +a+ ":");
for (int i= 1; i<=a; i++)
{
fact=fact*i;
}
System.out.println(fact);
}
catch (Exception e)
{
System.out.println("Array out of bounds exaception");
}
}
}
//PALINDROME NUMBER:
import java.io.*;
public class Palindrome
{
public static void main(String [] args)
{
try
{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number");
int num= Integer.parseInt(object.readLine());
int n = num;
int rev=0;
System.out.println("Number: ");
System.out.println(" "+ num)
for (int i=0; i<=num; i++)
{
int r=num%10;
num=num/10;
rev=rev*10+r;
i=0;
}
System.out.println("After reversing the number: "+ " "); System.out.println(" "+ rev);
if(n == rev)
{
System.out.print("Number is palindrome!");
}
else
{
System.out.println("Number is not palindrome!");
}
}
catch(Exception e)
{
System.out.println("Out of range!");
}
}
}
//AREA OF A RECTANGLE:
import java.io.*;
class RecArea
{
public static void main(String[] args)
{
int l=0; int w=0;
try
{
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter length of rectangle : ");
l = Integer.parseInt(br1.readLine());
System.out.println("Enter width of rectangle : ");
w = Integer.parseInt(br1.readLine());
int area = l*w;
System.out.println("Area of Rectangle : "+area);
int perimiter = 2*(l+w);
System.out.println("Perimeter: " + perimiter);
}
catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
//PRINT A TRIANGLE:
import java.io.*;
class triangle
{
public static void main(String[] args)
{
try
{
BufferedReader object = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the number");
int a= Integer.parseInt(object.readLine());
for (int i=1; i{
for (int j=1; j<=i;j++ )
{
System.out.print("*");
}
System.out.println("");
}
}
catch(Exception e)
{}
}
}

Monday, August 3, 2009

To find the area and perimeter of the circle - Java Code

//AREA OF THE CIRCLE:
import java.io.*;
class CircleArea
{
public static void main(String[] args)
{
int r=0;
try
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br1 = new BufferedReader(ir);
System.out.println("Enter Radius of Circle : ");
r = Integer.parseInt(br1.readLine());
double area = java.lang.Math.PI*r*r;
System.out.println("Area of Circle : "+area);
double perimeter =2*java.lang.Math.PI*r ;
System.out.println("Perimeter of Circle : "+perimeter);
}
catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}