The document outlines the basic steps to develop a JDBC application: 1) Register the JDBC driver by loading it using Class.forName(), 2) Establish a database connection using DriverManager.getConnection(), 3) Create and execute SQL statements using Statement or PreparedStatement objects, 4) Process query results, and 5) Close the database connection. It also provides code examples for loading drivers, establishing connections, creating statements, handling exceptions, and more.
The document outlines the basic steps to develop a JDBC application: 1) Register the JDBC driver by loading it using Class.forName(), 2) Establish a database connection using DriverManager.getConnection(), 3) Create and execute SQL statements using Statement or PreparedStatement objects, 4) Process query results, and 5) Close the database connection. It also provides code examples for loading drivers, establishing connections, creating statements, handling exceptions, and more.
The document outlines the basic steps to develop a JDBC application: 1) Register the JDBC driver by loading it using Class.forName(), 2) Establish a database connection using DriverManager.getConnection(), 3) Create and execute SQL statements using Statement or PreparedStatement objects, 4) Process query results, and 5) Close the database connection. It also provides code examples for loading drivers, establishing connections, creating statements, handling exceptions, and more.
Register the JDBC Drive Establish a Database Connection Create and Execute an SQL Statement Process the Results Close the Database Connection Loading a Driver using the Class.forName() method. Invoking the Class.forName() method creates an instance of the specified driver class and registers it with the DriverManager class. Once the driver is loaded successfully, the connection with a database can be established. Class.forName() For mysql Class.forName("com.mysql.jdbc.Driver"); Establishing a Connection using the DriverManager.getConnection() Connection URL The connection URL specifies necessary information that will be required for connecting to a particular database. Syntax: protocol: <subprotocol>:<subname> DriverManager.getConnection() Syntax: Connection cn = DriverManager.getConnection(connection url,username ,password ); For mysql Connection con=DriverManager.getConnection("jdbc:mysql://loca lhost:3306/shazia","root",""); Creating Statements and Queries Statement object is the most frequently used object to execute SQL queries that do not need any parameters to be passed.
A Statement object can be classified into three
categories based on the type of SQL statements sent to the database. Statement and PreparedStatement is inherited from Statement interface. Statement st = con.createStatement(); executeQuery() select ResultSet rs = st.executeQuery(“SELECT * FROM Department”); executeUpdate()->insert,update,delete The executeUpdate() method is used to execute INSERT, DELETE, UPDATE, and other SQL DDL (Data Definition Language) such as CREATE TABLE, DROP TABLE, and so on Creating Parameterized Queries PreparedStatement object implements the execution of dynamic SQL statements with parameters. Creating Parameterized Query The PreparedStatement object is created by using the preparedStatement() method of Connection class. String sqlStmt = “UPDATE Employees SET Dept_ID = ? WHERE Dept_Name LIKE ?”; PreparedStatement pStmt = cn.prepared Passing Parameters At the time of compilation, the parameter values are passed and used in the place of “?” placeholder. While compiling, the placeholder becomes a part of the statement and appears as static to the compiler. To substitute the “?” placeholder with a supplied value, one of the setXXX() method of the requird primitive type is used. pStmt.setInt(1, 25); pStmt.setString(2, “Production”); Executing Parameterized Query The executeUpdate() method is used to execute both the Statement and the PreparedStatement objects. This is associated with the tasks like update, insert, and delete. The return type of this method is integer pStmt.executeUpdate(); Handling Exceptions in JDBC applications ClassNotFoundException While loading a driver using Class.forName(), if the class for the specified driver is not present in the package, then the method invocation throws a ClassNotFoundException SQLException Every method defined by the JDBC objects such as Connection, Statement, and ResultSet can throw java.sql.SQLException. Hence, whenever, these methods are used, they should be wrapped inside a try/catch block to handle the exceptions.