![]() |
Java programming provides a lot of packages for solving problems in our case we need to execute SQL queries by using JDBC. We can execute a SQL query in two different approaches by using PreparedStatement and Statement. These two interfaces are available in java.sql package. When comparing both of them the PreparedStatement approach is secure. Approaches to Execute a SQL Query using JDBCWe have two different approaches to executing a SQL query using JDBC. Below is the list and we will explain them with examples to understand the concept correctly.
Statement in JDBCThe Statement is an interface that is available in java.sql package with JDBC.
Example:In this example, we will write an SQL query to fetch all data from the table in the database. We have already some data in the table. Now we will write an SQL query for fetching that data using Statement. For this, we have used a database named books and the table name is a book. Java
Output:Below we can see the retrieved data in book table. Explanation of the Code:
PreparedStatement in JDBCThe PreparedStatement is an interface, and it provides for security for our data by using parameter concept in Java.
Example:Java
Output:Explanation of the above Code:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |