- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use prepared statement for select query in Java with MySQL?
You need to use executeQuery() for this. The syntax is as follows −
yourPreparedStatementObject=yourConnectionObject.prepareStatement(yourQueryName); yourresultSetObject=yourPreparedStatementObject.executeQuery();
Create a table in the database ‘sample’. The query to create a table is as follows −
mysql> create table JavaPreparedStatement -> ( -> Id int, -> Name varchar(10), -> Age int -> ); Query OK, 0 rows affected (0.89 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into JavaPreparedStatement values(1,'Larry',23); Query OK, 1 row affected (0.16 sec) mysql> insert into JavaPreparedStatement values(2,'Sam',25); Query OK, 1 row affected (0.21 sec) mysql> insert into JavaPreparedStatement values(3,'Mike',26); Query OK, 1 row affected (0.12 sec)
Now you can display all records of the table using Java PreparedStatement. You need to use the executeQuery() method.
Here is the Java code −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class PreparedStatementSelectQueryDemo { public static void main(String[] args) { String JdbcURL = "jdbc:mysql://localhost:3306/sample?useSSL=false"; String Username = "root"; String password = "123456"; Connection con = null; PreparedStatement pstmt = null; ResultSet rst = null; String myQuery = "select Id,Name,Age from JavaPreparedStatement"; try { con = DriverManager.getConnection(JdbcURL, Username, password); pstmt = con.prepareStatement(myQuery); rst = pstmt.executeQuery(); System.out.println("Id\t\tName\t\tAge
"); while(rst.next()) { System.out.print(rst.getInt(1)); System.out.print("\t\t"+rst.getString(2)); System.out.print("\t\t"+rst.getInt(3)); System.out.println(); } } catch(Exception exec) { exec.printStackTrace(); } } }
Here is the snapshot of Java code −
Here is the snapshot of sample output −
- Related Articles
- How to select return value from MySQL prepared statement?
- Can we use SELECT NULL statement in a MySQL query?
- How to use NULL in MySQL SELECT statement?
- How to use alias in MySQL select query?
- How to use a select statement while updating in MySQL?
- MySQL query to generate row index (rank) in SELECT statement?
- How to use the CAST function in a MySQL SELECT statement?
- How to use MySQL CASE statement while using UPDATE Query?
- Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java
- How can we use group functions with non-group fields in MySQL SELECT query?
- How to order and select query with conditions in MySQL?
- How can I use MySQL IF() function within SELECT statement?
- MySQL SELECT IF statement with OR?
- How to use SELECT Query in Android sqlite?
- MySQL Select Statement DISTINCT for Multiple Columns?

Advertisements