Monday, February 3, 2025

To Connect Java program with the MYSQL Database


To connect java application with the MYSQL database, first of all to install MYSQL database in your machine.

To install MYSQL in your machine then follow the below steps:

1) To download MYSQL database on its official website : https://dev.mysql.com/downloads/installer/

2) After that double click on this exe file and run the installer. Choose Full setup option and click next.

3) Execute the entire file and wait to complete to install.

4) Next to check product configuration.

5) In this step choose port number. Default port number is 3306. If it was already acquired by other application like WAMP server or apache server, then change its value like 3307 or 3308.

6)  After that select authentication method. Select recommended method.

7) Choose password and user account as per your choice.

8) Follow next step and finish the installation process.


Watch this video to install MYSQL installation process:  https://youtu.be/QqFWLDSgedg?si=4yvPUXTUQI0DRX2P

 

To connect java application with the MYSQL Database:

1) Create database in MYSQL database using the following SQL Query. 

2) Write JAVA application to connect MYSQL Database and save using “MyDatabase.java” name.

import java.sql.*; 

class MyDatabase{ 

        public static void main(String args[]){ 

            try{

                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3309/emp?autoReconnect=true&useSSL=false", "root", "root"); 

                //here emp is database name, root is username and password

                if(con!= null){

                    Statement stmt=con.createStatement(); 

                    ResultSet rs=stmt.executeQuery("select * from employee"); 

                    while(rs.next()) 

                        System.out.println(rs.getInt(1) +"  "+ rs.getString(2) +"  "+ rs.getInt(3)); 

                }

                con.close();

        }

        catch(Exception e){

            System.out.println(e);

        }

    

}

 3) Compile this java program using : javac MyDatabase.java



4) After that run this program with the JDBC connector file download from  https://dev.mysql.com/downloads/connector/j/

    To run this file using : java -cp mysql-connector-java-8.0.11.jar;. MyDatabase


All right. Your database is connected successfully. If your like this post then share your friends.


Saturday, October 19, 2024

SYBSc (CS) Sem III : Data Structure Slip 20 Que - 2

  SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q2. There are lists where new elements are always appended at the end of the list. The list can be implemented as a circular list with the external pointer pointing to the last element of the list. Implement singly linked circular list of integers with append and display operations. The operation append(L, n), appends to the end of the list, n integers either accepted from user or randomly generated.*/

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

struct node *next;

};

struct node * create(struct node *head)

{

struct node *newnode = NULL, *temp = NULL;

newnode = (struct node*) malloc(sizeof(struct node));

if(newnode!=NULL)

{

    printf("Enter node data");

    scanf("%d",&(newnode->data));

    newnode->next = NULL;

   

    if(head == NULL)

        {

            head = newnode;

            newnode->next = head;

        }

        else

        {   

        temp = head;

       

        while(temp->next != head)

        {

                temp = temp->next;

            }

            temp->next = newnode;

            newnode->next = head;

         }

     }  

     return head;

}


struct node * append(struct node *head, int n)

{

struct node *newnode = NULL, *temp = NULL;

newnode = (struct node*) malloc(sizeof(struct node));

newnode->data = n;

newnode->next = NULL;

temp = head;

while(temp->next != head)

{

        temp = temp->next;

    }

    temp->next = newnode;

    newnode->next = head;    

    return head;

}


void display(struct node *head)

{

     struct node *temp = head; 

     do

    {

          printf("%d\t", temp->data);

          temp = temp->next;

    }while(temp != head);

}

int main()

{

int num, i;

struct node *head = NULL;

        printf("Enter the number of nodes in list: ");

        scanf("%d", &num);

   

for(i = 0; i < num; i++)

      head = create(head);

 

        display(head);


printf("\n enter data to be append:");

scanf("%d",&num);

head = append(head, num);

display(head);

system("pause");

return 0;

}


Tuesday, October 15, 2024

SYBSc (CS) Sem III : Data Structure Slip 14 Que - 2

 SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q. 2 A doubly ended queue allows additions and deletions from both the ends that 

is front and rear. Initially additions from the front will not be possible. To avoid

this situation, the array can be treated as if it were circular. Implement a 

queue library (dstqueue.h) of integers using a static implementation of the 

circular queue and implementing the following operations. 

a. isFull(Q)

b. addFront(Q)

c. getRear(Q)

d. deleteRear(Q)

*/

// File name : "dstqueue.c"

#include"dstqueue.h"

int init(struct Queue *q)

{

    q->front = q->rear = -1;

}

int isEmpty(struct Queue *q)

{

    if(q->front == q->rear)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int isFull(struct Queue *q)

{

    if(q->rear == MAX-1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int addFront(struct Queue *q, int x)

{

    if(isFull(q) == 1)

    {

       printf("The queue is overflow...");

       return 1;

    }

    else{

         q->data[++(q->front)] = x;

    }    

    return 0;

}

int getRear(struct Queue *q)

{

    if(isEmpty(q) == 1)

    {

       printf("The stack is underflow...");

       return 1;

    }

    else{

         return q->data[q->rear];

    }

}

int deleteRear(struct Queue *q)

{

    if(isEmpty(q) == 1)

    {

       printf("The stack is underflow...");

       return 1;

    }

    else{

         return q->data[++(q->rear)];

    }

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "dstqueue.h"

#include<stdio.h>
#include<stdlib.h>
#define MAX 30

struct Queue
{
        int data[MAX];
        int front, rear;
};

int init(struct Queue *Q);
int isEmpty(struct Queue *Q);
int isFull(struct Queue *q);
int addFront(struct Queue *Q, int x);
int getRear(struct Queue *Q);
int deleteRear(struct Queue *Q);


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "main.c"

#include"dstqueue.h"

int main()
{
    struct Queue q;
    
    int ch, num;
    
    init(&q);
    
    do{
       printf("\n1. addFront \n2. getRear \n3. deleteRear \n4. Exit \n");
       scanf("%d", &ch);
       
       switch(ch)
       {
            case 1: 
                 printf("Enter the element to insert into queue: ");
                 scanf("%d", &num);
                 
                 addFront(&q, num);                 
                 break;
                 
            case 2:
                 printf("The current element : %d", getRear(&q));
                 break;
            
            case 3:
                 printf("The deleted element : %d", deleteRear(&q));
                 break;
            default:
                 printf("Thank you visit again ....\n\n");
                 break;          
       }
    
    }while(ch != 4);
    
    system("PAUSE");
    return 0;
}

SYBSc (CS) Sem III : Data Structure Slip 11 Que - 2

   SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q. 2 Implement a priority queue library (Priority.h) of integers using a static implementation of the queue and implementing the below two operations. Write a driver program that includes queue library and calls different queue operations.

1) Add an element with its priority into the queue.

2) Delete an element from queue according to its priority. */

// File name : "PriorityQ.c"

#include"PriorityQ.h"

int initPQ(struct PriorityQueue *pq)

{

    pq->front = pq->rear = -1;

    return 0;

}

int isFullPQ(struct PriorityQueue *pq)

{

    if(pq->rear == MAX-1)

        return 1;

    else

        return 0;

}

int isEmptyPQ(struct PriorityQueue *pq)

{

    if(pq->front == -1)

        return 1;

    else

        return 0;

}

void InsertPQ(struct PriorityQueue *pq, struct data dt)

{

    struct data temp;

    int i, j;    

    pq->rear++;

    pq->d[pq->rear] = dt;

    

    if(pq->front == -1)

            pq->front = 0;    

    for( i = pq->front; i <= pq->rear; i++)

    {

          for( j = i+1; j <= pq->rear; j++ )

          {

               if( pq->d[i].priority > pq->d[j].priority )

               {

                   temp = pq->d[i];

                   pq->d[i] = pq->d[j];

                   pq->d[j] = temp;

               }

               else

               {

                   if( pq->d[i].priority == pq->d[j].priority )

                   {

                       if( pq->d[i].order > pq->d[j].order )

                       {

                            temp = pq->d[i];

                            pq->d[i] = pq->d[j];

                            pq->d[j] = temp;

                       }

                   }

               }

          }

    }    

}

struct data DeletePQ(struct PriorityQueue *pq)

{

    struct data t = pq->d[pq->front];   

    if( pq->front == pq->rear )

        pq->front = pq->rear = -1;

    else

        pq->front++;        

    return t;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "PriorityQ.h"

#include<stdio.h>
#include<stdlib.h>
#define MAX 10

struct data
{
       int item;
       int priority;
       int order;
};

struct PriorityQueue
{
       struct data d[MAX];
       int front;
       int rear;
};

int initPQ(struct PriorityQueue *pq);
int isFullPQ(struct PriorityQueue *pq);
int isEmptyPQ(struct PriorityQueue *pq);
void InsertPQ(struct PriorityQueue *pq, struct data dt);
struct data DeletePQ(struct PriorityQueue *pq);


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "main.c"

#include"PriorityQ.h"

int main(int argc, char *argv[])
{
    struct PriorityQueue q;
    struct data temp;
    
    int i = 0, ch;
        
    initPQ(&q);
    
    do{
       printf("\n1. Insert \n2. Delete \n3. Exit \n");
       scanf("%d", &ch);
       
       switch(ch)
       {
            case 1:
                 if(isFullPQ(&q))
                     printf("\n Queue is full.");
                 else
                 {
                     printf("Enter the data and Priority : ");
                     printf("\nData \t Priority\n");
                     scanf("%d%d", &(temp.item), &(temp.priority));
                     temp.order = i++;             
                     InsertPQ(&q, temp);
                 } 
                 break;
                 
            case 2:
                 if(isEmptyPQ(&q))
                    printf("\n Queue is Empty");
                 else
                 {
                     temp = DeletePQ(&q);
                     printf("Data = %d \t Priority  = %d \n", temp.item, temp.priority);
                 }
                 break;
            default:
                 printf("Thank you visit again ....\n\n");
                 break;          
       }
    
    }while(ch>0 && ch<3);
    
    system("PAUSE");
    return 0;
}

SYBSc (CS) Sem III : Data Structure Slip 21 Que - 2

  SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q. 2. Read the data from the file "employee.txt" and sort on names in alphabetical order (use strcmp) using insertion sort and selection sort */

// File Name: "employee.txt" (This file create separate with txt extension and save in current folder.)

John

Aniket

Julie

Dolly

Rosemary


// File Name: "Slip21_2.c"

#include<stdio.h>

#include<stdlib.h>

#define MAX 20


typedef struct employee{

        char name[MAX];

}record;

record emp[MAX];


int read_file(record *a){

    int i = 0;

    FILE *fp;   

    if ((fp = fopen("employee.txt", "r")) != NULL)

    {

            while (!feof(fp)){

                  fscanf(fp, "%s", a[i].name);

                  i++;

            }

    }    

    return (i-1);

}

void InsertionSort(record *a, int n)

{

    int i, j;    

    record elem;

    for (i = 1; i <= n; i++) 

   {        

        elem = a[i];        

        for (j = i-1; strcmp(a[j].name, elem.name) > 0 && j > 0; j--) 

       {

            a[j + 1] = a[j];  

        }        

        a[i] = elem; 

    }

}


void SelectionSort(int A[], int n)

{

     int i = 0, j = 0, loc = 0, min = 0, temp = 0;     

     for ( i = 0; i < n-1; i++)

     {

         min = A[i];

         loc = i;         

         for ( j = i+1; j < n; j++)

         {

             if ( min > A[j])

             {

                  min = A[j];

                  loc = j;

             }

         }         

         temp = A[i];         

         A[i] = A[loc];         

         A[loc] = temp;

     }

}

void display(record *a, int n)

{

    int i;

    printf("\n\n");

    for (i = 0; i < n; i++) {

        printf("%s ", a[i].name);

    }

    printf("\n\n");

}

int main()

{

   int n = read_file(emp);   

   printf("%d", n);   

   InsertionSort(emp, n);   

   display(emp, n);

   int n = read_file(emp);   

   printf("%d", n);   

   SelectionSort(emp, n);   

   display(emp, n);   

   system("pause");

   return 0;

}


SYBSc (CS) Sem III : Data Structure Slip 21 Que - 1

  SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q. 1. Write a program that reverses a string of characters. The function should use a stack library (cststack.h). Use a static implementation of the stack. */

/// File name : "cststack.h"

#include"cststack.h"

int init(struct Stack *s)

{

    s->top = -1;

}

int isEmpty(struct Stack *s)

{

    if(s->top == -1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int isFull(struct Stack *s)

{

    if(s->top == MAX-1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int Push(struct Stack *s, char x)

{

    if(isFull(s) == 1)

    {

       printf("The stack is overflow...");

       return 1;

    }

    else{

         s->top++;

         s->data[s->top] = x;

    }

    

    return 0;

}

char Pop(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

       printf("The stack is underflow...");

       return 1;

    }

    else{

         return s->data[(s->top)--];

    }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "cststack.h"

#include<stdio.h>
#include<stdlib.h>
#define MAX 30

struct Stack
{
       char data[MAX];
       int top;
};

int init(struct Stack *s);
int isEmpty(struct Stack *s);
int isFull(struct Stack *s);
int Push(struct Stack *s, char x);
char Pop(struct Stack *s);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "main.c"

#include"cststack.h"

int main(int argc, char *argv[])
{
    struct Stack s;
    char str[MAX];
    int i = 0;
    
    init(&s);
    
    printf("Enter the given String :: ");
    scanf("%s", str);
    
    while(str[i] != '\0')
    {
       Push(&s, str[i]);
       i++;
    }
    
    while(!isEmpty(&s))
    {
        printf("%c", Pop(&s));
    }
  
    system("PAUSE");
    return 0;
}

SYBSc (CS) Sem III : Data Structure Slip 17 Que - 2

  SAVITIBAI PHULE UNIVERSITY OF PUNE

S. Y. B.Sc. (Computer Science) Semester III

Practical Examination

      SUBJECT: CS-233 Practical course based on CS231 

/* Q. 2. Write a program that copies the contents of one stack into another. Use stack library to perform basic stack operations. The order of two stacks must be identical. (Hint: Use a temporary stack to preserve the order). */

/// File name : "stack.c"

#include"stack.h"

int init(struct Stack *s)

{

    s->top = -1;

}

int isEmpty(struct Stack *s)

{

    if(s->top == -1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int isFull(struct Stack *s)

{

    if(s->top == MAX-1)

    {

       return 1;

    }

    else{

       return 0;

    }

}

int Push(struct Stack *s, int n)

{

    if(isFull(s) == 1)

    {

       printf("The stack is overflow...");

       return 1;

    }

    else{

         s->top++;

         s->data[s->top] = n;

    }    

    return 0;

}

int Pop(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

       printf("The stack is underflow...");

       return 1;

    }

    else{

         return s->data[(s->top)--];

    }

}

int Peek(struct Stack *s)

{

    if(isEmpty(s) == 1)

    {

       printf("The stack is underflow...");

       return 1;

    }

    else{

         return s->data[s->top];

    }

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "stack.h"

#include<stdio.h>
#include<stdlib.h>
#define MAX 30

struct Stack
{
       int data[MAX];
       int top;
};

int init(struct Stack *s);
int isEmpty(struct Stack *s);
int isFull(struct Stack *s);
int Push(struct Stack *s, int n);
int Pop(struct Stack *s);
int Peek(struct Stack *s);


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File name : "main.c"

#include"stack.h"

int main(int argc, char *argv[])
{
    struct Stack s, s1, s2;
    int A[MAX], n = 0, i = 0, num = 0;
    
    init(&s);
    
    printf("How many number we have to enter : ");
    scanf("%d", &n);
    printf("Enter the stack elements: ");
    
    for (i = 0; i < n; i++)
    {
        scanf("%d", &num);
        Push(&s, num); 
    }
       
    for (i = 0; i < n; i++)
    {
        A[i] = Pop(&s);       
    }
    
    init(&s1);
        
    for (i = 0; i < n; i++)
    {
        Push(&s1, A[i]); 
    }
       
    for (i = 0; i < n; i++)
    {
        A[i] = Pop(&s1);       
    }
    
    init(&s2);
        
    for (i = 0; i < n; i++)
    {
        Push(&s2, A[i]); 
    }
       
    for (i = 0; i < n; i++)
    {
        printf("%d\t", Pop(&s2));       
    }
    
    system("PAUSE");
    return 0;
}

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...