Write a Java program to accept the details of patient(pno, pname) from user and insert it into the database, display it and delete the record of entered pno from the table
Write a Java program to accept the details of patient(pno, pname) from user and insert it into the database, display it and delete the record of entered pno from the table
Program:
import java.io.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class Patient extends Frame implements ActionListener {
Label labelPNo, labelPName;
TextField textPNo, textPName;
Button btnAdd, btnDelete;
String sql;
Statement stmt;
Connection con;
public Patient() throws SQLException, ClassNotFoundException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:patientDSN");
setLayout(new FlowLayout());
setSize(300,200);
labelPNo = new Label("Patient No:");
labelPName = new Label("Patient Name:");
textPNo = new TextField(20);
textPName = new TextField(20);
btnAdd = new Button("Add");
btnDelete = new Button("Delete");
btnAdd.addActionListener(this);
btnDelete.addActionListener(this);
add(labelPNo);
add(textPNo);
add(labelPName);
add(textPName);
add(btnAdd);
add(btnDelete);
addWindowListener(new WClose());
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
try {
if(ae.getSource == btnAdd){
int pno;
String pname;
pno = Integer.parseInt(textPNo.getText());
pname = textPName.getText();
sql = "insert into patient values("+pno+",'"+pname+"')";
stmt = con.createStatement();
int k = s.executeUpdate(sql);
if(k>0) {
System.out,println("Record Saved.");
}
stmt.close();
}
if(ae.getSource() == btnDelete){
int pno;
pno = Integer.parseInt(textPNo.getText());
sql = "delete from patient where pno="+pno;
stmt = con.createStatement();
int k = s.executeUpdate(sql);
if(k>0) {
System.out,println("Record Deleted.");
}
stmt.close();
}
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]){
new Patient();
}
}
class WClose extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
Comments
Post a Comment