惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog
Y
Y Combinator Blog
博客园_首页
有赞技术团队
有赞技术团队
博客园 - Franky
腾讯CDC
G
Google Developers Blog
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
D
Docker
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
A
About on SuperTechFans
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
V
V2EX
U
Unit 42
aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building a Simple Java JDBC Project with MySQL: Displayin...
Jayashree · 2026-06-23 · via DEV Community
Cover image for Building a Simple Java JDBC Project with MySQL: Displaying Cricket Players and Scores

Jayashree

While learning Java, I wanted to understand how Java connects with a database and retrieves data. Instead of using a random example, I created a small cricket project. The idea was simple: store Indian and Afghanistan players' scores in MySQL and display them through Java.

Step 1: Creating the Database

First, I created a database named cricket.

CREATE DATABASE cricket;
USE cricket;

Then, I created two tables:

CREATE TABLE india (
    player_name VARCHAR(50),
    score INT
);

CREATE TABLE afghanistan (
    player_name VARCHAR(50),
    score INT
);

After that, I inserted player data into both tables.

Step 2: Connecting Java with MySQL

To connect Java with MySQL, I used JDBC.

String url = "jdbc:mysql://localhost:3306/cricket";
String user = "root";
String password = "root";

Connection con = DriverManager.getConnection(url, user, password);

This line creates a connection between the Java application and the MySQL database.

Step 3: Creating a Statement Object

Once the connection is established, we need a Statement object to send SQL queries to the database.

Statement st = con.createStatement();

Think of it as a messenger that sends SQL commands from Java to MySQL.

Step 4: Writing the SQL Query

Since player data was stored in two different tables, I used UNION ALL to combine them.

String query =
"SELECT 'India' AS country, player_name, score FROM india " +
"UNION ALL " +
"SELECT 'Afghanistan' AS country, player_name, score FROM afghanistan";

The AS country part adds a country name because the tables themselves contain only player names and scores.

Step 5: Executing the Query

ResultSet rs = st.executeQuery(query);

The executeQuery() method sends the query to MySQL and returns the result in a ResultSet object.

Step 6: Reading Data from ResultSet

while (rs.next()) {
    System.out.println(
        rs.getString("country") + " " +
        rs.getString("player_name") + " " +
        rs.getInt("score"));
}

rs.next() moves row by row through the result.

Methods like:

rs.getString("country");
rs.getString("player_name");
rs.getInt("score");

retrieve column values from each row.

Formatting the Output

To make the output look neat, I used printf().

System.out.printf("%-15s %-25s %-10s%n",
                  "Country", "Player", "Score");

while (rs.next()) {
    System.out.printf("%-15s %-25s %-10d%n",
            rs.getString("country"),
            rs.getString("player_name"),
            rs.getInt("score"));
}

Output:

Country Player Score

India Rohit Sharma 76
India Shubman Gill 187
India Virat Kohli 154
Afghanistan Rahmanullah Gurbaz 170
Afghanistan Azmatullah Omarzai 140

Filtering Players with Scores Above 100

I also tried displaying only players who scored more than 100.

String query =
"SELECT 'India' AS country, player_name, score FROM india WHERE score > 100 " +
"UNION ALL " +
"SELECT 'Afghanistan' AS country, player_name, score FROM afghanistan WHERE score > 100";

This returned only the top performers.

What I Learned

Through this mini project, I got hands-on experience with:

  • MySQL database creation
  • Table creation and inserting data
  • JDBC connection
  • Statement and ResultSet
  • Executing SQL queries
  • Combining tables using UNION ALL
  • Reading records with while(rs.next())
  • Formatting output with printf()
  • Filtering records using WHERE

Although it was a simple project, it helped me understand how Java applications interact with databases in real-world scenarios. It also gave me a solid foundation for learning advanced JDBC concepts like PreparedStatement, transactions, and CRUD operations.