If you’re working with Java and databases, chances are you’ve encountered PreparedStatement
— a safe and efficient way to run SQL queries. But what if you want to group each row of results into a nice, reusable object like a Tuple, especially when you’re not using advanced tools like JPA or EntityManager
?
In this beginner-friendly guide, we’ll show you how to:
- Run SQL queries using
PreparedStatement
- Define your own simple Tuple class in Java 8
- Store and print the results cleanly
- Do all of this without any JPA, Hibernate, or EntityManager
What is a Tuple in Java?
In languages like Python or Scala, a Tuple is a simple object that groups together multiple values. Java doesn’t have built-in Tuple support, but you can easily mimic this behavior by creating a custom class.
Example:
public class MyTuple<T1, T2> {
private T1 item1;
private T2 item2;
public MyTuple(T1 item1, T2 item2) {
this.item1 = item1;
this.item2 = item2;
}
public T1 getItem1() { return item1; }
public T2 getItem2() { return item2; }
}
This class can store a pair of values from each database row.
Prerequisites
- Java 8+
- Basic knowledge of JDBC (Java Database Connectivity)
- A database (MySQL, PostgreSQL, SQLite, etc.)
- JDBC driver configured in your project
Step-by-Step Guide: Fetching Data into Tuples Using PreparedStatement
✅ Step 1: Setup Your JDBC Connection
Replace the placeholders with your actual database information.
String url = "jdbc:mysql://localhost:3306/yourdb";
String username = "yourusername";
String password = "yourpassword";
✅ Step 2: Write Your Tuple Class
Here’s a basic version that holds two values:
public class MyTuple<T1, T2> {
private T1 item1;
private T2 item2;
public MyTuple(T1 item1, T2 item2) {
this.item1 = item1;
this.item2 = item2;
}
public T1 getItem1() { return item1; }
public T2 getItem2() { return item2; }
@Override
public String toString() {
return "(" + item1 + ", " + item2 + ")";
}
}
You can extend it to hold more fields as needed.
✅ Step 3: Execute Query Using PreparedStatement
Now, let’s write the full working example that runs a query and stores each row into a MyTuple
.
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TupleExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/yourdb";
String username = "yourusername";
String password = "yourpassword";
String query = "SELECT column1, column2 FROM yourtable";
try (Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement stmt = conn.prepareStatement(query)) {
ResultSet rs = stmt.executeQuery();
List<MyTuple<String, String>> results = new ArrayList<>();
while (rs.next()) {
MyTuple<String, String> tuple = new MyTuple<>(
rs.getString("column1"),
rs.getString("column2")
);
results.add(tuple);
}
// Display the results
for (MyTuple<String, String> tuple : results) {
System.out.println(tuple);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
📝 Make sure the column names in
rs.getString("column1")
match the ones in your actual database table.
Advantages of Using a Custom Tuple Class
- ✅ Keeps code clean and readable
- ✅ Avoids dependency on JPA or other frameworks
- ✅ Helps beginners understand data mapping
- ✅ Easily extendable to hold 3 or more fields
Final Thoughts
You don’t need complex frameworks to write clean and maintainable database code in Java. By using a simple PreparedStatement
and a custom Tuple class, you can build lightweight, object-oriented database access — perfect for small projects, performance-sensitive applications, or just learning how things work under the hood.
What’s Next?
- Try modifying
MyTuple
to hold three or more fields - Add type-safe generic support or use third-party tuple libraries
- Extend to support filtering or sorting results after retrieval
Share It!
If this guide helped you, feel free to share it on LinkedIn, Twitter, or your developer group.