JDBC Flashcards
What is the purpose of the driver manager?
it works as a directory of JDBC drivers - it maintains a list of available data source and their drivers. The driver manager chooses an appropriate driver to communicate with the respective DBMS.
What is the general format of the JDBC URL?
jdbc: :
ie. jdbc:mysql://localhost:3306
What is the DriverManager used for?
helps establish the connection between the program (the user) and the JDBC drivers
What is the difference between Statement, PreparedStatement and CallableStatement?
Statement: Sends a SQL statement to the database without any parameters. For typical uses, you need to use this interface. You can create an instance of Statement using the createStatement() method in the Connection interface
PreparedStatement: Represents a precompiled SQL statement that can be customized using IN parameters. You can get an instance of PreparedStatement by calling the preparedStatement() method in the Connection interface
CallableStatement: Executes stored procedures. CallableStatement instances can handle IN as well as OUT and INOUT parameters. You need to call the prepareCall() method in the Connection interface to get an instance of this class
What does a executeQuery() method returns?
returns a ResultSet
What does a executeUpdate() method returns?
returns an update count
What does a execute() method returns?
multiple ResultResets or multiple update counts or a combination of both
What is the column index that the resultSet starts from?
1
What are the two ways of updating the database?
queries or you can fetch a resultset using SQL query and then change the database
What are the key interfaces of JDBC?
- Driver ( knows how to get a connection from the database)
- Connection (knows how to communicate with the database)
- Statement (knows how to run the SQL)
- Result (knows what was returned by the select query)
What are the two ways of getting a connection?
DriverManager or DataSource
DriverManager.getConnection(“jdbc:derby:zoo”)
How to obtain a statement?
stmt = conn.createStatement()
or
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY
What are the ResultSet Type options?
ResultSetType : Can go backwards : See latest data
- ResultSet.TYPE_FORWARD_ONLY : no : no
- ResultSet.TYPE_SCROLL_INSENSITIVE: yes : no
- ResultSet_TYPE_SCROLL_SENSITIVE : yes : yes
What are the ResultSet ConcurrencyMode?
ResultSetType : can read data : can update data
- ResultSet.CONCUR_READ_ONLY : yes : no
- ResultSet.CONCUR_UPDATABLE: yes : yes
How can you execute a statement?
Statement stmt = conn.createStatement()
int result = stmt.executeUpdate( “insert/update/delete”
or
ResultSet rs = stmt.executeQuery(“select * from species”);