Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

oops edited

Uploaded by

paviganesh8789
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

oops edited

Uploaded by

paviganesh8789
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

4.

Write a Java Program to create an abstract class named Shape


that contains two integers and an empty method named
printArea(). Provide three classes named Rectangle, Triangle and
Circle such that each one of the classes extends the class Shape.
Each one of the classes contains only the method printArea( ) that
prints the area of the given shape

AIM:
To write a Java program to calculate the area of rectangle, circle and triangle using
the concept of abstract class.

ALGORITHM:
Step 1: Start the program.
Step 2: Create an abstract class named shape that contains two integers and an
empty method named area().
Step 3: Create three classes named rectangle, triangle and circle such that each
one of the classes extends the class Shape.
Step 4: Each of the inherited class from shape class should provide the
implementation for the method area().
Step 5: In area() method get the input from user and calculate the area of
rectangle, circle and triangle.
Step 6: In the AbstractDDemo, create the objects for the three inherited classes
and invoke the methods and display the area values of the different shapes.
Step 7: Stop the program.

import java.util.*;
abstract class shape
{
int x,y;
abstract void area(double x,double y);
}
class Rectangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of rectangle is :"+
(x*y));
}
}
class Circle extends shape
{
void area(double x,double y)
{
System.out.println("Area of circle
is :"+(3.14*x*x));
}
}
class Triangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of triangle
is :"+(0.5*x*y));
}
}
public class AbstactDDemo
{
public static void main(String[] args)
{
Rectangle r=new Rectangle();
r.area(2,5);
Circle c=new Circle();
c.area(5,5);
Triangle t=new Triangle();
t.area(2,5);
}
}
5.Write a Java Program to create an abstract class named
Shape
that contains two integers and an empty method named
printArea(). Provide three classes named Rectangle, Triangle and
Circle such that each one of the classes extends the class Shape.
Each one of the classes contains only the method printArea( ) that
prints the area of the given shape. Solve the above problem using
an interface

AIM:
To write a Java program to calculate the area of rectangle, circle and triangle by
implementing the interface shape.

ALGORITHM:

Step 1: Start the program.

Step 2: Create an interface named shape that contains an empty methodnamed printArea().
Step 3: Create three classes named rectangle, triangle and circle such that each
one of the classes implements the class Shape.
Step 4: Each of the class should provide the implementation for the method printArea().
Step 5: In printArea() method get the input from user and calculate the area of
rectangle, circle and triangle.
Step 6: In the AbstractClassExample, create the objects for the three classes
and invoke the MethodprintArea() and display the area values of the different
shapes.
Step 7: Stop the program.

import java.util.*;
abstract class Shape {
int length, breadth, radius;
Scanner input = new Scanner(System.in);
abstract void printArea();
}
class Rectangle extends Shape {
void printArea() {
System.out.println("*** Finding the Area of Rectangle ***");
System.out.print("Enter length and breadth: ");
length = input.nextInt();
breadth = input.nextInt();
System.out.println("The area of Rectangle is: " + length *
breadth);
}
}

class Triangle extends Shape {


void printArea() {
System.out.println("\n*** Finding the Area of Triangle ***");
System.out.print("Enter Base And Height: ");
length = input.nextInt();
breadth = input.nextInt();
System.out.println("The area of Triangle is: " + (length *
breadth) / 2);
}
}

class Circle extends Shape {


void printArea() {
System.out.println("\n*** Finding the Area of Cricle ***");
System.out.print("Enter Radius: ");
radius = input.nextInt();
System.out.println("The area of Cricle is: " + 3.14f * radius *
radius);
}
}

public class AbstractClassExample {


public static void main(String[] args) {
Rectangle rec = new Rectangle();
rec.printArea();

Triangle tri = new Triangle();


tri.printArea();

Circle cri = new Circle();


cri.printArea();
}
}

RESULT:

Thus, the Java program to calculate the area of rectangle, circle and
triangle using the concept of interfacewasdeveloped and executed successfully.
6.IMPLEMENT EXCEPTION HANDLING AND CREATION OF USER DEFINED EXCEPTIONS

AIM:
To write a Java program to implement user defined exception handling.

ALGORITHM:

Step 1: Start the program.

Step 2: Create a class NegativeAmtException which extends Exception class.


Step 3: Create a constructor which receives the string as argument.
Step 4: Create a class named BankAccount with a constructor and methods
such as deposit(), withdraw(), getBalance() and toString().
Step 5: Inside the constructor, deposit() and withdraw() methods check for
negative amount. If the amount is negative, then exception will be
generated and thrown. Make these methods to specify that
NegativeAmtException will be thrown.
Step 6: Create a test class UserDefinedException . Read the account number and
initial balance from user and create object of BankAccount class.
Step 7: Display the menu and get the user choice and execute the required
operation. Write the code that generates exception, in try block.
Step 8: Use catch block to catch and handle NegativeAmtException. Display the
caught exception.
Step 9: Stop the program.

7.WRITE A JAVA PROGRAM THAT IMPLEMENTS A MULTI- THREADED APPLICATION THAT


HAS THREE THREADS. FIRST THREAD GENERATES A RANDOM INTEGER EVERY 1 SECOND
AND IF THE VALUE IS EVEN, THE SECOND THREAD COMPUTES THE SQUARE OF THE
NUMBER AND PRINTS. IF THE VALUE IS ODD, THE THIRD THREAD WILL PRINT THE VALUE
OF THE CUBE OF THE NUMBER

AIM:
To write a java program to implement a multi-threaded application.

ALGORITHM:

Step 1: Start the program.

Step 2: Create a class even which implements first thread that computes the
square of the number.
Step 3:Therun() method implements the code to be executed when thread gets
executed. Step 4:Create a class odd which implements second thread that
computes the cube of the number.
Step 5:Create a third thread that generates random number. If the random number is even,
it displaysthe square of the number. If the random number generated is odd, it displays
the cube of thegiven number.
Step 6: The Multithreading is performed and the task switched between multiple threads.
Step 7: The sleep () method makes the thread to suspend for the specified time.
Step 8: Stop the program.

import java.util.Random;

class RandomNumberThread extends Thread {

public void run() {


Random random = new Random();

for (int i = 0; i < 10; i++) {

int randomInteger = random.nextInt(100);

System.out.println("Random Integer generated : " +


randomInteger);

if((randomInteger%2) == 0) {

SquareThread sThread = new SquareThread(randomInteger);

sThread.start();

else {

CubeThread cThread = new CubeThread(randomInteger);

cThread.start();

try {

Thread.sleep(1000);

catch (InterruptedException ex) {

System.out.println(ex);

class SquareThread extends Thread {

int number;
SquareThread(int randomNumbern) {

number = randomNumbern;

public void run() {

System.out.println("Square of " + number + " = " + (number * number));

class CubeThread extends Thread {

int number;

CubeThread(int randomNumber) {

number = randomNumber;

public void run() {

System.out.println("Cube of " + number + " = " + number * number *


number);

public class MultiThreadingTest {

public static void main(String args[]) {

RandomNumberThread rnThread = new RandomNumberThread();

rnThread.start();

}
EX.NO.: 8
WRITE A PROGRAM TO PERFORM FILE OPERATIONS
AIM:
To write a java program to copy the contents of one file to another file using file
operations.

ALGORITHM:

Step 1: Start the program.

Step 2: Create a class FileCopy. Get the source and destination file names from the user.
Step 3:Create object of FileInputStream by passing the source file name.
Step 4:Using read() method read the contents of the file till end of the file.
Step 5:Create object of FileOutputStream by passing the second file to the constructor.
Step 6:Use write() method to write the contents to the destination file.
Step 7:Close the file input and output stream using close() method.
Step 8:Display the contents of both files.
Step 9: Stop the program.

PROGRAM

PackagejavaTpoint.javacodes;

import java.io.*;

importjava.util.*;

// create FileExample class to copy data of one file into another

publicclassFileExample {

// create coptData() method that copy data of file1 into file2

publicstaticvoidcopyData(File file1, File file2) throws Exception

// create instances of FileInputStream and FileOutputStream classes for file1 and file2

FileInputStreaminputStream = newFileInputStream(file1);

FileOutputStreamoutputStream = newFileOutputStream(file2);
// use try-catch-finally block to read and write bytes data into file

try {

// declare variable for indexing

inti;

// use while loop with read() method of FileInputStream class to read bytes data
from file1

while ((i = inputStream.read()) != -1) {

// use write() method of FileOutputStream class to write the byte data into file2

outputStream.write(i);

// catch block to handle exceptions

catch(Exception e) {

System.out.println("Error Found: "+e.getMessage());

// use finally to close instance of the FileInputStream and FileOutputStream classes

finally {

if (inputStream != null) {

// use close() method of FileInputStream class to close the stream

inputStream.close();

// use close() method of FileOutputStream class to close the stream

if (outputStream != null) {

outputStream.close();

}
}

System.out.println("File Copied");

// main() method start

publicstaticvoid main(String[] args) throws Exception

// create scanner class object to take file name from user

Scanner sc = newScanner(System.in);

// get the file from which the data would be copied.

System.out.println("Enter the name of the file from where the data would be
copied :");

String file1 = sc.nextLine();

// create instance of the File class for the source file

File a = newFile("C:\\Users\\pc\\OneDrive\\Desktop\\"+file1);

// get the file in which the data would be written.

System.out.println("Enter the name of the file from where the data would be
written :");

String file2 = sc.nextLine();

// create instance of the File class for the destination file

File b = newFile("C:\\Users\\pc\\OneDrive\\Desktop\\"+file2);

sc.close();

// method called to copy the data from file a to file b

copyData(a, b);

}
10.a.DEVELOP APPLICATIONS USING JAVAFX CONTROLS

import javafx.application.Application;

import static javafx.application.Application.launch;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.RadioButton;

import javafx.scene.control.ToggleGroup;

import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MCTest extends Application {

@Override

public void start(Stage primaryStage) {

primaryStage.setTitle("Test Question 1");

Label labelfirst= new Label("What is 10 + 20?");

Label labelresponse= new Label();

Button button= new Button("Submit");

RadioButton radio1, radio2, radio3, radio4;

radio1=new RadioButton("10");

radio2= new RadioButton("20");

radio3= new RadioButton("30");

radio4= new RadioButton("40");

ToggleGroup question1= new ToggleGroup();

radio1.setToggleGroup(question1);

radio2.setToggleGroup(question1);

radio3.setToggleGroup(question1);
radio4.setToggleGroup(question1);

button.setDisable(true);

radio1.setOnAction(e -> button.setDisable(false) );

radio2.setOnAction(e -> button.setDisable(false) );

radio3.setOnAction(e -> button.setDisable(false) );

radio4.setOnAction(e -> button.setDisable(false) );

button.setOnAction(e ->

if (radio3.isSelected())

labelresponse.setText("Correct answer");

button.setDisable(true);

else

labelresponse.setText("Wrong answer");

button.setDisable(true);

);

VBox layout= new VBox(5);


layout.getChildren().addAll(labelfirst, radio1, radio2, radio3, radio4, button, labelresponse);

Scene scene1= new Scene(layout, 400, 250);

primaryStage.setScene(scene1);

primaryStage.show();

public static void main(String[] args) {

launch(args);

10.bDEVELOP APPLICATIONS USING LAYOUTS AND MENUS

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.stage.Stage;

import javafx.scene.Scene;

import javafx.scene.control.Label;

import javafx.scene.control.Menu;

import javafx.scene.control.MenuBar;

import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;

public class MenuUI extends Application { @Override

public void start(Stage primaryStage) throws Exception

Menu newmenu = new


Menu("File"); Menu
newmenu2 = new
Menu("Edit"); MenuItem m1
= new MenuItem("Open");
MenuItem m2 = new
MenuItem("Save"); MenuItem
m3 = new MenuItem("Exit");
MenuItem m4 = new
MenuItem("Cut"); MenuItem
m5 = new MenuItem("Copy");
MenuItem m6 = new
MenuItem("Paste");
newmenu.getItems().add(m1);
newmenu.getItems().add(m2);
newmenu.getItems().add(m3);
newmenu2.getItems().add(m4);
newmenu2.getItems().add(m5);
newmenu2.getItems().add(m6);
MenuBar newmb = new MenuBar();
newmb.getMenus().add(newmenu);
newmb.getMenus().add(newmenu2);
Label l = new Label("\t\t\t\t\t\t + "You have selected no menu
items"); EventHandler<ActionEvent> event = new
EventHandler<ActionEvent>() { public void handle(ActionEvent e)
{

l.setText("\t\t\t\t\t\t" +
((MenuItem)e.getSource()).getT
ext() + " menu item selected");
}

};

m1.setOnAction(event);

m2.setOnAction(event);

m3.setOnAction(event);

m4.setOnAction(event);

m5.setOnAction(event);

m6.setOnAction(event);

VBox box = new VBox(newmb,l);

Scene scene = new Scene(box,400,200);

primaryStage.setScene(scene);

primaryStage.setTitle("JavaFX Menu");

primaryStage.show();

public static void main(String[] args)

Application.launch(args);

DEVELOP A MINI PROJECT FOR ANY APPLICATION USING JAVA CONCEPTS (LIBRARY
MANAGEMENT SYSTEM)

package library;

import javax.swing.JOptionPane;

public class Login extends javax.swing.JFrame {

public Login() {

initComponents();

}
@SuppressWarnings("unchecked")

private void initComponents() {

jLabel1 = new javax.swing.JLabel();

jPanel1 = new javax.swing.JPanel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

txtusername = new javax.swing.JTextField();

txtpass = new javax.swing.JPasswordField();

jButton2 = new javax.swing.JButton();

jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Segoe UI", 1, 36));

jLabel1.setText("Library Login");

jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Login"));

jLabel2.setText("Username");

jLabel3.setText("Password");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

jPanel1.setLayout(jPanel1Layout);

jPanel1Layout.setHorizontalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(48, 48, 48)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)

.addComponent(jLabel2)

.addComponent(jLabel3))

.addGap(51, 51, 51)


.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G, false)

.addGroup(jPanel1Layout.createSequentialGroup()

.addGap(4, 4, 4)

.addComponent(txtpass))

.addComponent(txtusername, javax.swing.GroupLayout.PREFERRED_SIZE, 161,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(114, Short.MAX_VALUE))

);

jPanel1Layout.setVerticalGroup(

jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()

.addGap(33, 33, 33)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)

.addComponent(jLabel2)

.addComponent(txtusername, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(43, 43, 43)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G)

.addComponent(jLabel3)

.addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(31, Short.MAX_VALUE))

);

jButton2.setText("Cancel");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {


jButton2ActionPerformed(evt);

});

jButton1.setText("Login");

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

.addGap(31, 31, 31)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel1)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addGroup(layout.createSequentialGroup()

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 126,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(4, 4, 4))

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

);
layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap(18, Short.MAX_VALUE)

.addComponent(jLabel1)

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()

.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(64, 64, 64))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 45,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 45,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap())))

);

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


FIRST:event_jButton1ActionPerformed

String username = txtusername.getText();

String pass = txtpass.getText();

if(username.equals("Peter") && pass.equals("123"))

Main m = new Main();

this.hide();

m.setVisible(true);
}

else

JOptionPane.showMessageDialog(this,"Username and Password do not match");

txtusername.setText("");

txtpass.setText("");

txtusername.requestFocus();

Category.java

package library;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.Vector;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JOptionPane;

import javax.swing.table.DefaultTableModel;

public class category extends javax.swing.JFrame {

public category() {

initComponents();
Connect();

table_update();

Connection con;

PreparedStatement pst;

private void initComponents() {

jPanel2 = new javax.swing.JPanel();

jLabel9 = new javax.swing.JLabel();

jLabel10 = new javax.swing.JLabel();

txtcat = new javax.swing.JTextField();

txtstatus = new javax.swing.JComboBox<>();

jButton1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jButton3 = new javax.swing.JButton();

jScrollPane1 = new javax.swing.JScrollPane();

jTable1 = new javax.swing.JTable();

jButton4 = new javax.swing.JButton();

jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Category",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14)));

jLabel9.setText("Category");

jLabel10.setText("Status");

txtstatus.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Active",


"DeActive" }));

jButton1.setText("Add");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {


jButton1ActionPerformed(evt);

});

jButton2.setText("Edit");

jButton2.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton2ActionPerformed(evt);

});

jButton3.setText("Delete");

jButton3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton3ActionPerformed(evt);

});

jTable1.setModel(new javax.swing.table.DefaultTableModel(

new Object [][] {

},

new String [] {

"Id", "Category", "Status"

){

Class[] types = new Class [] {

java.lang.Integer.class, java.lang.String.class, java.lang.String.class

};

public Class getColumnClass(int columnIndex) {

return types [columnIndex];


}

});

jTable1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

jTable1.setGridColor(new java.awt.Color(255, 255, 255));

jTable1.addMouseListener(new java.awt.event.MouseAdapter() {

public void mouseClicked(java.awt.event.MouseEvent evt) {

jTable1MouseClicked(evt);

});

jScrollPane1.setViewportView(jTable1);

jButton4.setText("Cancel");

jButton4.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton4ActionPerformed(evt);

});

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);

jPanel2.setLayout(jPanel2Layout);

jPanel2Layout.setHorizontalGroup(

jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(jPanel2Layout.createSequentialGroup()

.addGap(26, 26, 26)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G)

.addGroup(jPanel2Layout.createSequentialGroup()

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING)

.addComponent(jLabel9)
.addComponent(jLabel10))

.addGap(35, 35, 35)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING)

.addComponent(txtstatus, javax.swing.GroupLayout.PREFERRED_SIZE, 153,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(txtcat, javax.swing.GroupLayout.PREFERRED_SIZE, 153,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGroup(jPanel2Layout.createSequentialGroup()

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING)

.addGroup(jPanel2Layout.createSequentialGroup()

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 73,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 83,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 104,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(8, 8, 8)))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73,
Short.MAX_VALUE)

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


javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

jPanel2Layout.setVerticalGroup(

jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()

.addGap(21, 21, 21)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)

.addComponent(txtcat, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel9))

.addGap(27, 27, 27)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)

.addComponent(txtstatus, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel10))

.addGap(44, 44, 44)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)

.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 39,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(32, 32, 32)

.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 42,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addGroup(jPanel2Layout.createSequentialGroup()

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


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(0, 30, Short.MAX_VALUE))


);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36));

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

.addContainerGap()

.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGroup(layout.createSequentialGroup()

.addGap(287, 287, 287)

.addComponent(jLabel1)))

.addContainerGap(23, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addGap(27, 27, 27)

.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43,
Short.MAX_VALUE)

.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addContainerGap())

);

public void Connect()


{

try {

Class.forName("com.mysql.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql://localhost/slibr","root","");

} catch (ClassNotFoundException ex) {

Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);

} catch (SQLException ex) {

Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);

private void table_update()

int c;

try {

pst = con.prepareStatement("select * from category");

ResultSet rs = pst.executeQuery();

ResultSetMetaData rsd = rs.getMetaData();

c = rsd.getColumnCount()

DefaultTableModel d = (DefaultTableModel)jTable1.getModel();

d.setRowCount(0);

while(rs.next())

Vector v2 = new Vector();

for(int i=1; i<=c; i++)

v2.add(rs.getString("id"));

v2.add(rs.getString("category"));

v2.add(rs.getString("status"));
}

d.addRow(v2);

} catch (SQLException ex) {

Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);

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

String category = txtcat.getText();

String status = txtstatus.getSelectedItem().toString();

try {

pst = con.prepareStatement("insert into category(category,status)values(?,?) ");

pst.setString(1, category);

pst.setString(2, status);

pst.executeUpdate();

JOptionPane.showMessageDialog(null,"Category Adddeddd");

table_update();

txtcat.setText("");

txtstatus.setSelectedIndex(-1);

txtcat.requestFocus();

} catch (SQLException ex) {

Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);

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

DefaultTableModel d1 = (DefaultTableModel)jTable1.getModel();

int selectIndex = jTable1.getSelectedRow();

int id = Integer.parseInt(d1.getValueAt(selectIndex, 0).toString());


String category = txtcat.getText();

String status = txtstatus.getSelectedItem().toString();

try {

pst = con.prepareStatement("update category set category=?,status=? where id= ?");

pst.setString(1, category);

pst.setString(2, status);

pst.setInt(3, id);

pst.executeUpdate();

JOptionPane.showMessageDialog(null,"Category Updateddd");

table_update();

txtcat.setText("");

txtstatus.setSelectedIndex(-1);

txtcat.requestFocus();

jButton1.setEnabled(true);

catch (SQLException ex) {

Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_jButton3ActionPerformed

DefaultTableModel d1 = (DefaultTableModel)jTable1.getModel();

int selectIndex = jTable1.getSelectedRow();

int id = Integer.parseInt(d1.getValueAt(selectIndex, 0).toString());

int dialogResult = JOptionPane.showConfirmDialog(null, "Do you want to Delete the


Record","Warning",JOptionPane.YES_NO_OPTION);

if(dialogResult == JOptionPane.YES_OPTION)

try {
pst = con.prepareStatement("delete from category where id =?");

pst.setInt(1, id);

pst.executeUpdate();

JOptionPane.showMessageDialog(null,"Category Deletedd");

table_update();

txtcat.setText("");

txtstatus.setSelectedIndex(-1);

txtcat.requestFocus();

} catch (SQLException ex) {

Logger.getLogger(category.class.getName()).log(Level.SEVERE, null, ex);

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {

DefaultTableModel d1 = (DefaultTableModel)jTable1.getModel();

int selectIndex = jTable1.getSelectedRow();

txtcat.setText(d1.getValueAt(selectIndex, 1).toString());

txtstatus.setSelectedItem(d1.getValueAt(selectIndex, 2).toString());

jButton1.setEnabled(false);

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-


FIRST:event_jButton4ActionPerformed

this.setVisible(false);

public static void main(String args[]) {

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(category.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(category.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(category.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(category.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new category().setVisible(true);

});

You might also like