You are on page 1of 82

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 -

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.*;

public class DeptInsert {


public static void main(String arg[])throws SQLException{
//connection parm
try{
Class.forName("com.mysql.jdbc.Driver");
try(Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db","root","bhavans")) {
Statement st=con.createStatement();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in) );
System.out.printf("Enter Dept-No:");
int no=Integer.parseInt(br.readLine());
System.out.printf("Enter Dept-Name:");
String name=br.readLine();
System.out.printf("Enter Dept-Location:");
String loc=br.readLine();
String sqll="insert into depttable values("+no +", '"+name+"' , '"+loc+"' )";
st.executeUpdate(sqll);
System.out.println("\n\t\t Record Saved");
}

}
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;

public class NewClass {

static String url = "jdbc:mysql://localhost:3306/";


static String username = "root";
static String password = "bhavans";
static Connection connect;
final static Scanner sc = new Scanner(System.in);

// Inserting Data using Statement.


public static void insertData() throws IOException, SQLException {

String DB = "SYCS";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();

System.out.println("Enter Dept _No: ");


int dept_no = sc.nextInt();

System.out.println("Enter Dept_Name: ");


String dept_name = sc.next();
sc.nextLine();

System.out.println("Enter Dept _Location: ");


String dept_Location = sc.next();

String Query = "INSERT INTO department(deptno, deptname, dptloc)"


+ " VALUES(" + dept_no + ",'" + dept_name + "','" + dept_Location + "')";

st.execute(Query);

System.out.println("Data Inserted Sucessfully...");


connect.close();

} catch (Exception ex) {


System.out.println("error: " + ex);
}
}

// Inserting data into database using prepared statement.

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 -

public static void insert() throws IOException, SQLException {

String DB = "SYCS";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();

String query = "INSERT INTO department(deptno, deptname, dptloc) VALUES(?, ?, ?)";


PreparedStatement pstm = connect.prepareStatement(query);
pstm.setInt(1, 3);
pstm.setString(2, "FYCS");
pstm.setString(3, "Churchgate");

pstm.execute();

System.out.println("Data Inserted Sucessfully...");


connect.close();

} catch (Exception ex) {


System.out.println("error: " + ex);
}
}

public static void print() throws IOException, SQLException {


String table = "department";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + "Ashlok", username, password);
Statement st = connect.createStatement();
String Query = "SELECT * FROM " + table;
ResultSet rs = st.executeQuery(Query);

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);
}
}

public static void main(String[] args) throws IOException, SQLException {


insert();
print();
}
}

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 -

3. Write a jdbc program to update department information from java console.

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;

public class NewClass {

static String url = "jdbc:mysql://localhost:3306/";


static String username = "root";
static String password = "bhavans";
static Connection connect;
final static Scanner sc = new Scanner(System.in);

// Inserting Data using Statement.


public static void insertData() throws IOException, SQLException {

String DB = "Ashlok";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();

System.out.println("Enter Dept _No: ");


int dept_no = sc.nextInt();

System.out.println("Enter Dept_Name: ");


String dept_name = sc.next();
sc.nextLine();

System.out.println("Enter Dept _Location: ");


String dept_Location = sc.next();

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 -

String Query = "INSERT INTO department(deptno, deptname, dptloc)"


+ " VALUES(" + dept_no + ",'" + dept_name + "','" + dept_Location + "')";

st.execute(Query);

System.out.println("Data Inserted Sucessfully...");


connect.close();

} catch (Exception ex) {


System.out.println("error: " + ex);
}
}

// Inserting data into database using prepared statement.

public static void insert() throws IOException, SQLException {

String DB = "Ashlok";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();

String query = "INSERT INTO department(deptno, deptname, dptloc) VALUES(?, ?, ?)";


PreparedStatement pstm = connect.prepareStatement(query);
pstm.setInt(1, 3);
pstm.setString(2, "FYCS");
pstm.setString(3, "Churchgate");

pstm.execute();

System.out.println("Data Inserted Sucessfully...");


connect.close();

} catch (Exception ex) {


System.out.println("error: " + ex);
}
}

// updating the data using prepared statement

public static void update() throws IOException, SQLException {

String DB = "Ashlok";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();

String query = "UPDATE department SET dptloc = ? WHERE deptno = ? ";


PreparedStatement pstm = connect.prepareStatement(query);
pstm.setString(1, "Churchgate");
pstm.setInt(2, 1);

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 -

System.out.println("Data updated Sucessfully...");


connect.close();

} catch (Exception ex) {


System.out.println("error: " + ex);
}
}

// Printing the benefiacial data available in the created table :)


public static void print() throws IOException, SQLException {
String table = "department";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + "Ashlok", username, password);
Statement st = connect.createStatement();
String Query = "SELECT * FROM " + table;
ResultSet rs = st.executeQuery(Query);

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);
}
}

public static void main(String[] args) throws IOException, SQLException {


print();
update();
print();
}
}
OUTPUT :

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 {

static String url = "jdbc:mysql://localhost:3306/";


static String username = "root";
static String password = "bhavans";
static Connection connect;
final static Scanner sc = new Scanner(System.in);

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() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();

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);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75,
Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 91,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(67, 67, 67)
.addComponent(jTextField1))))
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(jButton1)))
.addGap(192, 192, 192))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(34, 34, 34)
.addComponent(jButton1)
.addContainerGap(161, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
String DB = "Ashlok";
try {
Class.forName("com.mysql.jdbc.Driver");
connect = (Connection) DriverManager.getConnection(url + DB, username, password);
Statement st = connect.createStatement();

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 -

int age = Integer.parseInt(jTextField2.getText());


String name = jTextField1.getText();
String Query = "INSERT INTO Student(age,name) VALUES ( ?, ?)";

PreparedStatement pstm = connect.prepareStatement(Query);


pstm.setInt(1, age);
pstm.setString(2, name);

pstm.execute();

System.out.println("Data Inserted Sucessfully...");


connect.close();

} catch (Exception ex) {


System.out.println("error: " + ex);
}

/**
* @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>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

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 -

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration

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;

public class P7 extends javax.swing.JFrame {

/**
* 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() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
S1 = new javax.swing.JTextField();
S2 = new javax.swing.JTextField();
S3 = new javax.swing.JTextField();
C1 = new javax.swing.JTextField();
C2 = new javax.swing.JTextField();
C3 = new javax.swing.JTextField();
T1 = new javax.swing.JComboBox<>();
T2 = new javax.swing.JComboBox<>();
T3 = new javax.swing.JComboBox<>();
jButton1 = new javax.swing.JButton();
name = new javax.swing.JTextField();

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);
}
});

T1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "int", "varchar", "char", "text" }));

T2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "int", "varchar", "char", "text" }));


T2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
T2ActionPerformed(evt);
}
});

T3.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "int", "varchar", "char", "text" }));

jButton1.setText("CREATE TABLE");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 65,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13,
Short.MAX_VALUE)

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 -

.addComponent(C1, javax.swing.GroupLayout.PREFERRED_SIZE, 71,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33, 33, 33))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 64,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(C2, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 64,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(C3, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(T3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(T2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(T1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(46, 46, 46)
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(S1, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(S2, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(S3, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(71, 71, 71))

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 -

.addGap(39, 39, 39)


.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(jLabel10)
.addComponent(C3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(T3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(38, 38, 38)
.addComponent(jButton1)
.addContainerGap(97, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void C2ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void T2ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SYCS", "root", "bhavans");

// Retrieve table name from a JTextField


String tableName = name.getText(); // Assuming name.getText() returns the table name as a String

// Initialize arrays to store column information


String[] columnNames = {C1.getText(), C2.getText(), C3.getText()};
String[] dataTypes = {T1.getSelectedItem().toString(), T2.getSelectedItem().toString(),
T3.getSelectedItem().toString()};
String[] sizes = {S1.getText(), S2.getText(), S3.getText()};

// Create the SQL query to create the table


StringBuilder query = new StringBuilder("CREATE TABLE ");
query.append(tableName).append(" (");

for (int i = 0; i < columnNames.length; i++) {


query.append(columnNames[i]).append(" ").append(dataTypes[i]);

// Add size if provided


if (!sizes[i].isEmpty()) {
query.append("(").append(sizes[i]).append(")");
}

// Add a comma for all columns except the last one


if (i < columnNames.length - 1) {
query.append(", ");
}
}

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 -

// Create a PreparedStatement with the SQL query


PreparedStatement preparedStatement = con.prepareStatement(query.toString());

// Execute the query to create the table


preparedStatement.executeUpdate();

// Inform the user about the successful table creation


JOptionPane.showMessageDialog(null, "Table created successfully", "Success",
JOptionPane.INFORMATION_MESSAGE);

// Close the resources (connection and preparedStatement) when done.


preparedStatement.close();
con.close();
} catch (Exception e) {
// Handle any exceptions that may occur during database access
JOptionPane.showMessageDialog(null, "Something is wrong with connectivity", "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(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>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new P7().setVisible(true);
}
});
}

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 -

// Variables declaration - do not modify


public javax.swing.JTextField C1;
public javax.swing.JTextField C2;
public javax.swing.JTextField C3;
public javax.swing.JTextField S1;
public javax.swing.JTextField S2;
public javax.swing.JTextField S3;
public javax.swing.JComboBox<String> T1;
public javax.swing.JComboBox<String> T2;
public javax.swing.JComboBox<String> T3;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
public javax.swing.JTextField name;
// End of variables declaration
}

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 class P6 extends javax.swing.JFrame {

public P6() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();


jLabel1 = new javax.swing.JLabel();
del = new javax.swing.JTextField();

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");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(84, 84, 84)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(del, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jButton1)))
.addContainerGap(178, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(69, Short.MAX_VALUE)
.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(36, 36, 36)
.addComponent(jButton1)
.addGap(150, 150, 150))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SYCS", "root", "bhavans");

String idToDelete = del.getText(); // Assuming del.getText() returns the ID as a String

// 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;

Statement statement = con.createStatement();

int rowsAffected = statement.executeUpdate(query);

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 -

JOptionPane.showMessageDialog(null, "Data deleted successfully", "Success",


JOptionPane.INFORMATION_MESSAGE);
} else {
// Handle the case when no rows are deleted
JOptionPane.showMessageDialog(null, "No data found to delete", "Failure",
JOptionPane.ERROR_MESSAGE);
}

// Close the resources (connection and statement) when done.


statement.close();
con.close();
} catch (Exception e) {
// Handle any exceptions that may occur during database access
JOptionPane.showMessageDialog(null, "Something is wrong with connectivity", "Failure",
JOptionPane.ERROR_MESSAGE);
}

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(P6.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(P6.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(P6.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(P6.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new P6().setVisible(true);
}
});
}

// Variables declaration - do not modify


public javax.swing.JTextField del;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;

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 -

// End of variables declaration


}
OUTPUT :

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 class P4 extends javax.swing.JFrame {

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 -

private void initComponents() {

sal = new javax.swing.JButton();

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);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(138, 138, 138)
.addComponent(sal)
.addContainerGap(190, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(88, 88, 88)
.addComponent(sal)
.addContainerGap(189, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void salActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SYCS", "root", "bhavans");
Statement statement = con.createStatement();
String query = "SELECT MAX(salary) AS max_salary FROM emp";
ResultSet rs = statement.executeQuery(query);

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);

// Close the resources (connection and statement) when done.


statement.close();
con.close();
} catch (ClassNotFoundException | SQLException e) {

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 -

// Handle any exceptions that may occur during database access


JOptionPane.showMessageDialog(null, "Something is wrong with connectivity", "Failure",
JOptionPane.ERROR_MESSAGE);

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>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new P4().setVisible(true);
}
});
}

// Variables declaration - do not modify


public javax.swing.JButton sal;
// End of variables declaration
}

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 class P5 extends javax.swing.JFrame {

public P5() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
id = new javax.swing.JTextField();
pass = new javax.swing.JTextField();
npass = new javax.swing.JTextField();

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);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()

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 -

.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 105,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(npass, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 84,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(35, 35, 35)
.addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(layout.createSequentialGroup()
.addGap(109, 109, 109)
.addComponent(jButton1)))
.addGap(74, 146, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(npass, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(28, 28, 28)
.addComponent(jButton1)
.addContainerGap(94, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void idActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
Class.forName("com.mysql.cj.jdbc.Driver");

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 -

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SYCS", "root", "bhavans");

int idToDelete = Integer.parseInt(id.getText()); // Assuming id.getText() returns the ID as a String


String newPassword = npass.getText();
String currentPassword = pass.getText();

// Use a PreparedStatement to safely handle query parameters


String query = "UPDATE emp_id SET password = ? WHERE id = ? AND password = ?";
PreparedStatement preparedStatement = con.prepareStatement(query);

// Set the parameters for the PreparedStatement


preparedStatement.setString(1, newPassword);
preparedStatement.setInt(2, idToDelete);
preparedStatement.setString(3, currentPassword);

int rowsAffected = preparedStatement.executeUpdate();

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);
}

// Close the resources (connection and preparedStatement) when done.


preparedStatement.close();
con.close();
} catch (Exception e) {
// Handle any exceptions that may occur during database access
JOptionPane.showMessageDialog(null, "Something is wrong with connectivity", "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 -

} catch (IllegalAccessException ex) {


java.util.logging.Logger.getLogger(P5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(P5.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new P5().setVisible(true);
}
});
}

// Variables declaration - do not modify


public javax.swing.JTextField id;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
public javax.swing.JTextField npass;
public javax.swing.JTextField pass;
// End of variables declaration
}

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() {

name = new javax.swing.JLabel();


age = new javax.swing.JLabel();
marks = new javax.swing.JLabel();
grade = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable();
naame = new javax.swing.JTextField();
agee = new javax.swing.JTextField();
gradee = new javax.swing.JTextField();
markss = new javax.swing.JTextField();

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 -

insert = new javax.swing.JButton();


display = new javax.swing.JButton();

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
};

public Class getColumnClass(int columnIndex) {


return types [columnIndex];
}
});
jScrollPane1.setViewportView(table);

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);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(64, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(grade, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
javax.swing.GroupLayout.PREFERRED_SIZE)

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 -

.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, 57,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(age, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(marks, javax.swing.GroupLayout.PREFERRED_SIZE, 47,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(naame, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(gradee, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(markss, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(agee, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(insert)
.addGap(33, 33, 33)
.addComponent(display)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(name)
.addComponent(naame, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(age)
.addComponent(agee, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(marks)
.addComponent(markss, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(grade)
.addComponent(gradee, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(insert)
.addComponent(display))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(19, Short.MAX_VALUE)

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 -

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182,


javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(142, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void insertActionPerformed(java.awt.event.ActionEvent evt) {

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());

int rowsAffected = preparedStatement.executeUpdate();

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);
}

// Close the resources (connection and statement) when done.


preparedStatement.close();
con.close();
} catch (Exception e) {
// Handle any exceptions that may occur during database access
JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);

private void displayActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:

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 -

String marks = String.valueOf(rs.getInt("marks"));


String grade = rs.getString("grade");

// Add a new row to the table model


DefaultTableModel tableModel = (DefaultTableModel)table.getModel();
tableModel.addRow(new Object[]{namee, age, marks, grade});
}

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>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new P8().setVisible(true);
}
});
}

// Variables declaration - do not modify


public javax.swing.JLabel age;
public javax.swing.JTextField agee;
public javax.swing.JButton display;
public javax.swing.JLabel grade;

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 -

public javax.swing.JTextField gradee;


public javax.swing.JButton insert;
private javax.swing.JScrollPane jScrollPane1;
public javax.swing.JLabel marks;
public javax.swing.JTextField markss;
public javax.swing.JTextField naame;
public javax.swing.JLabel name;
public javax.swing.JTable table;
// End of variables declaration
}
OUTPUT :

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() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
name = new javax.swing.JTextField();
age = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();

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);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(94, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
.addComponent(age))
.addGap(95, 95, 95))
.addGroup(layout.createSequentialGroup()
.addGap(137, 137, 137)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(40, 40, 40)
.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))
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(age, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addComponent(jButton1)
.addContainerGap(121, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

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 -

int rowsAffected = preparedStatement.executeUpdate();

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);
}

// Close the resources (connection and statement) when done.


preparedStatement.close();
con.close();
} catch (Exception e) {
// Handle any exceptions that may occur during database access
JOptionPane.showMessageDialog(null, "Something is wrong with connectivity", "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>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new P2().setVisible(true);
}
});
}

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 -

// Variables declaration - do not modify


public javax.swing.JTextField age;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
public javax.swing.JTextField name;
// End of variables declaration
}

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;

public class P3 extends javax.swing.JFrame {

/**
* 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() {

jLabel1 = new javax.swing.JLabel();


del = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();

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);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(85, 85, 85)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40)
.addComponent(del, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
javax.swing.GroupLayout.PREFERRED_SIZE))

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>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SYCS", "root", "bhavans");
String query = "DELETE FROM emp WHERE ID = (?)";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.setInt(1, Integer.parseInt(del.getText()));

int rowsAffected = preparedStatement.executeUpdate();

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);
}

// Close the resources (connection and statement) when done.


preparedStatement.close();
con.close();
} catch (Exception e) {
// Handle any exceptions that may occur during database access
JOptionPane.showMessageDialog(null, "Something is wrong with connectivity", "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 -

* 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(P3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(P3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(P3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(P3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new P3().setVisible(true);
}
});
}

// Variables declaration - do not modify


public javax.swing.JTextField del;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
OUTPUT :

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

You might also like