In JDBC, you can execute SQL queries using different types of statements, such as Statement, PreparedStatement, and CallableStatement. Each type of statement has its own use case and advantages. Here, I'll provide examples of how to execute SQL queries using each of these statement types.
Before running these examples, ensure that you have a MySQL database set up, and you have the MySQL JDBC driver in your classpath.
1. Using Statement to Execute SQL Queries
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;publicclassStatementExample {publicstaticvoidmain(String[] args) {try {Connection connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database","your_username","your_password");Statement statement =connection.createStatement();// Execute a SELECT queryResultSet resultSet =statement.executeQuery("SELECT * FROM your_table_name");// Process the resultswhile (resultSet.next()) {int id =resultSet.getInt("id");String name =resultSet.getString("name");System.out.println("ID: "+ id +", Name: "+ name); }// Close the resourcesresultSet.close();statement.close();connection.close(); } catch (Exception e) {e.printStackTrace(); } }}
2. Using PreparedStatement to Execute SQL Queries
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;publicclassPreparedStatementExample {publicstaticvoidmain(String[] args) {try {Connection connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database","your_username","your_password");// Define a SQL query with parametersString sql ="SELECT * FROM your_table_name WHERE id = ?";int targetId =1;PreparedStatement preparedStatement =connection.prepareStatement(sql);preparedStatement.setInt(1, targetId);// Execute the queryResultSet resultSet =preparedStatement.executeQuery();// Process the resultswhile (resultSet.next()) {int id =resultSet.getInt("id");String name =resultSet.getString("name");System.out.println("ID: "+ id +", Name: "+ name); }// Close the resourcesresultSet.close();preparedStatement.close();connection.close(); } catch (Exception e) {e.printStackTrace(); } }}
3. Using CallableStatement to Execute SQL Queries
Callable statements are typically used for calling stored procedures in databases. Here's a basic example:
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.CallableStatement;importjava.sql.Types;publicclassCallableStatementExample {publicstaticvoidmain(String[] args) {try {Connection connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database","your_username","your_password");// Define a stored procedure callString sql ="{CALL your_stored_procedure(?, ?)}";int param1 =1;int outputParam =0;CallableStatement callableStatement =connection.prepareCall(sql);callableStatement.setInt(1, param1);callableStatement.registerOutParameter(2,Types.INTEGER);// Execute the stored procedure callcallableStatement.execute();// Retrieve the output parameter value outputParam =callableStatement.getInt(2);System.out.println("Output Parameter: "+ outputParam);// Close the resourcescallableStatement.close();connection.close(); } catch (Exception e) {e.printStackTrace(); } }}
Make sure to replace "your_database", "your_username", "your_table_name", "your_stored_procedure", and any placeholders with your actual database information and SQL statements. Handle exceptions properly in a real application for robust error handling.