Sunday, July 15, 2018

Solution of Today's assignment


This code is implemented and provied by Dheeraj Jain from TMU, Moradabad.


Note: Before using this code do following tasks

1. create a database, name of database must be school
2. create a table inside this database, name of table must be student
3. link mysqlconnector.jar file with application using the build-path 

import java.sql.*;

import java.util.Scanner;

public class Student
{
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"
       static final String DB_URL = "jdbc:mysql://localhost:3306/School";
      
       static final String USER = "root";
       static final String PASS = "";
      
        public static void main(String[] args)
        {
               Connection conn = null;
               Statement stmt = null;
               
               int p,c,m,e,h,tot,roll;
               float per;
               String name,gra,rem;
               
              Scanner sc=new Scanner(System.in);
              Scanner sc1=new Scanner(System.in);
             
              String ch="yes";
              while(ch.equalsIgnoreCase("yes"))
              {
                     System.out.println("Enter ur Roll Number " );
                     roll=sc.nextInt();
                    
                     System.out.println("Enter ur Name : ");
                     name=sc1.nextLine();
                    
                     System.out.println("Enter ur Marks in physics " );
                     p=sc.nextInt();
                    
                     System.out.println("Enter ur Marks in chemistry " );
                     c=sc.nextInt();
                    
                     System.out.println("Enter ur Marks in maths " );
                     m=sc.nextInt();
                    
                     System.out.println("Enter ur Marks in english " );
                     e=sc.nextInt();
                    
                     System.out.println("Enter ur Marks in hindi " );
                     h=sc.nextInt();
                    
                     if(p>100||p<0)
                           p=0;
                    
                     if(c>100||c<0)
                           c=0;
                          
                     if(m>100||m<0)
                           m=0;
                          
                     if(h>100||h<0)
                           h=0;
                          
                     if(e>100||e<0)
                           e=0;
                    
                     tot=p+m+c+e+h;
                    
                     per=(float)tot/5;
                    
                     if(per<=40.0)
                     {
                           gra="F";
                     }
                     else if(per>=41.0&&per<=50.0)
                     {
                           gra="E";
                     }
                     else if(per>=51.0&&per<=60.0)
                     {
                           gra="D";
                     }
                     else if(per>=61.0&&per<=70.0)
                     {
                           gra="C";
                     }
                     else if(per>=71.0&&per<=80.0)
                     {
                           gra="B";
                     }
                     else if(per>=81.0&&per<=90.0)
                     {
                           gra="A";
                     }
                     else
                     {
                           gra="A+";
                     }
                    
                 try{
                    Class.forName("com.mysql.jdbc.Driver");

                    System.out.println("Connecting to a selected database...");
                  
                    conn = DriverManager.getConnection(DB_URL, USER, PASS);
                    System.out.println("Connected database successfully...");

                    System.out.println("Inserting Record in given database...");
                    stmt = conn.createStatement();
                   
                    String sql = "Insert into Student values ('"+roll+"','"+name+"','"+p+"','"+c+"','"+m+"','"+h+"','"+e+"','"+tot+"','"+per+"','"+gra+"')";

                    stmt.executeUpdate(sql);
                    System.out.println("Record inserted in given database...");
                   
                  
                   
                 }catch(Exception se){
                    System.out.println("Error : "+se);
                 }
                 System.out.println("Do you want to enter more records(yes||no) : ");
                 ch=sc1.nextLine();
              }
              System.out.println("Thank you");
              }//end main
}



Note: save this code inside Student.java file

import java.sql.*;
import java.util.Scanner;

public class Student
{
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
       static final String DB_URL = "jdbc:mysql://localhost:3306/School";
      
       static final String USER = "root";
       static final String PASS = "";
      
        public static void main(String[] args)
        {
               Connection conn = null;
               Statement stmt = null;
               
               int p,c,m,e,h,tot,roll;
               float per;
               String name,gra,rem;
               
              Scanner sc=new Scanner(System.in);
              Scanner sc1=new Scanner(System.in);
             
              String ch="yes";
              while(ch.equalsIgnoreCase("yes"))
              {
                     System.out.println("Enter ur Roll Number " );
                     roll=sc.nextInt();
                    
                     System.out.println("Enter ur Name : ");
                     name=sc1.nextLine();
                    
                     System.out.println("Enter ur Marks in physics " );
                     p=sc.nextInt();
                    
                     System.out.println("Enter ur Marks in chemistry " );
                     c=sc.nextInt();
                    
                     System.out.println("Enter ur Marks in maths " );
                     m=sc.nextInt();
                    
                     System.out.println("Enter ur Marks in english " );
                     e=sc.nextInt();
                    
                     System.out.println("Enter ur Marks in hindi " );
                     h=sc.nextInt();
                    
                     if(p>100||p<0)
                           p=0;
                    
                     if(c>100||c<0)
                           c=0;
                          
                     if(m>100||m<0)
                           m=0;
                          
                     if(h>100||h<0)
                           h=0;
                          
                     if(e>100||e<0)
                           e=0;
                    
                     tot=p+m+c+e+h;
                    
                     per=(float)tot/5;
                    
                     if(per<=40.0)
                     {
                           gra="F";
                     }
                     else if(per>=41.0&&per<=50.0)
                     {
                           gra="E";
                     }
                     else if(per>=51.0&&per<=60.0)
                     {
                           gra="D";
                     }
                     else if(per>=61.0&&per<=70.0)
                     {
                           gra="C";
                     }
                     else if(per>=71.0&&per<=80.0)
                     {
                           gra="B";
                     }
                     else if(per>=81.0&&per<=90.0)
                     {
                           gra="A";
                     }
                     else
                     {
                           gra="A+";
                     }
                    
                 try{
                    Class.forName("com.mysql.jdbc.Driver");

                    System.out.println("Connecting to a selected database...");
                  
                    conn = DriverManager.getConnection(DB_URL, USER, PASS);
                    System.out.println("Connected database successfully...");

                    System.out.println("Inserting Record in given database...");
                    stmt = conn.createStatement();
                   
                    String sql = "Insert into Student values ('"+roll+"','"+name+"','"+p+"','"+c+"','"+m+"','"+h+"','"+e+"','"+tot+"','"+per+"','"+gra+"')";

                    stmt.executeUpdate(sql);
                    System.out.println("Record inserted in given database...");
                   
                  
                   
                 }catch(Exception se){
                    System.out.println("Error : "+se);
                 }
                 System.out.println("Do you want to enter more records(yes||no) : ");
                 ch=sc1.nextLine();
              }
              System.out.println("Thank you");
              }//end main
}


No comments:

Post a Comment