OCP - 4

NIO.2, JDBC

NIO.2, JDBC


Set of flashcards Details

Flashcards 66
Language English
Category Computer Science
Level Other
Created / Updated 30.04.2019 / 08.01.2020
Weblink
https://card2brain.ch/box/20190430_ocp_4
Embed
<iframe src="https://card2brain.ch/box/20190430_ocp_4/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

What are NIO Views for?

A view is a group of realte related attributes for a particular file system.

They increase performance by reading all metadata in one operation rather than accessing the fs multiple times.

How can you read all Metadata of a Path?

BasicFileAttributes data = Files.readAttributes(path, BasicFileAttributes.class)

How can you write Metadata of a Path?

BasicFileAttributeView data = Files.getFileAttributeView(path, BasicFileAttributeView.class)

How can you travese a directory using NIO2?

Using the Files.walk(...) method which returns a Stream<Path>

What are the arguments of the Files.fild(...) method?

FIles.filnd(Path path, int maxDepth, BiPredicate<Path,BasicFileAttributes> predicate)

What does Files.list(path) return?

A stream of Paths of all the child paths of the given one

What is the difference between Files.readAllLines and Files.lines?

  • Files.readAllLines returns a List of Strings
  • Files.readLines  returns a Stream of Strings
  • The latter is usually better as it does not require the entire file to be stored in memory

Assuming the current directory is /seals/harp/food, what is the result of executing the following code?


final Path path = Paths.get(".").normalize(); // h1
int count = 0;
for(int i=0; i<path.getNameCount(); ++i) {
count++;
}
System.out.println(count);

What are the 4 Key Concepts of JDBC?

  • Driver: Knows how to get a connection to the database
  • Connection: Knows how to communicate with the database
  • Statement: Konws how to run the SQL
  • ResultSet: Knows what was returned by a SELECT Query

What is the format of a JDBC URL?

protocol:Product/Vendor Name:DatabaseSpecific Connection Details

eg. jdbc:postgres://localhost:5432/zoo

 

How can you connect to a database using JDBC?

DriverManager.getConnection("jdbc:derby:zoo")

DriverManager.getConnection("jdbc:derby:zoo", "username", "password")

How can jdbc determine which driver to use?

A driver does advertise itself via a file called java.sql.Driver in the META-INF/services directory. Otherwise, the Class.forName("fqn.of.driver") has to be used.

How can you obtain a JDBC statement?

connection.createStatement()

connection.createStatement(ResultSet.Type_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)

What is the difference between the different ResultSet Types?

  • TYPE_FORWARD_ONLY: Usually sufficient, only next() is availbale.The view is static (new updates are not visible)
  • TYPE_SCROLL_INSENSITIVE Can scroll in any directoy,.The view is static (new updates are not visible)
  • TYPE_SCROLL_SENSITIVE: Can scroll in any directoy, the view is not static: new updates are visible. Not supported by most dirvers

What is the difference of the different result set concurrency modes?

  • CONCUR_READ_ONLY
  • CONCUR_UPDATABLE - not universally supported.

What is the difference between statment.execute() , statment.executeUpdate() and statement.executeQuery()?

  • statment.executeUpdate() - intended for DELETE, INSERT, UPDATE: Returns the number of affected rows
  • statement.executeQuery() - intended for SELECT: Returns a Result Set
  • statment.execute(): Generic: Returns true for a SELECT, otherwise false

What is the result of this code:

int result = stmt.executeUpdate("select * from animal");

An Exception is thrown: A resut was returned when none was expected

How does a Result set work?

  • It is a Cursor, meaning it points to a row (or a virtual row above/below the table)
  • navigate using next() and - if supported - previous(), absolte(), relative(), ...
  • multiple get___ Methods return the value of the column index/name in the current row

What has to be considered when working with column indices in Result Sets?

The columns are counted starting with 1 rather than 0

What happens if you don't call and check rs.next() before calling rs.get___(..)

The get___(..) method might throw an SQLException

How can you convert a java.sql.Date, java.sql.Time and java.sql.TimeStamp Object into a LocalDate/LocalTime, LocalDateTime?

Using the convenient Time.toLocalTime(), TimeStamp.toLocalDateTime(), Date.toLocalDate methods

Why do the return type of first()/last() and beforeFirst()/afterLast() differ?

The beforeFirst and afterLast do always work, making a return type redundant.

Where does the cursor point to when calling rs.absolute(0)

Same as rs.beforeFirst()

Where does the cursor point to when calling rs.absolute(-1)

To the last entyr in the table

What implicit rules corresponding to a JDBC connection, Statement and ResultSet exist?

  • Closing a Connection also closes the Statement and ResultSet
  • Closing a Statement also closes the ResultSet
  • JDBC automatically clsoes a ResultSet when you run another SQL statement from the same Statement

What is the difference between a paht.toRealPaht() and paht.toAbsolutePath()?

toAbsolutePaht does not operate on the fs ("theory") while toRealPath will resolve symbolic links . toRealPaht will throw an exceptiom if the file does not exist