In: Computer Science
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
Answer : Given data
import java.sql.*;
public class Selectdb {
public static void main(String[] args) throws ClassNotFoundException,SQLException{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
PreparedStatement
ps=con.prepareStatement("select *from Registration ");
ResultSet
rs=ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
con.close();
} //close of try
catch(SQLException se){ System.out.println("Sql exception generated "); }
catch(Exception e){ System.out.println("error in class.forName loading "); }
}
}
note:- inside while loop i have used rs.getString upto 3 column. check your registraiton table and continue upto the number of tables in registration table.
___________THE END___________