Posts

Showing posts with the label JDBC

Execution of queries with JDBC

Image
What are the database queries made with? Java uses three interfaces to query the database: java.sql.Statement - for SQL statements without parameters; java.sql.PreparedStatement - for SQL statements with parameters and frequently executed statements; java.sql.CallableStatement - for executing procedures stored in the database. Interface bearers are created using the methods of the java.sql.Connection object: java.sql.createStatement() returns a Statement object; java.sql.prepareStatement() returns a PreparedStatement object; java.sql.prepareCall() returns a CallableStatement object; Difference between Statement and PreparedStatement Statement: Used for simple query cases without parameters. PreparedStatement: precompiles a query that can contain input parameters and be executed multiple times with a different set of those parameters. Before execution, the DBMS parses each query, optimizes it, and creates a query plan for its execution. If the same query is executed se...

Transaction isolation levels supported in JDBC

Image
Transaction isolation level is a value that determines the level at which inconsistent data is allowed in a transaction, that is, the degree of isolation of one transaction from another. A higher level of isolation improves the accuracy of the data, but it can reduce the number of concurrent transactions. On the other hand, a lower level of isolation allows more concurrent transactions, but reduces the accuracy of the data. During the use of transactions, to ensure data integrity, the DBMS uses locks to block other access to the data involved in the transaction. Such locks are necessary to prevent: "dirty" read - reading data, added or modified by a transaction, which later will not be confirmed (rolled back); non-repeatable read - when re-reading within one transaction, previously read data is changed; phantom reads - a situation when, when reading repeatedly within the same transaction, the same sample gives different sets of rows. Transaction isolation levels are de...

Basic steps for working with a database using JDBC

Image
Registration of drivers; Establishing a connection to the database; Creation of query(s) to the database; Executing the query(s) to the database; Processing the result(s); Closing the connection to the database. Register the JDBC driver Driver registration can be done in several ways: java.sql.DriverManager.registerDriver(%driver class object%) Class.forName("fully qualified driver class name").NewInstance() Class.forName("fully qualified driver class name") Establish a connection to the database The java.sql.DriverManager.getConnection(...) static call is used to establish a connection to the database. The parameter can be passed: Database URL static Connection getConnection(String url) Database URL and set of properties to initialize static Connection getConnection(String url, Properties info) Database URL, username and password static Connection getConnection(String url, String user, String password) As a result of the cal...

Parts of JDBC

Image
JDBC has two parts: JDBC API, which contains a set of classes and interfaces that define access to databases. These classes and methods are declared in two packages - java.sql and javax.sql; JDBC driver, a component specific to each database. JDBC turns API calls into native commands for a particular database server. List the main JDBC classes and interfaces java.sql.DriverManager - Lets you download and register the required JDBC driver, and then get a connection to the database. javax.sql.DataSource - solves the same tasks as DriverManager, but in a more convenient and versatile way. There are also javax.sql.ConnectionPoolDataSource and javax.sql.XADataSource whose job it is to provide connection pooling. java.sql.Connection - Provides data source queries and transaction management. The javax.sql.PooledConnection and javax.sql.XAConnection interfaces are also provided. java.sql.Statement, java.sql.PreparedStatement, java.sql.CallableStatement - These interfaces allow you t...

JDBC

Image
JDBC, Java DataBase Connectivity (connection to databases in Java) - the industry standard for the interaction of Java applications with various DBMS. Implemented as the java.sql package included with Java SE. JDBC is based on the concept of drivers that allow you to get a connection to a database at a specially described URL. When loaded, the driver registers itself in the system and is then automatically called when the program requires a URL containing the protocol for which this driver is responsible. Benefits of using JDBC The advantages of JDBC are: Ease of development: the developer may not know the specifics of the database with which he works; The code practically does not change if the company moves to another database (the number of changes depends solely on the differences between the SQL dialects); There is no need to additionally install the client program; Any database can be connected via an easily descriptive URL. JDBC URL JDBC URL consists of: <protoco...