Found 317 Articles for JDBC

What are stored procedures? How to call stored procedures using JDBC program?

Krantik Chavan
Updated on 09-Mar-2020 06:32:51

334 Views

Stored procedures are sub routines, segment of SQL statements which are stored in SQL catalog. All the applications that can access Relational databases (Java, Python, PHP etc.), can access these procedures.Stored procedures contain IN and OUT parameters, or both. They may return result sets in case you use SELECT statements, they can return multiple result-sets.ExampleSuppose we have a table named Dispatches in the MySQL database with the following data:+--------------+------------------+------------------+------------------+ | Product_Name | Date_Of_Dispatch | Time_Of_Dispatch | Location         | +--------------+------------------+------------------+------------------+ | KeyBoard     | 1970-01-19       | 08:51:36         | Hyderabad ... Read More

What are the advantages and limitations of JDBC PreparedStatement?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

1K+ Views

Following are the advantages of the prepared statement:By avoiding multiple compilation and execution of statements, prepared statements perform faster.Using prepared statements, we can insert values to advanced datatypes such as BLOB, CLOB, OBJECT easily with the help of the setter methods provided by the PreparedStatement interface.By providing setter method to set values prepared statement avoids the use of quotes and other special characters with in the query, and thereby it escapes the SQL injection attacksFollowing are the limitations of prepared statements:Since a PreparedStatement object represents only one SQL statement at a time, we can execute only one statement by one ... Read More

Why are Prepared Statements in JDBC faster than Statements? Explain?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

414 Views

While executing statements using Statement object, especially insert statements, each time a query is executed the whole statement is compiled and executed again and again where, the only difference among these statements is the values of the statements.Whereas, prepared statement is a precompiled statement i.e. the query is compiled and stored in the database, using place holders (?) instead of values and values to these place holders are supplied later.Thus, avoiding unnecessary compilation and execution of the statement again and again.ExampleSuppose, we have a table named Dataset in the database with the columns mobile_brand and unit_sale, if we want to ... Read More

Is it mandatory to close JDBC connections?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

2K+ Views

At the end of your JDBC program, it is required explicitly to close all the connections to the database to end each database session. However, if you forget, Java's garbage collector will close the connection when it cleans up stale objects.Relying on the garbage collection, especially in database programming, is a very poor programming practice. You should make a habit of always closing the connection with the close() method associated with connection object.To ensure that a connection is closed, you could provide a 'finally' block in your code. A finally block always executes, regardless of an exception occurs or not.To ... Read More

How to call a stored procedure using callable statement in JDBC explain?

Krantik Chavan
Updated on 09-Mar-2020 06:37:08

3K+ Views

You can call the SQL stored procedures using the CallableStatement interface. A Callable statement can have input parameters, output parameters, or both.You can create an object of the CallableStatement (interface) using the prepareCall() method of the Connection interface. This method accepts a string variable representing a query to call the stored procedures and returns a CallableStatement object.Suppose you have a procedure name myProcedure in the database you can prepare a callable statement as://Preparing a CallableStatement CallableStatement cstmt = con.prepareCall("{call myProcedure(?, ?, ?)}");Then you can set values to the place holders using the setter methods of the CallableStatement interface and execute ... Read More

Among all 4 JDBC driver types, when to use which driver?

Nancy Den
Updated on 30-Jul-2019 22:30:25

108 Views

If you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is 4.If your Java application is accessing multiple types of databases at the same time, type 3 is the preferred driver.Type 2 drivers are useful in situations, where a type 3 or type 4 driver is not available yet for your database.The type 1 driver is not considered a deployment-level driver, and is typically used for development and testing purposes only.

What is the difference between execute(), executeQuery() and executeUpdate() methods in JDBC?

Nancy Den
Updated on 30-Jul-2019 22:30:25

17K+ Views

Once you have created the statement object you can execute it using one of the execute methods of the Statement interface namely, execute(), executeUpdate() and, executeQuery().The execute() method: This method is used to execute SQL DDL statements, it returns a boolean value specifying weather the ResultSet object can be retrieved.Exampleimport java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class Example {    public static void main(String args[]) throws SQLException {       //Registering the Driver       DriverManager.registerDriver(new com.mysql.jdbc.Driver());       //Getting the connection       String mysqlUrl = "jdbc:mysql://localhost/sampleDB";       Connection con = ... Read More

What are Stored procedures in JDBC?

Nancy Den
Updated on 30-Jul-2019 22:30:25

234 Views

Stored procedures are sub routines, segment of SQL statements which are stored in SQL catalog. All the applications that can access Relational databases (Java, Python, PHP etc.), can access stored procedures.Stored procedures contain IN and OUT parameters or both. They may return result sets in case you use SELECT statements. Stored procedures can return multiple result sets.Creating a Stored procedureSuppose, we have created a table named Employee in MySQL database as shown below:String createTable = "CREATE TABLE Employee("    + "Name VARCHAR(255), "    + "Salary INT NOT NULL, "    + "Location VARCHAR(255))";Following is an example of a MySQL ... Read More

What is CallableStatement in JDBC?

Nancy Den
Updated on 30-Jul-2019 22:30:25

6K+ Views

The CallableStatement interface provides methods to execute the stored procedures. Since the JDBC API provides a stored procedure SQL escape syntax, you can call stored procedures of all RDBMS in single standard way.Creating a CallableStatementYou can create an object of the CallableStatement (interface) using the prepareCall() method of the Connection interface. This method accepts a string variable representing a query to call the stored procedure and returns a CallableStatement object.A Callable statement can have input parameters, output parameters or both. To pass input parameters to the procedure call you can use place holder and set values to these using the ... Read More

What is PreparedStatement in JDBC?

Nancy Den
Updated on 30-Jul-2019 22:30:25

9K+ Views

The PreparedStatement interface extends the Statement interface it represents a precompiled SQL statement which can be executed multiple times. This accepts parameterized SQL quires and you can pass 0 or more parameters to this query.Initially this statement uses place holders “?” instead of parameters, later on you can pass arguments to these dynamically using the setXXX() methods of the PreparedStatement interface.Creating a PreparedStatementYou can create an object of the PreparedStatement (interface) using the prepareStatement() method of the Connection interface. This method accepts a query (parameterized) and returns a PreparedStatement object.When you invoke this method the Connection object sends the given ... Read More

Advertisements