Nearby lessons

4 of 37

JDBC - Environment Setup (MySQL)

📌 What You Will Learn
  • SQL aggregate functions: count, sum, avg, min, max
  • Real-time coding standards every JDBC program should follow
  • Setting up MySQL and running JDBC with it
  • Using try-with-resources to close things safely

Environment Setup (MySQL) is a fundamental part of database programming with JDBC. This lesson explains Setting Up MySQL for JDBC (Quick Guide) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Setting Up MySQL for JDBC (Quick Guide)

Follow these steps once, and all the examples in this material will run:

  • Install MySQL Server (and MySQL Workbench for convenience).
  • Start the MySQL service and note the root password you set.
  • Create a database: CREATE DATABASE company;
  • Create a table: CREATE TABLE employee (id INT, name VARCHAR(50), salary DOUBLE);
  • Insert a few rows so SELECT works.
  • Download mysql-connector-j JAR and put it in your classpath.
Example01
JCode Cell
1CREATE DATABASE company;
2USE company;
3CREATE TABLE employee (id INT, name VARCHAR(50), salary DOUBLE);
4INSERT INTO employee VALUES (101, 'Rahul', 50000), (102, 'Priya', 60000);
5 
6# Run your program:
7java -cp .;mysql-connector-j.jar YourProgram
📝 Key Takeaways
  • Aggregate functions: count, sum, avg, min, max — one summary value from many rows.
  • rs.next() then getInt(1) reads the single-row aggregate result.
  • Real-time standards: close resources, use PreparedStatement, use connection pools.
  • try-with-resources closes Connection, Statement, ResultSet automatically.
  • MySQL setup: create DB → create table → add rows → add connector JAR to classpath.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1