Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Java Assignment#27

1.What are different steps involved in reading the data from database.
A. 1.Driver
2.Establish the Connection
3.Creating the statement
4.Result set
5.Iterating the result

2.Connect any database(Mysql which is installed in your machine) using jdbc and
read the data from any table by running the query.
A. import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ReadingSqlDatabase{
public static void main(String[] args) throws IO
Exception,classNotFoundException,SQLException {
readFromMySql();
}
public static void readFromMySql() throws ClassNotFoundException {
class.forName("com.mysql.cj.jdbc.Driver");
Connection
link=DriverManager.getConnection("jdbc:mysql://localhost:3307/tabelist","root","pas
sword");
Statement set= link.createStatemnt();
ResultSet output= stmt.executeQuery("select * from employmentdetails");
while(output.next()){
String name=result.getString(1);
System.out.println(name);
}
}
}
3.Read the data from table and iterate them to print into the console.
A. import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ReadingSqlDatabase{
public static void main(String[] args) throws IO
Exception,classNotFoundException,SQLException {
readFromMySql();
}
public static void readFromMySql() throws ClassNotFoundException {
class.forName("com.mysql.cj.jdbc.Driver");
Connection
link=DriverManager.getConnection("jdbc:mysql://localhost:3307/tabelist","root","pas
sword");
Statement set= link.createStatemnt();
ResultSet output= stmt.executeQuery("select * from Companydetails");
while(output.next()){
int salary =result.getin(1);
System.out.println(salary);
}
}
}
4.How to write the data to a file in java? List out all classes to write the data
in java?
A. 1. FileWriter

2. BufferedWriter

5.What is user defined exceptions/custom exceptions in java?


A. In Java, user-defined exceptions, also known as custom exceptions, allow us to
create their own exception types to handle specific scenarios that aren't
adequately covered by built-in exceptions.

6.Write the simple program to demonstrate the custom exceptions in java?


A.
import java.util.Scanner;
public class FailedExamException extends Exception {
public FailedExamException() {
}

public FailedExamException(String message) {


super(message);
}
}

public class StudentExam {


int marks;
public StudentExam(int marks) {
this.marks = marks;
}

public void checkResult() throws FailedExamException {


int passingMarks = 40;
if (marks < passingMarks) {
throw new FailedExamException("Student has failed with");
}

System.out.println("Student passed the exam with");


}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter marks of student : ");


int marks1 = scanner.nextInt();
StudentExam student = new StudentExam(marks1);

try {
student.checkResult();
}
catch (FailedExamException e) {
System.out.println("FailedExamException occurred");
}

}
}

You might also like