FIRST WE HAVE TO CREATE A DATABASE : LIBRARY
To create Publisher, we have to click on Publisher Button then
Click on Publisher Console. Now a new frame is appear.
All the details can filled by the user.
Click on Save Button. Publisher will be created.
TYPE CODE:
private void cmdNewActionPerformed(java.awt.event.ActionEvent evt) {
// Activate the Save button when New button clicked
cmdSave.setEnabled(true);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","
123456");
Statement stmt = null;
ResultSet rs = null; // ResultSet for publisher table.
String SQL = "SELECT * FROM publisher";
stmt = con.createStatement(); // Connection string for ResultSet - rs.
rs = stmt.executeQuery(SQL);
int pno = 1;
int PID=0;
while (rs.next()) {
PID = rs.getInt("pub_id");
pno++;
PID++;
pno = PID;
txtPno.setText(Integer.toString(pno));
txtPName.setFocusable(true);
con.close();
rs.close();
stmt.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage());
e.printStackTrace();
} private void
cmdSaveActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","
123456");
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM publisher";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int Pno = Integer.parseInt(txtPno.getText());
String PName = txtPName.getText();
String PAdd = txtPAdd.getText();
String Pph1 = txtPh1.getText();
String Pph2 = txtPh2.getText();
if(txtPh1.getText().length()!=10)
JOptionPane.showMessageDialog(this,"Incorrect Phone Number!");
if(txtPh1.getText().contains("[0-9]"))
{
System.out.println("Invalid");
if(txtPh2.getText().length()!=10)
JOptionPane.showMessageDialog(this,"Incorrect Phone Number!");
if(txtPh2.getText().contains("[0-9]"))
System.out.println("Invalid");
char PStatus = 'Y';
int code = JOptionPane.showConfirmDialog(this, "Are you sure to
add?", "Confirmation Dialog Box", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION) {
String strSQL = "INSERT INTO Publisher(pub_id, pub_name,
pub_address, pub_phone1, pub_phone2, status) VALUES ("+(Pno)+",
'"+(PName)+"', '"+(PAdd)+"', '"+(Pph1)+"', '"+(Pph2)+"', '"+(PStatus)+"')";
int rowsEffected = stmt.executeUpdate(strSQL);
JOptionPane.showMessageDialog(this, "Record added successfully
into Publisher table");
}
con.close();
stmt.close();
rs.close();
cmdSave.setEnabled(false);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
Now we have to MODIFY PUBLISHER DETAILS
We have to click on MODIFICATION button.
A new frame will open. On the left side of the frame all the
Publishers are shown.
Click on the Publisher which has to be Updated.
All the details will be shown.
Click to Update Button. Details will be Updated.
TYPE CODE :
private void cmdUpdateActionPerformed(java.awt.event.ActionEvent evt)
{
try {
// Connect to MySQL database
// Don't forget to import the two packages
// import java.sql.*;
// import javax.swing.JOptionPane;
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/library","roo
t","123456");
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM Publisher";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int pno = Integer.parseInt(txtPno.getText().trim());
String PubName = txtPName.getText();
String PubAdd = txtPAdd.getText();
String PubPh1 = txtPh1.getText();
String PubPh2 = txtPh2.getText();
if(txtPh1.getText().length()!=10)
{
JOptionPane.showMessageDialog(this,"Incorrect Phone
Number!");
if(txtPh1.getText().contains("[0-9]"))
System.out.println("Invalid");
if(txtPh2.getText().length()!=10)
JOptionPane.showMessageDialog(this,"Incorrect Phone
Number!");
if(txtPh2.getText().contains("[0-9]"))
System.out.println("Invalid");
String PStatus = txtStatus.getText();
int code = JOptionPane.showConfirmDialog(this, "Are you sure to
modify?", "Confirmation Dialog Box",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (code == JOptionPane.YES_OPTION){
String strSQL = "Update publisher set pub_name
='"+(PubName)+"',pub_address = '"+(PubAdd)+"', pub_phone1 =
'"+(PubPh1)+"', pub_phone2 = '"+(PubPh2)+"', status = '"+(PStatus)+"'
where pub_id = " + (pno);
int rowsEffected = stmt.executeUpdate(strSQL);
if (rowsEffected == 0)
JOptionPane.showMessageDialog(this, "Record does not exists");
else
JOptionPane.showMessageDialog(this,"Record modified");
} con.close();
stmt.close();
rs.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
Now we have to DELETE PUBLISHER DETAILS
We have to click on DELETE button.
A new frame will open. On the left side of the frame all the
Publishers are shown.
Click on the Publisher which has to be Deleted.
All the details will be shown.
Click to Delete Button. Details will be Deleted.
TYPE CODE :
Statement stmt = null;
ResultSet rs = null;
String SQL = "SELECT * FROM Publisher";
private void cmdDeleteActionPerformed(java.awt.event.ActionEvent
evt) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/library","roo
t","123456");
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
int pno = Integer.parseInt(txtPno.getText().trim());
// Steps to confirm deletion
int opt = JOptionPane.showConfirmDialog(null, "Are you sure to
delete this record ?");
if (opt == JOptionPane.YES_OPTION)
try {
char stb = 'N'; // Member table
String strSQL = "DELETE FROM publisher WHERE pub_id =" +
(pno);
int rowsEffected = stmt.executeUpdate(strSQL);
if (rowsEffected == 0)
JOptionPane.showMessageDialog(this, "Record does not
exists");
else
JOptionPane.showMessageDialog(this,"Record Deleted");
// Text boxes cleared
txtPno.setText("");
txtPName.setText("");
txtPAdd.setText("");
txtPh1.setText("");
txtPh2.setText("");
txtStatus.setText("");
txtPno.setEditable(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Unable to delete");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}