Skip to main content

Write a Java program to accept the details of Teacher(tno, name, salary) from the user and store into the database table and update the salary of techer yo entered salary amount and tno.

Write a Java program to accept the details of Teacher(tno, name, salary) from the user and store into the database table and update the salary of techer yo entered salary amount and tno.



Program:
import java.io.*;
import java.sql.*;

class Teacher {
    public static void main(String args[]) throws SQLException, ClassNotFoundException, IOException {
        int tno, salary, choice, r;
        String name;
        Statement s;
        ResultSet rs;
        BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:teacherDSN");
        do {
            System.out.println("Menu");
            System.out.println("\n 1. Add Teacher \n 2. Update Teacher Salary");
            switch(choice) {
                case 1:
                    PreparedStatement ps = con.prepareStatement("insert into Teacher values(?,?,?)");
                    System.out.println("Enter Teacher No:");
                    tno = Integer.parseInt(br.readLine());
                    System.out.println("Enter Teacher Name:");
                    name = br.readLine();
                    System.out.println("Enter Teacher Salary:");
                    salary = Integer.parseInt(br.readLine());
                    ps.setInt(1, tno);
                    ps.setString(2, name);
                    ps.setInt(3, salary);
                    r = ps.executeUpdate();
                    if(r>0){
                        System.out.println("Record Saved.");
                    }
                    ps.close();
                    s = con.createStatement();
                    rs = s.executeQuery("select * from Teacher");
                    while(rs.next()){
                        System.out.println("Teacher = [No:"+rs.getInt(1)+", Name:"+rs.getString(2)+", Salary:"+rs.getInt(3)+"]\n");
                    }
                    rs.close();
                    s.close();
                    break;
                case 2:
                    
                    System.out.println("Enter tno to Update Salary:");
                    tno = Integer.parseInt(br.readLine());
                    System.out.println("Enter Teacher Salary:");
                    salary = Integer.parseInt(br.readLine());
                    s = con.createStatement();
                    r = s.executeUpdate("update Teacher set salary="+salary+" where tno="+tno);
                    if(r>0){
                        System.out.println("Record Updated Successfully");
                    }
                    s.close();
                    s = con.createStatement();
                    rs = s.executeQuery("select * from Teacher");
                    while(rs.next()){
                        System.out.println("Teacher = [No:"+rs.getInt(1)+", Name:"+rs.getString(2)+", Salary:"+rs.getInt(3)+"]\n");
                    }
                    rs.close();
                    s.close();
                    break;
                default: System.out.println("Invalid Choice.");
            }
        } while(choice<=2);
        con.close();
    }
}

Comments

Trending Posts

Write a Java program using servlet for email registration with userid, password, name, address fields and display the details on next page.

Write a Java program using servlet for email registration with userid, password, name, address fields and display the details on next page. Program: import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class User extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ resp.setContentType("Text/http:charset=UTF-8"); PrintWriter out = resp.getWriter(); out.print("<html>"); out.print("<head>"); out.print("<title>Today</title>"); out.print("</head>"); out.print("<body>"); out.print("<br>UserId="+resp.getParameter("userid")); out.print("<br>Password="+resp.getParameter("password")); out.print("<br>Name=&quo

Write a Java program to accept names of n students and insert into LinkedList. Display the contents of list using Iterator and also Display the content in reverse order using ListIterator.

Write a Java program to accept names of n students and insert into LinkedList. Display the contents of list using Iterator and also Display the content in reverse order using ListIterator. Program: import java.io.*; import java.util.*; class ReverseLinkedList { public static void main(String args[]) throws IOException { LinkedList students = new LinkedList(); String str; int i, n; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter how many names:"); n = Integer.parseInt(br.readLine()); for(i=0;i<n;i++){ System.out.println("Enter the names of students:"); str = br.readLine(); students.add(str); } Iterator itr = students.iterator(); System.out.println("Content of LinkedList using Iterator"); while(itr.hasNext()){ System.out.println(itr.next()); }

Write a Java program to read n integers into LinkedList collection. Do the following operations. Display only negative integers and delete last element.

Write a Java program to read n integers into LinkedList collection. Do the following operations. Display only negative integers and delete last element. Program: import java.io.*; import java.util.*; class LinkedListInt { public static void main(String args[]) throws IOException { LinkedList list = new LinkedList(); int i,n,x; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many integers:"); n = Integer.parseInt(br.readLine()); for(i=0;i<n;i++){ System.out.println("Enter Number:"); x = Integer.parseInt(br.readLine()); list.add(x); } Iterator itr = list.iterator(); while(itr.hasNext()){ Integer y = (Integer)itr.next(); int a = (int)y; if(a<0){ System.out.println("Negative number:"+a); } } System.out.pri

Write a Java program to accept the details of employee(eno, ename, salary) using AWT and insert into database

Write a Java program to accept the details of employee(eno, ename, salary) using AWT and insert into database. Program: import java.awt.*; import java.awt.event.*; import java.io.*; import java.sql.*; public class Employee extends Frame implements ActionListener { Label labelEno, labelEname, labelSalary; TextField textEno, textEname, textSalary; Button btnAdd; String sqlQuery; Statement s; Connection dbConnection; public Employee() throws ClassNotFoundException, SQLException { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); dbConnection = DriverManager.getConnection("jdbc.odbc:EmployeeDSN"); setLayout(new FlowLayout()); setSize(300,300); labelEno = new Label("Employee No:"); labelEname = new Label("Employee Name:"); labelSalary = new Label(Employee Salary:); textEno = new TextField(20); textEname = new TextField(20); textSalary = new TextField(20); btnAdd = new Button("Add"); btnAdd.addAction

Write simulation program for demand paging and show the page scheduling and total number of page faults according to LFU page replacement algorithm. Assume the memory of n frames.

Write simulation program for demand paging and show the page scheduling and total number of page faults according to LFU page replacement algorithm. Assume the memory of n frames. Program: #include <stdio.h> #include <stdlib.h> void main(){ setbuf(stdout,NULL); int f, tp ,al[50][50]={-1, -1}, i,j,pg[50],pf,x,queue[50]; printf("\nEnter total number of frames:"); scanf("%d",&f); printf("\nEnter total number of pages:"); scanf("%d",&tp); printf("\nEnter page string:"); for(i=0;i<tp;i++){ scanf("%d",&pg[i]); } pf=0; for(i=0;i<tp;i++){ for(j=0;j<f;j++){ if(al[j][i]==pg[i]){ break; } } if(f==j){ for(j=0;j<f;j++){ if(al[j][i]==-1){ al[j][i]=pg[i]; queue[++x]=pg[i]; pf++; break; } } if(f==j){ for(j=0;j<f;j++){ if(al[j][i]==queue[x]){ al[j][i]=pg[i]; pf++; x--; } } } } else{ queue[++