Java Lab Manual (2)
Java Lab Manual (2)
#imports
import java.sql.*; // For database operations
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane; // For displaying dialog boxes
import javax.swing.table.DefaultTableModel;
import java.io.*; // For file operations and exceptions
import com.itextpdf.text.*; // For PDF document creation
import com.itextpdf.text.pdf.*; // For PDF table and writer
#1 Create connection
// after product form
try {
Class.forName("com.mysql.cj.jdbc.Driver"); // Use the newer driver class name
con = DriverManager.getConnection(url, user, password);
System.out.println("Connection established successfully.");
} catch (ClassNotFoundException e) {
Logger.getLogger(productForm.class.getName()).log(Level.SEVERE, "MySQL JDBC
Driver not found.", e);
} catch (SQLException e) {
Logger.getLogger(productForm.class.getName()).log(Level.SEVERE, "Connection
failed.", e);
}
}
#2 insert code
// double click create button and inside action performed.
if(txtName.getText().isEmpty()){
JOptionPane.showMessageDialog(this,"Product Name is required !!!");
} else if(txtPrice.getText().isEmpty()){
JOptionPane.showMessageDialog(this,"Product Price is required !!!");
} else if(txtQty.getText().isEmpty()){
JOptionPane.showMessageDialog(this,"Product Qty is required !!!");
} else {
try {
1
// Retrieve input values
String name = txtName.getText().trim();
String price = txtPrice.getText().trim();
String qty = txtQty.getText().trim();
// Set parameters
pst.setString(1, name);
pst.setString(2, price);
pst.setString(3, qty);
while(rs.next()){
txtpid.addItem(rs.getString(1));
}
} catch (SQLException ex) {
Logger.getLogger(productForm.class.getName()).log(Level.SEVERE, "Database
error:", ex);
}
}
#4 Search code
// inside Search button event action performed
try {
2
pst = con.prepareStatement("SELECT * FROM product WHERE id=?");
pst.setString(1,pid);
rs = pst.executeQuery();
if(rs.next()==true){
txtName.setText(rs.getString(2));
txtPrice.setText(rs.getString(3));
txtQty.setText(rs.getString(4));
}else{
JOptionPane.showMessageDialog(this,"No record found !!!");
}
} catch (SQLException ex) {
Logger.getLogger(productForm.class.getName()).log(Level.SEVERE,null, ex);
}
#5 Update Product
// inside update button action performed
try {
String name = txtName.getText();
String price = txtPrice.getText();
String qty = txtQty.getText();
String pid = txtpid.getSelectedItem().toString();
pst.setString(1,name);
pst.setString(2,price);
pst.setString(3,qty);
pst.setString(4,pid);
int k=pst.executeUpdate();
if(k==1){
JOptionPane.showMessageDialog(this,"Record updated Successfully !!!");
txtName.setText("");
txtPrice.setText("");
txtQty.setText("");
txtName.requestFocus();
LoadProductNo();
}
} catch (SQLException ex){
Logger.getLogger(productForm.class.getName()).log(Level.SEVERE,null, ex);
}
#6 Delete Product
// inside delete button action performed
try {
String pid = txtpid.getSelectedItem().toString();
pst = con.prepareStatement("DELETE FROM product WHERE id=?");
pst.setString(1, pid);
int k = pst.executeUpdate();
if (k == 1) {
JOptionPane.showMessageDialog(this, "Record has been deleted successfully!");
txtName.setText("");
3
txtPrice.setText("");
txtQty.setText("");
txtName.requestFocus();
LoadProductNo();
} else {
JOptionPane.showMessageDialog(this, "Failed to delete record!");
}
} catch (SQLException ex) {
Logger.getLogger(productForm.class.getName()).log(Level.SEVERE, null, ex);
}
while (rs.next()) {
Object[] rowData = {
rs.getString("id"),
rs.getString("name"),
rs.getString("price"),
rs.getString("qty")
};
df.addRow(rowData);
}
} catch (SQLException ex) {
Logger.getLogger(productForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
4
while (rs.next()) {
fw.append(rs.getString(1)).append(',')
.append(rs.getString(2)).append(',')
.append(rs.getString(3)).append(',')
.append(rs.getString(4)).append('\n');
}
JOptionPane.showMessageDialog(this, "File exported successfully !!!");
} catch (IOException | SQLException ex) {
Logger.getLogger(productForm.class.getName()).log(Level.SEVERE, null,
ex);
}
# 9 Login Form
// create LoginForm like product form by right click inside default
//package.
// to make login form comes as first change inside main class of product
//form productForm().setVisible(true);to loginForm().setVisible(true);
// copy connection and import code from product form
// create users table with id,username and password.
Imports
import java.sql.*; // For database operations
import java.util.logging.*; // For logging
import javax.swing.*; // For Swing components like JOptionPane
// inside btnLogin
5
} else {
JOptionPane.showMessageDialog(null, "Username or password is incorrect
!!!");
txtUserName.setText("");
txtPassword.setText("");
}
}
} catch (SQLException ex) {
Logger.getLogger(loginForm.class.getName()).log(Level.SEVERE, null,
ex);
}
// inside btnCancel
//System.exit(0);
txtUserName.setText("");
txtPassword.setText("");