JDBC – Java Database Connectivity is an application programming interface (API) for the Java programming language, which sets up a connection between a client and a database.
Main components (interfaces and classes) of JDBC are:
DriverManager – it makes sure that the user/client is using the correct DB driver when conduncting the requests
Driver – the rules or the way the client communicate with the database
Statement – in short, we define a statement (SQL Query), which we execute on the database
ResultSet – output of the execution of a statement
SQLException – methods using JDBC usualy throw SQLException
JDBC code example
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCconnectionExample {
private static Connection connection;
private static final String user = "username";
private static final String password = "password";
private static final String dbName = "scheme_name";
public static Connection getMyConnection() {
Properties properties = new Properties();
properties.setProperty("user", user);
properties.setProperty("password", password);
try {
connection = DriverManager
.getConnection("jdbc:[db type]://[host]:[port]/" + dbName, properties);
} catch (SQLException e) {
System.out.println("Here comes some error message!");
e.printStackTrace();
}
return connection;
}
}
The Main class:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main_Example {
private static final Connection connection = JDBCconnectionExample.getMyConnection();
private static final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException, SQLException {
simpleUpdateMethod();
int x = simpleSelectMethod();
connection.close();
}
private static void simpleUpdateMethod() throws IOException, SQLException {
// getting the input distributed into variables
String[] myInput = input.readLine().split("\\s+");
String aStringVariable = myInput[0];
int anIntegerVariable = Integer.parseInt(myInput[1]);
PreparedStatement preparedStatement = connection
.prepareStatement("INSERT INTO TABLE_NAME(name, age) VALUE (?, ?)");
preparedStatement.setString(1, aStringVariable);
preparedStatement.setInt(2, anIntegerVariable);
preparedStatement.executeUpdate();
System.out.printf("name %s with age %d were inserted to the database.%n"
, aStringVariable, anIntegerVariable);
}
private static int simpleSelectMethod() throws SQLException, IOException {
String[] myInput = input.readLine().split("\\s+");
String aStringVariable = myInput[0];
PreparedStatement preparedStatement = connection.prepareStatement(
"SELECT column FROM TABLE_NAME WHERE name = ?");
preparedStatement.setString(1, aStringVariable);
ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
return resultSet.getInt("column");
}
}
JDBC – ORM – Hibernate
JDBC – API application programming interface i.e. it makes it possible, two applications to speak with each other.
ORM – Object Relational Mapping, Framework
Hibernate – Framework
!!! ORMs are based on JDBC under the hood
!!! As an Object/Relational Mapping (ORM) Framework, Hibernate is concerned with data persistence as it applies to relational databases (via JDBC).
Get Java and start coding now!
Java is a versatile, high-level programming language that was first developed by Sun Microsystems in the mid-1990s and is now owned by Oracle Corporation. Known for its „write once, run anywhere“ philosophy, Java is designed to be platform-independent, meaning that programs written in Java can run on any device or operating system that has a Java Virtual Machine (JVM). This makes Java particularly popular for developing web applications, mobile apps (especially Android), and enterprise-level solutions. It is an object-oriented language, which promotes reusable and modular code, and comes with a rich set of libraries and frameworks that streamline development. Java’s strong community support, security features, and scalability have made it one of the most widely used programming languages in the world.