
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to fetch only a single result from a table in Java-MySQL?
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (1.37 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(101,'Chris'); Query OK, 1 row affected (0.54 sec) mysql> insert into DemoTable values(102,'David'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(103,'Mike'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 101 | Chris | | 102 | David | | 103 | Mike | +------+-------+ 3 rows in set (0.00 sec)
Let us now see the Java-MySQL code. This will fetch only a single record from the top of the table −
Example
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import com.mysql.jdbc.ResultSetMetaData; public class OneResultDemo { public static void main(String[] args) { Connection con = null; PreparedStatement ps = null; Statement st = null; ResultSet rs = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost:3306/web?useSSL=false", "root", "123456"); String query = "select *from DemoTable " + " limit 1"; st = con.createStatement(); rs = st.executeQuery(query); while (rs.next()) { System.out.println("Id=" + rs.getInt("Id")); System.out.println("Name=" + rs.getString("Name")); } } catch (Exception e) { e.printStackTrace(); } } }
Output
This will produce the following output −
Id=101 Name=Chris
- Related Articles
- How to insert only a single column into a MySQL table with Java?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- How to fetch the newly added records from a MySQL table?
- How will you extract multiple rows from a DB2 table in a single FETCH call?
- Fetch random rows from a table with MySQL
- Want to fetch only the month part from a date in MySQL
- Fetch a single ordered date from a column with MySQL LIMIT
- How to fetch only N rows at a time in MySQL?
- How can we delete a single row from a MySQL table?
- Insertion in a MySQL table with only a single column set as auto_increment?
- How to delete a single value from a MySQL table with duplicate records?
- How can we fetch a particular row as output from a MySQL table?
- How can fetch records from specific month and year in a MySQL table?
- MySQL query to fetch only a single field on the basis of boolean value in another field
- Get only a single value from a specific MySQL row?

Advertisements