fdsa


Fichier Détails

Cartes-fiches 496
Langue Deutsch
Catégorie Informatique
Niveau Autres
Crée / Actualisé 06.12.2020 / 24.01.2021
Lien de web
https://card2brain.ch/box/20201206_java_2_3
Intégrer
<iframe src="https://card2brain.ch/box/20201206_java_2_3/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

Is it possible to create a jdbc-connection via constructor

No, use DataManager.getConnection for the exam, in real life: DataSource

Which exception is thrown by DriverManager.getConnection?

SqlException  (check in examquestions if it is thrown)

What are the benefits of a PreparedStatement?

  • Performance
  • Security
  • Readability
  • Future use

Is it possible to create a preparedStatement without providing a query as parameter?

No, this will not compile:

var ps = conn.prepareStatement()

How to get the returned Value of ps.execute() ? 

Check first if it returned something (returned value will be boolean indicating if returnvalue or updatecount), then get the resultset via ps.getResultset if it is true or ps.getUpdateCount if it is false

What happens with incompatible returntypes? (f.e. SELECT-query but executeUpdate() called)

Will throw a SQLException like: .executeUpdate cannot be called with a statement

What is the returntype and the returnvalues for select/[delete|insert|update] for ps.execute()

boolean, true for select, false for updates

What is the returntype and the returnvalues for select/[delete|insert|update] for ps.executeQuery()?

ResultSet, rows and columns returned for select

What is the returntype and the returnvalues for select/[delete|insert|update] for ps.executeUpdate?

int, number of rows added/changed/removed returned for updates

Where does the bindvariables within a preparedStatement start to count? 

start with 1

What happens if not all bindVariables are set in a preparedStatement?

Code compiles but SQLException

What happens if to many bindVariables are provided for a prepared statement?

SQLException like "the number of values assigned is not the same as the number of specified or implied columns"

Can we use the same perparedStatement for multiple executeUpdates? Whats with the bindVariables?

Yes, the perparedStatement will remember the parameters that were already set if not changed

What does resultSet.next() do? 

Sets the cursor to the next element, returns boolean indicating if it worked

Where does the counting stat within a resultSet?

at 1

What params does f.e. resultSet.getInt accept?

either 1/2/.. or the columnname

Is it neccessary to call rs.next() first?

Yes, if no result returned we have to break. Even with result we have to call it to set the cursor to the first element

What happens if we try to acces a column which doesn access in jdbc?

Throws a SQLException like: "Column 'bla' not found"

Which getMethods are provided by a resultSet?

  • getBoolean
  • getDouble
  • getInt
  • getLong
  • getObject
  • getString

What are the benefits of stored procedures?

redudes network roundtrips, allows expert to handle their part of code, ..

Whats the returntype of connection.perpareCall(sql)?

CallableStatement

How is the syntax to call a stored procedure?

call + proecdurename in braces, f.e.: {call read_e_names()} 

Whats neccessary for passing IN parameters to stored procedures?

a ? to show that we have a parameter, f.e.: "{call read_names_by_letter(?)]"

How to set parameters in callableStatements?

Unlike preparedStatement htere are two ways, the parameter number or name:

cs.setString(1, "Z")

cs.setString("prefix", "Z")

Are there any neccessary special characters for outparameters?

No, it is optional to put ?= in front: "{?= call magic_number(?) }"

Whats important to register with OUT/INOUT parameters?

registerOutParamenter() for each OUT/INOUT parameter:

cs.registerOutParameter(1, Types.INTEGER)

Doe we call executeQuery for calls to stored procedures with OUT-parameters?

No, we use execute() since we are not returning a ResultSet

How to use a INOUT parameter here?:

var sql = "{call double_number(?)}"

try (var cs = conn.prepareCall(sql)) {
   // fill in the gap
}

cs.setInt(1,8);
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
System.out.println(cs.getInt("num"));

Whats the order of closing DB resources?

ResultSet, Prepared/Callable-Statement, Connection

Is it neccessary to close DB resoruces in the right order?

Yes, to avoid resource leaks and exceptions

What happens with a resultset if we run another query from the same statement?

JDBC automatically closes a ResultSet when we run another SQLstatement from the same statement

What are the only I/O classes we have to know for the exam which doesnt have complementary InputStream or Reader classes?

PrintStream and PrintWriter

Is "Object" serializable?

No

Can a File-instance only represent a File?

No, also a directory

How to get the local separator character?

System.getProperty("file.separator");

What are the three main constructors for File? 
Has the File to exist?

public File(String pathname)

public File(File parent, String child)

public File(String parent, String child)

-> Represents only the path to a file, not an actual file

What happens with new File(parent, child) if the parent is null?

Will be skipped and the method will revert to the single String constructor

Does a directory have to be empty for File.delete()?

Yes, if not it will return false

How to check if a file exists with the File-object?

boolean exists()

How to retrieve the absolute name of the file/directory with a File-object?

String getAbsolutePath()