java 2 3
fdsa
fdsa
-
- 1 / 496
-
Flashcards
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()
How to retrieve the name of a file/directory with a File-Object?
String getName()
How to retrieve the parentDirectory of a File-Object? Whats if there is none?
String getParent() -> null if no parent
How to check if a File-object is a directory?
boolean isDirectory()
How to check if a File-Object is a File?
boolean isFile();
how to return the number of milliseconds since the epoch when the file was last modified for a File-Object?
long lastModified()
How to return the length of a File-Object? What if the file doesnt exist?
long length() -> 0L if not exists
How to retrieve a list of files within a directory with a Files-Object?
File[] listFiles()
How to create a directory with a File-Object?
boolean mkdir()
boolean mkdirs()