Professional Documents
Culture Documents
Java Assignment
Java Assignment
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
try {
student.checkResult();
}
catch (FailedExamException e) {
System.out.println("FailedExamException occurred");
}
}
}