⬅ Previous Topic
SQL with ExcelConnecting to SQL Databases via JDBC/ODBC
Access Your Data Securely from Applications
Introduction
Connecting your app or tool to a database is like giving it a voice — one that can ask, update, and respond to data. Whether you're building a Java school app using JDBC or using Excel to pull live data via ODBC, this tutorial will guide you through both paths clearly and practically.
What Are JDBC and ODBC?
- JDBC (Java Database Connectivity) is a Java API to connect and execute SQL queries from Java applications.
- ODBC (Open Database Connectivity) is a standard interface that applications (like Excel or Python) use to connect to any SQL-compliant database via a driver.
Part 1: Connecting via JDBC (Java)
Step 1: Add MySQL JDBC Driver
Download from: https://dev.mysql.com/downloads/connector/j/
Include the JAR file in your project’s classpath (via IDE or build tool).
Step 2: Java Code to Connect
import java.sql.*;
public class JDBCConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/school";
String user = "root";
String password = "your_password";
try {
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while (rs.next()) {
System.out.println(rs.getInt("roll_no") + " - " + rs.getString("name"));
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
1 - Aarav
2 - Diya
Part 2: Connecting via ODBC (Excel, Python, Access)
Step 1: Install ODBC Driver
- For MySQL: Install MySQL ODBC Connector from [https://dev.mysql.com/downloads/connector/odbc/](https://dev.mysql.com/downloads/connector/odbc/)
- For SQL Server: Use built-in Microsoft ODBC Driver
Step 2: Create DSN (Data Source Name)
- Go to Control Panel → Administrative Tools → ODBC Data Sources (64-bit)
- Click Add → Choose your driver (e.g., MySQL)
- Give a name (e.g.,
school_dsn
), set host, port, user, password, and database
Step 3: Use DSN in Excel
- Open Excel → Data → Get Data → From Other Sources → From ODBC
- Select
school_dsn
- Choose
students
table - Use filters and transforms as needed via Power Query
Step 4: Use DSN in Python (optional)
import pyodbc
conn = pyodbc.connect("DSN=school_dsn;UID=root;PWD=your_password")
cursor = conn.cursor()
cursor.execute("SELECT name, city FROM students WHERE class = '10A'")
for row in cursor.fetchall():
print(row)
('Aarav', 'Pune')
('Diya', 'Chennai')
Real-World School Use Cases
- Java application pulling student records in real-time via JDBC
- Excel dashboard using ODBC to pull marks directly from MySQL
- Python scripts automating daily attendance upload via DSN
- Connecting centralized school database to admin tools securely
Best Practices
- Always close connections after use
- Use PreparedStatements (JDBC) to avoid SQL injection
- Limit query permissions for ODBC users (read-only if needed)
- Use connection pooling for scalable JDBC apps
Summary
JDBC and ODBC open the doors to powerful SQL integration from your favorite environments — Java, Excel, Python, and more. When working with school data like student marks, attendance, or fee records, connecting properly means enabling secure, flexible access for admins, teachers, and tools alike.
What’s Next?
Up next: SQL Interview Questions — learn to handle real-world SQL problems and integrations with confidence.