Java2 Exer
Java2 Exer
Question
Develop a Java program to connect with database and retrieve following details:
• Write a Java code to insert records by reading input from user.
• Display all the details which are stored in Nobel Prize table.
• Display the details of the Nobel Prize winners who got for the subject Peace.
• Delete the NobelPrize winners who got NobelPrize before 1930.
Aim
To write a java program to connect database and perform CRUD operations in NobelPrize table.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class noble_prize {
public static Connection getConnect() throws Exception
{
String url="jdbc:mysql://localhost:3306/noble";
String username="root";
String password="root";
Connection con=DriverManager.getConnection(url,username,password);
return con;
}
public static void create_table() throws Exception
{
Connection con=getConnect();
String query="create table noble_prize(Name varchar(50),Year int,Subject
varchar(50))";
Statement s=con.createStatement();
s.execute(query);
con.close();
}
public static void insert(String name,int year,String subject) throws Exception
{
Connection con=getConnect();
String query="insert into noble_prize values(?,?,?)";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1,name);
ps.setInt(2,year);
ps.setString(3,subject);
ps.execute();
con.close();
System.out.println("===============================================");
System.out.println("Name\t\t\tYear\t\tSubject");
System.out.println("===============================================");
Statement s=con.createStatement();
ResultSet rs=s.executeQuery(query);
while(rs.next())
{
System.out.println(rs.getString(1)+"\t\t"+rs.getInt(2)+"\t\t"+rs.getString(3));
}
System.out.println("===============================================");
con.close();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try {
create_table();
for(int i=0;i<5;i++) {
System.out.print("Enter name: ");
String s=sc.nextLine();
System.out.print("Enter year: ");
int n=sc.nextInt();
sc.nextLine();
System.out.print("Enter subject: ");
String sub=sc.nextLine();
insert(s,n,sub);
}
insert("Rabindhranath Tagore",1917,"Literature");
display();
Connection con=getConnect();
String query1="select * from noble_prize where Subject='Peace'";
Statement s=con.createStatement();
ResultSet rs=s.executeQuery(query1);
rs.next();
System.out.println("\nPeace winners: "+rs.getString(1)+" "+rs.getInt(2));
String query2="delete from noble_prize where Year<1930";
s.executeUpdate(query2);
display();
}
catch(Exception e)
{
e.printStackTrace();
}
}}
Result
Thus, the java program was executed successfully and the output was verified.
Question
Given the following table containing information about employees of an organization,
develop a small java application, using JDBC, which displays a menu for the user consisting
of following options: (Use Prepared Statement for Executing Queries)
1. Add Record 2. Modify Record 3. Delete Record
4. Display one Record 5. Display All 6. Exit
Aim
To write a java program to connect database and perform CRUD operations using prepared
statement.
Result
Thus, the java program was executed successfully and the output was verified.
Question
1) Develop a Java program to connect with database and retrieve following details: 1) Add a new book
‘HTML, CSS & JavaScript’ by Laura Lemay, Prentice Hall, Rs.250.00 using Prepared Statement.
2) Display all the details which are stored in Books table. 3) Create a Procedure to increase the price
of books by Rs.200. 4) Create a Procedure to add new record into table. 5) Execute procedures using
Callable Statement.
Aim
To write a java program to connect jdbc and perform CRUD operations using callable statement.
Result
Thus, the java program was executed successfully and the output was verified.
Question
Write a Java program to test the function using JUnit & check whether it is finding the Nth Fibonacci
number.Try to fix the logical errors if any.
Aim
To write a java program that checks Fibonacci number is found correctly.
Output
Result
Thus, the java program was executed successfully and the output was verified.
Question
Create the class and implement the method to check given string is a palindrome and return the
result. Create a Junit test class to test the above class.
Aim
To write a java program that checks palindrome string is found correctly..
Output
Result
Thus, the java program was executed successfully and the output was verified.
Question
Create the class and implement the method to find number of digits in a given string. Create a Junit
test class to test the above class.
Aim
To write a java program that checks number of digits is found correctly.
Result
Thus, the java program was executed successfully and the output was verified.
Question
Test the functions of Calculate class using JUnit. Print binary function is used to print the binary
equivalent of the given number. Note: Try to fix the logical errors in these functions.
Aim
To write a java program that checks the binary form of a number is found correctly.
import java.util.Arrays;
public class Calculate {
public byte add(byte b1, byte b2) {
return (byte)(b1+b2);
}public short add(short b1,short b2) {
return (short)(b1+b2);
}public int[] printBinary(byte number) {
int[] result;
if (number < 16) result = new int[4];
else if (number < 32) result = new int[5];
else if (number < 64) result = new int[6];
else result = new int[7];
for(int i = result.length - 1; number > 0; i--) {
result[i] = number % 2;
number = (byte) (number / 2);
}
return Arrays.copyOfRange(result, i + 1, result.length);
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
@RunWith(Parameterized.class)
public class CalculateTest {
byte input;
int[] expected;
Output:
Result
Thus, the java program was executed successfully and the output was verified.
Question
Create a test suite class for the above exercises 2 & 3 and test the methods.
Aim
To write a java program that test the program using suite method.
Output
Result
Thus, the java program was executed successfully and the output was verified.