Professional Documents
Culture Documents
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
Practical
JDBC
1. Write a jdbc program to insert depno, deptname and location of department from java console.
CODE :
import java.sql.*;
import java.io.*;
}
catch(Exception e){
System.out.println("Error11 :"+e.getMessage());
}
}
}
OUTPUT :
Page 1 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
2. Write a jdbc program to insert depno, deptname and location ofdepartment from java console using
PreparedStatement.
CODE :
package javaapplication21;
import com.mysql.jdbc.Connection;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
String DB = "SYCS";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();
st.execute(Query);
Page 2 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
String DB = "SYCS";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();
pstm.execute();
while (rs.next()) {
System.out.println("Dept_No : " + rs.getInt(1)
+ " Dept_Name: " + rs.getString(2)
+ " Dept_Location: " + rs.getString(3));
}
connect.close();
} catch (Exception ex) {
System.out.println("error: " + ex);
}
}
Page 3 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
CODE :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication21;
import com.mysql.jdbc.Connection;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
String DB = "Ashlok";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();
Page 4 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
st.execute(Query);
String DB = "Ashlok";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();
pstm.execute();
String DB = "Ashlok";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();
pstm.execute();
Page 5 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
while (rs.next()) {
System.out.println("Dept_No : " + rs.getInt(1)
+ " Dept_Name: " + rs.getString(2)
+ " Dept_Location: " + rs.getString(3));
}
connect.close();
} catch (Exception ex) {
System.out.println("error: " + ex);
}
}
Page 6 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
4. Write a jdbc program to insert name and age of the student into the Student table using the
following GUI.
CODE :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication22;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Scanner;
/**
*
* @author LAB1
*/
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name");
jLabel2.setText("Age");
jButton1.setText("Insert");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Page 7 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
jButton1ActionPerformed(evt);
}
});
pack();
}// </editor-fold>
Page 8 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
pstm.execute();
/**
* @param args the command line arguments
*/
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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
}
//</editor-fold>
Page 9 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
OUTPUT :
Page 10 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
5. Write a jdbc program to create a table in the database using the following GUI. (HW)
CODE :
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package practice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
/**
* Creates new form P7
*/
public P7() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
Page 11 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
jLabel1.setText("TABLE NAME");
jLabel2.setText("TYPE");
jLabel3.setText("COLUME !");
jLabel4.setText("COLUME 2");
jLabel5.setText("TYPE");
jLabel6.setText("SIZE");
jLabel7.setText("SIZE");
jLabel8.setText("SIZE");
jLabel9.setText("TYPE");
jLabel10.setText("COLUME 3");
C2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
C2ActionPerformed(evt);
}
});
jButton1.setText("CREATE TABLE");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
Page 12 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
Page 13 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(68, 68, 68)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 87,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 215,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(198, 198, 198)
.addComponent(jButton1)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(15, 15, 15)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel8)
.addComponent(jLabel2)
.addComponent(S1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(T1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(42, 42, 42)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(C1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(jLabel5)
.addComponent(jLabel4)
.addComponent(S2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(C2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(T2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(S3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
Page 14 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
pack();
}// </editor-fold>
query.append(")");
Page 15 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
/**
* @param args the command line arguments
*/
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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(P7.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(P7.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(P7.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(P7.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
Page 16 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
OUTPUT :
Page 17 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
6. Write a jdbc program to delete an employee record from the Employee table using the following
GUI. (HW)
CODE:
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package practice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.swing.JOptionPane;
public P6() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("DELETE");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
Page 18 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
}
});
jLabel1.setText("ID");
pack();
}// </editor-fold>
// Construct the SQL query with the ID directly (not recommended for user input due to SQL injection
risk)
String query = "DELETE FROM emp WHERE ID = " + idToDelete;
if (rowsAffected > 0) {
// Data deleted successfully
Page 19 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
Page 20 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
7. Write a jdbc program to select the maximum salary of the employee from the employee table using
the following GUI. (HW)
CODE :
package practice;
import java.sql.*;
import javax.swing.JOptionPane;
public P4() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
Page 21 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
sal.setText("SALARY");
sal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
salActionPerformed(evt);
}
});
pack();
}// </editor-fold>
if (rs.next()) {
int maxSalary = rs.getInt("max_salary");
JOptionPane.showMessageDialog(null, "MAX SALARY = "+ maxSalary, "Success",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Max salary not found", "Failure",
JOptionPane.ERROR_MESSAGE);
Page 22 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(P4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(P4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(P4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(P4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
Page 23 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
8. Write a jdbc program to update the password in the Login table using the following GUI.
CODE :
package practice;
import java.sql.*;
import javax.swing.JOptionPane;
public P5() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("ID");
jLabel2.setText("PASSWORD");
jLabel3.setText("NEW PASSWORD");
jButton1.setText("change");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
id.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
idActionPerformed(evt);
}
});
Page 24 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
pack();
}// </editor-fold>
Page 25 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
if (rowsAffected > 0) {
// Password updated successfully
JOptionPane.showMessageDialog(null, "Password updated successfully", "Success",
JOptionPane.INFORMATION_MESSAGE);
} else {
// Handle the case when no rows are updated (incorrect current password or invalid ID)
JOptionPane.showMessageDialog(null, "Password update failed. Check your ID and current password.",
"Failure", JOptionPane.ERROR_MESSAGE);
}
/**
* @param args the command line arguments
*/
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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(P5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(P5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
Page 26 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
OUTPUT :
Page 27 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
9.Write a jdbc program to insert the student information into tabular form
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package practice;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
*
* @author pranav mishra
*/
public class P8 extends javax.swing.JFrame {
/**
* Creates new form P8
*/
public P8() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
Page 28 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
name.setText("NAME");
age.setText("AGE");
marks.setText("MARKS");
grade.setText("GRADE");
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"NAME", "AGE", "MARKS", "GRADE"
}
){
Class[] types = new Class [] {
java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class
};
insert.setText("insert");
insert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
insertActionPerformed(evt);
}
});
display.setText("display");
display.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
displayActionPerformed(evt);
}
});
Page 29 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
Page 30 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
pack();
}// </editor-fold>
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SYCS", "root", "bhavans");
String query = "INSERT INTO MARKS (NAME, AGE, MARKS, GRADE) VALUES (?, ?, ?, ?)";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.setString(1, naame.getText());
preparedStatement.setString(2, agee.getText());
preparedStatement.setString(3, markss.getText());
preparedStatement.setString(4, gradee.getText());
if (rowsAffected > 0) {
// Data inserted successfully
JOptionPane.showMessageDialog(null, "Data inserted successfully", "Success",
JOptionPane.INFORMATION_MESSAGE);
} else {
// Handle the case when no rows are inserted
JOptionPane.showMessageDialog(null, "Failed to insert data", "Failure", JOptionPane.ERROR_MESSAGE);
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SYCS", "root",
"bhavans");
java.sql.Statement st = con.createStatement();
String query = "SELECT * FROM marks";
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
String namee = rs.getString("name");
String age = String.valueOf(rs.getInt("age"));
Page 31 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
con.close();
} catch (Exception e) {
// Handle any exceptions that may occur during database access
JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Failure",
JOptionPane.ERROR_MESSAGE);
}
/**
* @param args the command line arguments
*/
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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(P8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(P8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(P8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(P8.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
Page 32 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
Page 33 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
10. Write a jdbc program to insert name and age into Student tablewith prepared statement using
following GUI. (HW)
CODE :
package practice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class P2 extends javax.swing.JFrame {
/**
* Creates new form P2
*/
public P2() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("NAME");
jLabel2.setText("AGE");
jButton1.setText("INSERT");
Page 34 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
pack();
}// </editor-fold>
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SYCS", "oot", "bhavans");
String query = "INSERT INTO STUDENT (NAME, AGE) VALUES (?, ?)";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.setString(1, name.getText());
preparedStatement.setInt(2, Integer.parseInt(age.getText()));
Page 35 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
if (rowsAffected > 0) {
// Data inserted successfully
JOptionPane.showMessageDialog(null, "Data inserted successfully", "Success",
JOptionPane.INFORMATION_MESSAGE);
} else {
// Handle the case when no rows are inserted
JOptionPane.showMessageDialog(null, "Failed to insert data", "Failure", JOptionPane.ERROR_MESSAGE);
}
/**
* @param args the command line arguments
*/
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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(P2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(P2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(P2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(P2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
Page 36 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
OUTPUT :
Page 37 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
11. Write a jdbc program to delete the employee record from Employee table with prepared statement
using the following GUI.(HW)
package practice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
/**
* Creates new form P3
*/
public P3() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("ID");
jButton1.setText("DELETE");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
Page 38 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
.addGroup(layout.createSequentialGroup()
.addGap(130, 130, 130)
.addComponent(jButton1)))
.addContainerGap(167, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(del, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(40, 40, 40)
.addComponent(jButton1)
.addContainerGap(135, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
if (rowsAffected > 0) {
// Data inserted successfully
JOptionPane.showMessageDialog(null, "Data deleted successfully", "Success",
JOptionPane.INFORMATION_MESSAGE);
} else {
// Handle the case when no rows are inserted
JOptionPane.showMessageDialog(null, "Failed to delete data", "Failure", JOptionPane.ERROR_MESSAGE);
}
/**
* @param args the command line arguments
*/
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.
Page 39 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
Page 40 of 41
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
Class: SYCS Sem: 3 Date of Performance:
Course Name : Introduction to Java Programming Course Number : BH.USCSP302
Practical Number : Page Number :
Aim:
Teacher’s Signature :
SYCS - 13 Introduction of Java Programming DATE -
Page 41 of 41