
- 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
Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java
For this, you can use PrepareStatement in Java. Following is the syntax −
String anyVariableName="select yourColumnName from yourTableName where name = ?"; PreparedStatement ps = (PreparedStatement) con.prepareStatement(yourVariableName); ps.setString(yourColumnIndex, yourValue);
Let us create a table −
mysql> create table demo37 −> ( −> id int not null auto_increment primary key, −> name varchar(200) −> ); Query OK, 0 rows affected (2.46 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo37(name) values('John'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo37(name) values('Bob'); Query OK, 1 row affected (0.08 sec) mysql> insert into demo37(name) values('John'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo37(name) values('Chris'); Query OK, 1 row affected (0.08 sec) mysql> insert into demo37(name) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo37(name) values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo37(name) values('Mike'); Query OK, 1 row affected (0.09 sec)
Display records from the table using select statement −/p>
mysql> select *from demo37;
This will produce the following output −
+----+-------+ | id | name | +----+-------+ | 1 | John | | 2 | Bob | | 3 | John | | 4 | Chris | | 5 | David | | 6 | John | | 7 | Mike | +----+-------+ 7 rows in set (0.00 sec)
Example
Following is the Java code for PrepareStatement −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class PrepareStatementDemo { public static void main(String[] args) { Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledatabase", "root", "123456"); String query = "select name from demo37 where name = ?"; PreparedStatement ps = (PreparedStatement) con.prepareStatement(query); ps.setString(1, "John"); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getString(1)); } } catch (Exception e) { e.printStackTrace(); } } }
Output
This will produce the following output −
John John John
Following is the snapshot of the output −
- Related Articles
- How to use prepared statement for select query in Java with MySQL?
- Correctly implement the AND condition in MySQL
- Change value from 1 to Y in MySQL Select Statement using CASE?
- Using CASE statement in MySQL to display custom name for empty value
- How to select return value from MySQL prepared statement?
- Can we use WHERE clause inside MySQL CASE statement?
- Perform count with CASE WHEN statement in MySQL?
- How to implement MySQL CASE with OR condition?
- How to use count with CASE condition in a MySQL query?
- MySQL order by field using CASE Statement
- Implement MySQL CASE statement with WHEN clause
- How to use quotes correctly while using LIKE with search variable in MySQL in Java?
- Using Update statement with TINYINT in MySQL?
- MySQL case statement inside a select statement?
- Assign an SQL result to variable from prepared statement in MySQL?

Advertisements