-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatient.java
88 lines (81 loc) · 2.73 KB
/
patient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package JDBC.HospitalManagment;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class patient {
private Connection con;
private Scanner sc;
public patient(Connection con, Scanner sc) {
this.con = con;
this.sc = sc;
}
// methods
public void AddPatient() {
sc.nextLine();
System.out.print("Enter Patient Name: ");
String name = sc.nextLine();
System.out.print("Enter Patient Age: ");
int age = sc.nextInt();
System.out.print("Enter Patient Gender: ");
String gender = sc.next();
System.out.println();
try {
String query = "INSERT INTO patients(name,age,gender) VALUES(?,?,?);";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, gender);
int rowinserted = ps.executeUpdate();
if (rowinserted > 0) {
System.out.println("Patient Added Successfully...");
} else {
System.out.println("Failed to Add Patient..");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void ViewPatients()
{
String query="SELECT * FROM patients;";
try{
PreparedStatement ps=con.prepareStatement(query);
ResultSet rs=ps.executeQuery();
System.out.println("Patients Details...");
System.out.println("+-----------+----------------------------+---------+----------------+");
System.out.println("| PatientId | Name | Age | Gender |");
System.out.println("+-----------+----------------------------+---------+----------------+");
while(rs.next())
{
int id=rs.getInt("id");
String name=rs.getString("name");
int age=rs.getInt("age");
String gender=rs.getString("gender");
System.out.printf("|%-11s|%-28s|%-9s|%-16s|\n",id,name,age,gender);
System.out.println("+-----------+----------------------------+---------+----------------+");
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public boolean getPatientbyid(int id)
{
String query="SELECT * FROM patients WHERE ID=? ; ";
try {
PreparedStatement ps=con.prepareStatement(query);
ps.setInt(1, id);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
return true;
}else{
return false;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
return false;
}
}