JAVA 26
26 details of given employee && calculate sum of first and last
Q1]
importjava.sql.*;
class Slip27_1
{
public static void main(String a[])
{
Connection con;
PreparedStatementps;
ResultSetrs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:dsn");
if(con==null)
{
System.out.println("Connection Failed....");
System.exit(1);
}
System.out.println("Connection Established...");
ps=con.prepareStatement("select * from employee where eid=?");
int id = Integer.parseInt(a[0]);
ps.setInt(1,id);
rs=ps.executeQuery();
System.out.println("eno\t"+"ename\t"+"department\t"+"sal"); while(rs.next())
{
System.out.println("\n"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.ge tInt(4));
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OR
/*
Write a Java program to delete the details of given employee (ENo EName Salary).
Accept employee ID through command line. (Use PreparedStatement Interface)
*/
package com.mycompany.javaslip;
import java.sql.*;
public class slip26_1
{
public static void main(String[] args) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "postgres");
String sql = "delete from emp where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, Integer.parseInt(args[0]));
ps.executeUpdate();
}
}
Q2]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
String numberStr = request.getParameter("number");
if (numberStr == null || numberStr.isEmpty()) {
out.println("<p style=\"color: red;\">Please provide a number.</p>");
} else {
int number = Integer.parseInt(numberStr);
int lastDigit = number % 10;
int firstDigit = Character.getNumericValue(numberStr.charAt(0));
int sum = firstDigit + lastDigit;
out.println("<p style=\"color: red; font-size: 18px;\">Sum of first and last digit of " + number + " is: " + sum + "</p>");
}
%>
</body>
</html>