Sobes.tech
Junior — Middle

Do you have practical experience working with JDBC for interacting with databases?

sobes.tech AI

Answer from AI

Yes, I have practical experience working with JDBC for interacting with databases. JDBC (Java Database Connectivity) is a standard API for connecting to and executing queries on relational databases from Java applications.

Example of simple JDBC usage for executing an SQL query:

import java.sql.*;

public class JdbcExample {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mydb";
        String user = "root";
        String password = "password";

        try (Connection conn = DriverManager.getConnection(url, user, password);
             Statement stmt = conn.createStatement()) {

            ResultSet rs = stmt.executeQuery("SELECT id, name FROM users");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                System.out.println(id + ": " + name);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

This code establishes a connection to the database, executes a query, and outputs the results.