TRIBHUVAN UNIVERSITY
RATNA RAJYA LAXMI CAMPUS
Exhibition Road, Kathmandu
Advance Java
Lab Sheet from 02
Submitted by: Submitted by:
Name: Ajaya Bhattarai Mr. Bipin Timilsina
Roll no.: 02(A)
BCA 6th Semester Signature:………………..
Reg No.: 6-2-40-2-2022 Date:……………………..
Question No.:12 (Output)-
Question No.:13 (Output)-
Question No.:14 (Output)-
Question No.:15a (Output)-
Question No.:15b (Output)-
Question No.:15c (Output)-
Question No.:16a (Output)-
Question No.:16b (Output)-
Question No.:17 (Output)-
Question No.:18 (Output)-
Question No.:19 (Output)-
Question No.:20 (Output)-
Question No.:21 (Output)-
Question No.:22 (Output)-
Question No.:23 (Output)-
Question No.:24 (Output)-
Question No.:25 (Output)-
Question No.:26 (Output)-
Question No.:27 (Code)-
/**
*
* @author User
*/
import [Link].*;
import [Link];
import [Link];
import [Link];
public class NewJFrame extends [Link]
{
private static final [Link] logger =
[Link]([Link]());
/**
* Creates new form NewJFrame
*/
Connection con;
Statement st;
PreparedStatement pst;
public NewJFrame()
{
initComponents();
Connect();
DisplayData();
}
// Database Connection
public void Connect()
{
try
{
[Link]("[Link]");
con =
[Link]("jdbc:mysql://localhost:3306/database","root","");
}
catch(Exception ex)
{
[Link]();
[Link](this,"Database connection failed: " +
[Link]());
}
}
// Display data in JTable
public void DisplayData()
{
PreparedStatement pst = null;
ResultSet rs = null;
try{
String sql = "SELECT id, name, address FROM students";
pst = [Link](sql);
rs = [Link]();
DefaultTableModel dt = (DefaultTableModel)[Link]();
[Link](0);
while([Link]())
{
Vector<Object> v = new Vector<>();
[Link]([Link]("id"));
[Link]([Link]("name"));
[Link]([Link]("address"));
[Link](v);
}
}
catch(Exception ex)
{
[Link]();
[Link](this,"Error loading data: " +
[Link]());
}
finally
{
try
{
if (rs != null) [Link]();
if (pst != null) [Link]();
}
catch(Exception ex)
{
[Link]();
}
}
}
private void jButton1ActionPerformed([Link] evt) {
// TODO add your handling code here:
// Check empty fields
if ([Link]().equals("") ||[Link]().equals("")
||[Link]().equals(""))
{
[Link](rootPane, "Please fill all fields");
return;
}
PreparedStatement pst = null;
try
{
int id = [Link]([Link]());
String name = [Link]();
String address = [Link]();
// Correct SQL using PreparedStatement
String sql = "INSERT INTO students (id, name, address) VALUES (?, ?,
?)";
pst = [Link](sql);
[Link](1, id);
[Link](2, name);
[Link](3, address);
int row = [Link]();
if (row > 0)
{
[Link](rootPane, "Inserted Successfully");
DisplayData(); // Refresh JTable
}
else
{
[Link](rootPane, "Insertion Failed");
}
}
catch (Exception ex)
{
[Link]();
[Link](rootPane,
"Error while inserting record: " + [Link]());
}
finally
{
try
{
if (pst != null)
{
[Link]();
}
}
catch (SQLException ex)
{
[Link]();
}
}
}
private void jButton2ActionPerformed([Link] evt) {
// TODO add your handling code here:
// Check empty fields
if ([Link]().equals("") || [Link]().equals("")
||[Link]().equals(""))
{
[Link](rootPane, "Please fill all fields");
return;
}
int selectedIndex = [Link]();
// Check if row is selected
if (selectedIndex == -1)
{
[Link](rootPane, "No record selected for
update");
return;
}
PreparedStatement pst = null;
try
{
int id = [Link]([Link]());
String name = [Link]();
String address = [Link]();
// Correct SQL (removed extra comma)
String sql = "UPDATE students SET name = ?, address = ? WHERE id =
?";
pst = [Link](sql);
[Link](1, name);
[Link](2, address);
[Link](3, id);
int rows = [Link]();
if (rows > 0)
{
[Link](rootPane, "Updated Successfully");
DisplayData(); // Refresh table
}
else
{
[Link](rootPane, "Update Failed");
}
}
catch (Exception ex)
{
[Link]();
[Link](rootPane,"Error while updating record:
" + [Link]());
}
finally
{
try
{
if (pst != null)
{
[Link]();
}
}
catch (SQLException ex)
{
[Link]();
}
}
}
//Button3 ActionPerformed
private void jButton3ActionPerformed([Link] evt) {
// TODO add your handling code here:
int selectedIndex = [Link]();
// Check if row is selected
if (selectedIndex == -1)
{
[Link](rootPane, "No record selected for
delete");
return;
}
// Confirmation dialog
int confirm = [Link](rootPane,"Are you sure you
want to delete this record?",
"Confirm Deletion",JOptionPane.YES_NO_OPTION);
if (confirm != JOptionPane.YES_OPTION)
{
return;
}
DefaultTableModel dt = (DefaultTableModel) [Link]();
PreparedStatement pst = null;
try
{
int id = [Link]([Link](selectedIndex, 0).toString());
String sql = "DELETE FROM students WHERE id = ?";
pst = [Link](sql);
[Link](1, id);
int rows = [Link]();
if (rows > 0)
{
[Link](rootPane, "Deleted Successfully");
DisplayData(); // Refresh JTable
}
else
{
[Link](rootPane, "Deletion Failed");
}
}
catch (Exception ex)
{
[Link]();
[Link](rootPane,"Error while deleting record: "
+ [Link]());
}
finally
{
try
{
if (pst != null)
{
[Link]();
}
}
catch (SQLException ex)
{
[Link]();
}
}
}
//JTableMouseClicked
private void jTable1MouseClicked([Link] evt) {
// TODO add your handling code here:
DefaultTableModel dt = (DefaultTableModel)[Link]();
int selectedIndex = [Link]();
if(selectedIndex == -1)
{
return ;
}
[Link]([Link](selectedIndex,0).toString());
[Link]([Link](selectedIndex,1).toString());
[Link]([Link](selectedIndex,2).toString());
}
//Void Main
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look
and feel.
* For details see
[Link]
*/
try {
for ([Link] info :
[Link]()) {
if ("Nimbus".equals([Link]())) {
[Link]([Link]());
break;
}
}
} catch (ReflectiveOperationException |
[Link] ex) {
[Link]([Link], null, ex);
}
//</editor-fold>
try
{
for([Link]
info:[Link]())
{
if("Nimbus".equals([Link]()))
{
[Link]([Link]());
break;
}
}
}
catch(Exception ex)
{
[Link]([Link]()).log([Link]
.[Link],null,ex);
}
/* Create and display the form */
[Link](() -> {
new NewJFrame().setVisible(true);
});
}
private [Link] jButton1;
private [Link] jButton2;
private [Link] jButton3;
private [Link] jLabel1;
private [Link] jLabel2;
private [Link] jLabel3;
private [Link] jLabel4;
private [Link] jScrollPane1;
private [Link] jTable1;
private [Link] jTextField1;
private [Link] jTextField2;
private [Link] jTextField3;
// End of variables declaration
}
Question No.:27 (Output)-