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();
}
}

No comments:

Post a Comment