{"id":67,"date":"2018-07-10T10:35:33","date_gmt":"2018-07-10T10:35:33","guid":{"rendered":"http:\/\/itp16.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=67"},"modified":"2019-05-15T07:12:13","modified_gmt":"2019-05-15T07:12:13","slug":"jdbc2","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/chapter\/jdbc2\/","title":{"rendered":"JDBC2"},"content":{"raw":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/Hvcq2RLLtnk\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div style=\"text-align: justify;\">\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Executing aprecompiled SQL using Prepared Statement<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">An instance of Statement is used to execute any kind of SQL. There is a \u00a0sub-interface \u00a0of \u00a0Statement called PreparedStatement, which is used to execute precompiled SQL. An instance of PreparedStatement is also created from the instance of Connection by using the following methods of Connection\u00a0\u00a0 interface:<\/p>\r\n&nbsp;\r\n\r\nPreparedStatement prepareStatement(String sql)\r\n\r\nPreparedStatement prepareStatement(String sql, int rstype, int rsconcur)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Here unlike the Statement the SQL is specified at the time of creating the instance of PreparedStatement. The SQL, which is passed as parameter can have place holders which are introduced by using the \u2019 ?\u2019 character within the SQL string passed as parameter. These place holders are referred to as parameters for the PreparedStatement.<\/p>\r\n&nbsp;\r\n\r\nPreparedStatement ps = con.prepareStatement(\"select * from account where acno=? and type = ?\");\r\n\r\nPreparedStatement ps = con.prepareStatement(\"insert into account values(?,?,?,?)\");\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The first \u2019 ?\u2019 in the SQL string is the first parameter of the PreparedStatement, and so on. So, \u00a0the number of parameters in a\u00a0 PreparedSTatement will be\u00a0 the number of \u2019 ?\u2019\u00a0 characters used in the SQL string. The parameters can only be used in place for values in the SQL statement. These \u00a0parameter values need to be setup before the precompiled SQL corresponding to the PreparedStatement can be executed.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Now the PreparedStatement interface has additional methods for executing the SQL. These are the same three methods as in Statement interface, but without any parameters. In Statement interface the parameter to the three execute methods was the SQL to be executed. In case of PreparedStatement this has already been specified at the time of creating the instance using the parameter in the prepareStatement() method. So, the methods for executing the precompiled SQL in a PreparedStatement are:<\/p>\r\n&nbsp;\r\n\r\nboolean execute() int executeUpdate()\r\n\r\nResultSet executeQuery()\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">Before invoking any of the execute methods on an instance of PreparedStatement, the values of all the parameters in the PreparedStatement, which are indicated using the \u2019 ?\u2019 character, must be initialized. To set the values for the parameters in a PreparedStatement, the PreparedStatement interface has a set of methods. The method to be used depends on the type (SQL type) of the parameter(place holder) whose value is to be set. The general form of these methods is as given below:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void setXXX (int parno, XXX value)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">where XXX is the type of parameter. eg. If the SQL type of the parameter whose value is to be set is a VARCHAR, then String type in Java would be more appropriate. and we would use the method<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void set String ( int param , String value )<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Similarly there are other methods for setting parameter values of other types.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So we first use the appropriate setXXX() methods to set values of the parameters in the PreparedStatement and then use the execute method \u00a0on \u00a0the \u00a0instance \u00a0of \u00a0PreparedStatement \u00a0to execute the precompiled SQL.<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Executing Stored Procedures and functions using CallableStatement<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">There is also\u00a0 a sub-interface\u00a0 of PreparedStatement\u00a0 called the\u00a0 CallableStatement, this\u00a0 is used to execute stored procedures and functions. The instance of CallableStatement is created by the method on Connection as<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">CallableStatement prepareCall(String callstatement)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">CallableStatement prepareCall(String callstatement,int rstype,int rsconcur)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The CallableStatement inherits methods from the PreparedStatement and some more additional methods. let us look at how to create an instance of CallableStatement. In the above methods \u00a0the syntax for the callstatement is:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">{ [?] = call &lt;proc-name or function name&gt; (?,...} }<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">eg. if it is a stored procedure which is to be invoked we create CallableStatement as<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">CallableStatement cs = con.prepareCall(\"{ call myproc(?,?,?) }\");<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">to execute a stored procedure or<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">CallableStatement cs = con.prepareCall(\"{ ? = call myfunctino(?, ?, ?) }\");<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">to execute a function, which returns some value.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">In the above stored procedure invocation we assume that \u201cmyproc\u201d stored procedure has 3 parameters required. Now, the parameters in storedprocedure are of 3 types, ie. there are some parameters which are IN parameters, some are OUT parameters and some are INOUT parameters. This is decided when the procedure has been created in the database. The IN parameters and the IN- OUT parameters need to be initialized before using the execute method on the instance of CallableStatement. This initialization can be done similar to the PreparedStatement. ie. by calling the setXXX methods corresponding to the parameters, which are IN and INOUT types. Now before calling the execute methods the OUT parameters need to be registered, this can be done by using the method:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void registerOut Parameter(int parno, int sqldatatype)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The value in the second parameter \u00a0of \u00a0the \u00a0registerOutParameter() \u00a0is \u00a0the \u00a0SQL \u00a0datatype \u00a0constants defined in the java.sql.Types class. Corresponding to the various standard ANSI SQL data types, there is a constant in the java.sql.Types class. We have the following kinds of declarations in the Types class.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public static final int VARCHAR = 12 public static final int CHAR = 1\u00a0 public static final int NUMERIC = 2 public static final int DECIMAL = 3 public static final int INTEGER = 4<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">So the sequence for executing a stored procedure will be as follows:<\/span><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n<ul>\r\n \t<li>Have an instance of CallableStatement with the appropriate parameters.<\/li>\r\n \t<li>Then set up the values of the IN and the INOUT parameters using the setXXX methods.<\/li>\r\n \t<li>Register the OUT parameters using the registerOutParameter method.<\/li>\r\n \t<li>Then finally call the appropriate execute method.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">After the execute method has finished, the stored procedure has executed in the database, and \u00a0the values of the OUT and the INOUT parameters are now available for reading. For reading the values of the OUT and the INOUT parameters we can use the getXXX methods which are available in the CallableStatement. So, after execute we may use the getXXX methods and read the values of the OUT and INOUT parameters. In case of executing a function. we can have parameter no 1 as an OUT parameter which is put before the call statement in the String parameter of the prepareCall() method.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">This would be similar to executing a stored procedure, except for that the parameter no 1 is an OUT parameter whose value will be read using the getXXX method after calling the appropriate execute method.<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Examining the results of a Query from the ResultSet<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">The resultSet contains data from the database. This data is made up of rows. The columns of the resultset give information about the data in the rows.<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Examining the Meta Information from ResultSetMetaData<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">The information about the columns in the resultset is available \u00a0from \u00a0an \u00a0instance \u00a0of ResultSetMetaData which can be obtained from the ResultSet instance by using the \u00a0method getMetaData() on the ResultSet. ie. the ResultSet interface<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">has a method as follows:<\/span>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">ResultSetMetaData getMetaData()<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">Now to get information about the columns \u00a0from \u00a0the \u00a0instance \u00a0of \u00a0ResultSetMeta- \u00a0Data \u00a0we \u00a0have methods as follows:<\/span><\/p>\r\n&nbsp;\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">int getColumnCount () \/\/ this returns the no. of<\/span>\r\n\r\n<span style=\"text-align: initial; font-size: 1em;\">\/\/ columns in the ResultSet.<\/span>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify;\">All other methods in the ResultSetMetaData have one parameter which is int, indicating the column number for which we want the information. The column number starts from column number 1 and not 0. The other methods inResultSetMetaData are:<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">String getColumnName(int colno) String getColumnLabel(int colno)\u00a0 int getColumnDisplaySize(int colno) int getColumnType(int colno)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">The getColumnType method returns the sql data type. like varchar, numeric, date, etc. For all the valid sql data types there is an integer constant available in the java.sql.Types class. This class contains only constants, for the SQL types.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">The ResultSetMetaData also contains method called<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String getColumnTypeName (int colno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">This would return the String name of the sql type. continuing with methods of ResultSetMetaData:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String\u00a0\u00a0 \u00a0getColumnLabel(int colno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">int getScale(int colno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">int getPrecision(int colno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String getSchemaName(int colno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String getTableName(int colno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean\u00a0 \u00a0isAutoIncrement(int colno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean\u00a0 \u00a0isNullable(int colno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String \u00a0getColumnClassName(int colno)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The data which is in the database is of SQL type, this has to be used from java. eg. we may have a column which is VARCHAR, and if we want to use it in Java application, we would use it as String in java. There are two things, one is the SQL data type that is used in database, another is java type that we want to use from java application. For every SQL data type, there is some java data type which is most appropriate for representing its value in Java. eg. for VARCHAR SQL type, String is most appropriate, for NUMERIC data type float may be most appropriate, for BLOB SQL type, we have java.sql.Blob. For a given column if we want to know the most appropriate Java data type, we can use the method getColumnClassName().<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Examining the data from the ResultSet<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Now coming back to ResultSet. This contains the data ie. the rows. When we want to get information\u00a0<\/span><span style=\"font-size: 1em;\">from ResultSet, we first need to position on the row from which data is desired. In ResultSet \u00a0we always have a row position. Initially current row position is before the first row. We normally use the method next() on the ResultSet to move\u00a0 to the next row. Following are the methods which help us to navigate within a ResultSet:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean next()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean previous() \/\/ cannot be used in forward only resultset. boolean first()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean last() void beforeFirst() void afterLast(0<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean absolute(int rowno)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean relative(int count) \/\/ could be negative to move backwards.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">We can know the current row number by using the method getRow().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">int getRow() boolean isBeforeFirst() boolean isAfterlast() boolean isFirst() boolean isLast()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Once we are positioned on a row, we may now get the data from that row for any of its column. Again, depending on the type of column we have to fetch data from, we use different method to fetch the data. eg. if the column type is VARCHAR, then we use the method<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String getString(int colno) or String getString(String colname)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The ResultSet interface has methods for various datatypes to get the data. like int getInt(...) and so on. we normally refer to these set of methods as getXXX methods. where XXX would be appropriate java datatype. for most of the data types, using getString(..) would work. so even if u have a numeric field or a data field u may still use getString(...) to extract data in String form. the getDate(...) method returns java.sql.Date. there is java.sql.Date class which is a subclass of java.util.Date. You also have java.sql.Time, and java.sql.TimeStame which are subclasses of java.util.Date. You also have interfaces like Array, Blob, Clob, Ref and Struct to take care of the corresponding sql data types. In ResultSet you also have a method<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Object getObject(...)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">This would use the appropriate getXXX(...) method and return its instance. The return type is Object and hence it may return any kind of instance. so if the column corresponds to data sql type it would return the object of type java.sql.Date So many times it might be appropriate to use getObject(...).<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Updating an updatable ResultSet<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Now in case the ResultSet is updatable. then we can also use methods which can update into the ResultSet. for updates we may go for either deleting row, updating row or inserting row into the resultset. If we want to delete row then we position on the row u want to delete and then call the method<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void deleteRow()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">If we want to update a row in the resultset then we position on the row we want to update, then we call the appropriate updateXXX(....) methods to update the various column values in the current row. The updateXXX(...) methods are:<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void updateXXX(int colno, XXX value)<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void updateXXX(String colname, XXX value)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">now in order to update the values updated in this row we need to call the method void updateRow() on the resultSet, you may cancel the updateXXX by calling the method<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void cancelUpdate() instead of the updateRow().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">ie. after calling updateXXX(...) for changing the values in the current row u may use either cancelUpdate() \u00a0or \u00a0updateRow().<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">If u want to insert rows into the resultset, then there is special row called the insertrow, so the sequence will be.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">call the method<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void moveToInsertRow()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\">then call the appropriate updateXXX(..) to set up the values in the columns for the row to be inserted. and then call the method insertRow() to insert the row. you can now insert any no. of rows by simply calling updateXXX(...) followed by insertRow(). the method moveToInsertRow() is like moving into insert mode. when you want to come out of insert mode you call the method<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void moveToCurrentRow() this will take you back from insert mode to normal mode.<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Managing transactions<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">In SQL, you can have transactions. it requires \u00a0that \u00a0the \u00a0auto \u00a0commit \u00a0should \u00a0be \u00a0off. \u00a0The \u00a0transactions can be used from JDBC also. for this the Connection interface has methods like<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void\u00a0\u00a0 setAutoCommit(boolean autocommit) boolean getAutoCommit()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">you can call setAutoCommit(false) on an instance of Connection and then whatever DML you execute using this <\/span>Connection<span style=\"font-size: 1em;\">, is in a transaction. The DML will be executed using the Statement or PreparedStatement. Now you may decide to either commit or rollback the transaction. for this Connection interface has methods like<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void rollback() void commit()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">You like to define savepoints in you transaction and then maybe rollback to a savepoint. To define savepoints in the transaction, you have methods like<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Savepoint setSavepoint()<\/span><\/p>\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Savepoint setSavepoint(String name)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">you can rollback to a specified savepoint by using the method<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void rollback(Savepoint sp)<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The Connection interface has also another method called getMetaData which returns an instance of DatabaseMetaData.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">the DatabaseMetaData instance can be used to find information about the database, like which version of database, which jdbc driver version is being used, what are the capabilities of the database. for all this u can refer to the methods in the DatabaseMetaData in the api.<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">SQL Exception<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Also almost all the methods in the java.sql package throw SQLException which is in the java.sql package. When we execute an sql, it can result in error. The SQLException instance also encapsulates the vendor specific error codes, which can be found by using the method<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">int getErrorCode()<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">for eg. in case of Oracle we get the ora error no. This will be returned in the getErrorCode method.<\/span><\/p>\r\n&nbsp;\r\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">In Summary:<\/strong><\/p>\r\n\r\n<\/div>\r\n<div style=\"text-align: justify;\">\r\n<ul>\r\n \t<li style=\"text-align: justify;\">instances of Statement, PreparedStatement and CallableStatement are used to execute any sql, precompile-sql and stored-procedure respectively. These are obtained from the \u00a0instance \u00a0of Connection.<\/li>\r\n \t<li style=\"text-align: justify;\">The statement instances provide the methods to execute the sql, precompiled-sql or the stored-procedure. The method executeQuery() is used to obtain the results of executing a select query. The result of executing a select query are available as instance of ResultSet.<\/li>\r\n \t<li style=\"text-align: justify;\">The information about the columns in a ResultSet are available from the instance of ResultSetMetaData associated with the ResultSet. The ResultSet has methods for row-wise navigation and for extracting the values in the various columns of the curren row. The result sets which are obtained, can be updatable, depending on the setting in the statement instance being used to execute the select query. The ResultSet has methods for making updates in the resultset in case it is updatable.<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on JDBC2<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/Hvcq2RLLtnk\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<p class=\"hanging-indent\"><strong style=\"font-size: 1em;\">Suggested Reading:<\/strong><\/p>\r\n\r\n<\/div>\r\n<ol>\r\n \t<li style=\"text-align: justify;\">Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson Education.<\/li>\r\n \t<li style=\"text-align: justify;\">The class of JAVA by Pravin Jain, Pearson Education.<\/li>\r\n \t<li style=\"text-align: justify;\">Database programming with JDBC and Java by George Reese. O'Reilly Media<\/li>\r\n \t<li style=\"text-align: justify;\">https:\/\/docs.oracle.com\/javase\/tutorial\/jdbc\/basics\/<\/li>\r\n<\/ol>","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/Hvcq2RLLtnk\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div style=\"text-align: justify;\">\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Executing aprecompiled SQL using Prepared Statement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">An instance of Statement is used to execute any kind of SQL. There is a \u00a0sub-interface \u00a0of \u00a0Statement called PreparedStatement, which is used to execute precompiled SQL. An instance of PreparedStatement is also created from the instance of Connection by using the following methods of Connection\u00a0\u00a0 interface:<\/p>\n<p>&nbsp;<\/p>\n<p>PreparedStatement prepareStatement(String sql)<\/p>\n<p>PreparedStatement prepareStatement(String sql, int rstype, int rsconcur)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Here unlike the Statement the SQL is specified at the time of creating the instance of PreparedStatement. The SQL, which is passed as parameter can have place holders which are introduced by using the \u2019 ?\u2019 character within the SQL string passed as parameter. These place holders are referred to as parameters for the PreparedStatement.<\/p>\n<p>&nbsp;<\/p>\n<p>PreparedStatement ps = con.prepareStatement(&#8220;select * from account where acno=? and type = ?&#8221;);<\/p>\n<p>PreparedStatement ps = con.prepareStatement(&#8220;insert into account values(?,?,?,?)&#8221;);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The first \u2019 ?\u2019 in the SQL string is the first parameter of the PreparedStatement, and so on. So, \u00a0the number of parameters in a\u00a0 PreparedSTatement will be\u00a0 the number of \u2019 ?\u2019\u00a0 characters used in the SQL string. The parameters can only be used in place for values in the SQL statement. These \u00a0parameter values need to be setup before the precompiled SQL corresponding to the PreparedStatement can be executed.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Now the PreparedStatement interface has additional methods for executing the SQL. These are the same three methods as in Statement interface, but without any parameters. In Statement interface the parameter to the three execute methods was the SQL to be executed. In case of PreparedStatement this has already been specified at the time of creating the instance using the parameter in the prepareStatement() method. So, the methods for executing the precompiled SQL in a PreparedStatement are:<\/p>\n<p>&nbsp;<\/p>\n<p>boolean execute() int executeUpdate()<\/p>\n<p>ResultSet executeQuery()<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Before invoking any of the execute methods on an instance of PreparedStatement, the values of all the parameters in the PreparedStatement, which are indicated using the \u2019 ?\u2019 character, must be initialized. To set the values for the parameters in a PreparedStatement, the PreparedStatement interface has a set of methods. The method to be used depends on the type (SQL type) of the parameter(place holder) whose value is to be set. The general form of these methods is as given below:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void setXXX (int parno, XXX value)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">where XXX is the type of parameter. eg. If the SQL type of the parameter whose value is to be set is a VARCHAR, then String type in Java would be more appropriate. and we would use the method<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void set String ( int param , String value )<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Similarly there are other methods for setting parameter values of other types.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">So we first use the appropriate setXXX() methods to set values of the parameters in the PreparedStatement and then use the execute method \u00a0on \u00a0the \u00a0instance \u00a0of \u00a0PreparedStatement \u00a0to execute the precompiled SQL.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Executing Stored Procedures and functions using CallableStatement<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">There is also\u00a0 a sub-interface\u00a0 of PreparedStatement\u00a0 called the\u00a0 CallableStatement, this\u00a0 is used to execute stored procedures and functions. The instance of CallableStatement is created by the method on Connection as<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">CallableStatement prepareCall(String callstatement)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">CallableStatement prepareCall(String callstatement,int rstype,int rsconcur)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The CallableStatement inherits methods from the PreparedStatement and some more additional methods. let us look at how to create an instance of CallableStatement. In the above methods \u00a0the syntax for the callstatement is:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">{ [?] = call &lt;proc-name or function name&gt; (?,&#8230;} }<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">eg. if it is a stored procedure which is to be invoked we create CallableStatement as<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">CallableStatement cs = con.prepareCall(&#8220;{ call myproc(?,?,?) }&#8221;);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">to execute a stored procedure or<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">CallableStatement cs = con.prepareCall(&#8220;{ ? = call myfunctino(?, ?, ?) }&#8221;);<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">to execute a function, which returns some value.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">In the above stored procedure invocation we assume that \u201cmyproc\u201d stored procedure has 3 parameters required. Now, the parameters in storedprocedure are of 3 types, ie. there are some parameters which are IN parameters, some are OUT parameters and some are INOUT parameters. This is decided when the procedure has been created in the database. The IN parameters and the IN- OUT parameters need to be initialized before using the execute method on the instance of CallableStatement. This initialization can be done similar to the PreparedStatement. ie. by calling the setXXX methods corresponding to the parameters, which are IN and INOUT types. Now before calling the execute methods the OUT parameters need to be registered, this can be done by using the method:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void registerOut Parameter(int parno, int sqldatatype)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The value in the second parameter \u00a0of \u00a0the \u00a0registerOutParameter() \u00a0is \u00a0the \u00a0SQL \u00a0datatype \u00a0constants defined in the java.sql.Types class. Corresponding to the various standard ANSI SQL data types, there is a constant in the java.sql.Types class. We have the following kinds of declarations in the Types class.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">public static final int VARCHAR = 12 public static final int CHAR = 1\u00a0 public static final int NUMERIC = 2 public static final int DECIMAL = 3 public static final int INTEGER = 4<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">So the sequence for executing a stored procedure will be as follows:<\/span><\/p>\n<\/div>\n<div style=\"text-align: justify;\">\n<ul>\n<li>Have an instance of CallableStatement with the appropriate parameters.<\/li>\n<li>Then set up the values of the IN and the INOUT parameters using the setXXX methods.<\/li>\n<li>Register the OUT parameters using the registerOutParameter method.<\/li>\n<li>Then finally call the appropriate execute method.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">After the execute method has finished, the stored procedure has executed in the database, and \u00a0the values of the OUT and the INOUT parameters are now available for reading. For reading the values of the OUT and the INOUT parameters we can use the getXXX methods which are available in the CallableStatement. So, after execute we may use the getXXX methods and read the values of the OUT and INOUT parameters. In case of executing a function. we can have parameter no 1 as an OUT parameter which is put before the call statement in the String parameter of the prepareCall() method.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em; text-align: initial;\">This would be similar to executing a stored procedure, except for that the parameter no 1 is an OUT parameter whose value will be read using the getXXX method after calling the appropriate execute method.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Examining the results of a Query from the ResultSet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">The resultSet contains data from the database. This data is made up of rows. The columns of the resultset give information about the data in the rows.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Examining the Meta Information from ResultSetMetaData<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">The information about the columns in the resultset is available \u00a0from \u00a0an \u00a0instance \u00a0of ResultSetMetaData which can be obtained from the ResultSet instance by using the \u00a0method getMetaData() on the ResultSet. ie. the ResultSet interface<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">has a method as follows:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">ResultSetMetaData getMetaData()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: justify; font-size: 1em;\">Now to get information about the columns \u00a0from \u00a0the \u00a0instance \u00a0of \u00a0ResultSetMeta- \u00a0Data \u00a0we \u00a0have methods as follows:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">int getColumnCount () \/\/ this returns the no. of<\/span><\/p>\n<p><span style=\"text-align: initial; font-size: 1em;\">\/\/ columns in the ResultSet.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">All other methods in the ResultSetMetaData have one parameter which is int, indicating the column number for which we want the information. The column number starts from column number 1 and not 0. The other methods inResultSetMetaData are:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">String getColumnName(int colno) String getColumnLabel(int colno)\u00a0 int getColumnDisplaySize(int colno) int getColumnType(int colno)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">The getColumnType method returns the sql data type. like varchar, numeric, date, etc. For all the valid sql data types there is an integer constant available in the java.sql.Types class. This class contains only constants, for the SQL types.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">The ResultSetMetaData also contains method called<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String getColumnTypeName (int colno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">This would return the String name of the sql type. continuing with methods of ResultSetMetaData:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String\u00a0\u00a0 \u00a0getColumnLabel(int colno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">int getScale(int colno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">int getPrecision(int colno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String getSchemaName(int colno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String getTableName(int colno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean\u00a0 \u00a0isAutoIncrement(int colno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean\u00a0 \u00a0isNullable(int colno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String \u00a0getColumnClassName(int colno)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The data which is in the database is of SQL type, this has to be used from java. eg. we may have a column which is VARCHAR, and if we want to use it in Java application, we would use it as String in java. There are two things, one is the SQL data type that is used in database, another is java type that we want to use from java application. For every SQL data type, there is some java data type which is most appropriate for representing its value in Java. eg. for VARCHAR SQL type, String is most appropriate, for NUMERIC data type float may be most appropriate, for BLOB SQL type, we have java.sql.Blob. For a given column if we want to know the most appropriate Java data type, we can use the method getColumnClassName().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Examining the data from the ResultSet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Now coming back to ResultSet. This contains the data ie. the rows. When we want to get information\u00a0<\/span><span style=\"font-size: 1em;\">from ResultSet, we first need to position on the row from which data is desired. In ResultSet \u00a0we always have a row position. Initially current row position is before the first row. We normally use the method next() on the ResultSet to move\u00a0 to the next row. Following are the methods which help us to navigate within a ResultSet:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean next()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean previous() \/\/ cannot be used in forward only resultset. boolean first()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean last() void beforeFirst() void afterLast(0<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean absolute(int rowno)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">boolean relative(int count) \/\/ could be negative to move backwards.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">We can know the current row number by using the method getRow().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">int getRow() boolean isBeforeFirst() boolean isAfterlast() boolean isFirst() boolean isLast()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Once we are positioned on a row, we may now get the data from that row for any of its column. Again, depending on the type of column we have to fetch data from, we use different method to fetch the data. eg. if the column type is VARCHAR, then we use the method<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">String getString(int colno) or String getString(String colname)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The ResultSet interface has methods for various datatypes to get the data. like int getInt(&#8230;) and so on. we normally refer to these set of methods as getXXX methods. where XXX would be appropriate java datatype. for most of the data types, using getString(..) would work. so even if u have a numeric field or a data field u may still use getString(&#8230;) to extract data in String form. the getDate(&#8230;) method returns java.sql.Date. there is java.sql.Date class which is a subclass of java.util.Date. You also have java.sql.Time, and java.sql.TimeStame which are subclasses of java.util.Date. You also have interfaces like Array, Blob, Clob, Ref and Struct to take care of the corresponding sql data types. In ResultSet you also have a method<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Object getObject(&#8230;)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">This would use the appropriate getXXX(&#8230;) method and return its instance. The return type is Object and hence it may return any kind of instance. so if the column corresponds to data sql type it would return the object of type java.sql.Date So many times it might be appropriate to use getObject(&#8230;).<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Updating an updatable ResultSet<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Now in case the ResultSet is updatable. then we can also use methods which can update into the ResultSet. for updates we may go for either deleting row, updating row or inserting row into the resultset. If we want to delete row then we position on the row u want to delete and then call the method<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void deleteRow()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">If we want to update a row in the resultset then we position on the row we want to update, then we call the appropriate updateXXX(&#8230;.) methods to update the various column values in the current row. The updateXXX(&#8230;) methods are:<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void updateXXX(int colno, XXX value)<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void updateXXX(String colname, XXX value)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">now in order to update the values updated in this row we need to call the method void updateRow() on the resultSet, you may cancel the updateXXX by calling the method<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void cancelUpdate() instead of the updateRow().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">ie. after calling updateXXX(&#8230;) for changing the values in the current row u may use either cancelUpdate() \u00a0or \u00a0updateRow().<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">If u want to insert rows into the resultset, then there is special row called the insertrow, so the sequence will be.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">call the method<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void moveToInsertRow()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">then call the appropriate updateXXX(..) to set up the values in the columns for the row to be inserted. and then call the method insertRow() to insert the row. you can now insert any no. of rows by simply calling updateXXX(&#8230;) followed by insertRow(). the method moveToInsertRow() is like moving into insert mode. when you want to come out of insert mode you call the method<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void moveToCurrentRow() this will take you back from insert mode to normal mode.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">Managing transactions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">In SQL, you can have transactions. it requires \u00a0that \u00a0the \u00a0auto \u00a0commit \u00a0should \u00a0be \u00a0off. \u00a0The \u00a0transactions can be used from JDBC also. for this the Connection interface has methods like<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void\u00a0\u00a0 setAutoCommit(boolean autocommit) boolean getAutoCommit()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">you can call setAutoCommit(false) on an instance of Connection and then whatever DML you execute using this <\/span>Connection<span style=\"font-size: 1em;\">, is in a transaction. The DML will be executed using the Statement or PreparedStatement. Now you may decide to either commit or rollback the transaction. for this Connection interface has methods like<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void rollback() void commit()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">You like to define savepoints in you transaction and then maybe rollback to a savepoint. To define savepoints in the transaction, you have methods like<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Savepoint setSavepoint()<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">Savepoint setSavepoint(String name)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">you can rollback to a specified savepoint by using the method<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">void rollback(Savepoint sp)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">The Connection interface has also another method called getMetaData which returns an instance of DatabaseMetaData.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">the DatabaseMetaData instance can be used to find information about the database, like which version of database, which jdbc driver version is being used, what are the capabilities of the database. for all this u can refer to the methods in the DatabaseMetaData in the api.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">SQL Exception<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 1em;\">Also almost all the methods in the java.sql package throw SQLException which is in the java.sql package. When we execute an sql, it can result in error. The SQLException instance also encapsulates the vendor specific error codes, which can be found by using the method<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">int getErrorCode()<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-align: initial; font-size: 1em;\">for eg. in case of Oracle we get the ora error no. This will be returned in the getErrorCode method.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p class=\"hanging-indent\" style=\"text-align: justify;\"><strong style=\"text-align: initial; text-indent: -1em; font-size: 1em;\">In Summary:<\/strong><\/p>\n<\/div>\n<div style=\"text-align: justify;\">\n<ul>\n<li style=\"text-align: justify;\">instances of Statement, PreparedStatement and CallableStatement are used to execute any sql, precompile-sql and stored-procedure respectively. These are obtained from the \u00a0instance \u00a0of Connection.<\/li>\n<li style=\"text-align: justify;\">The statement instances provide the methods to execute the sql, precompiled-sql or the stored-procedure. The method executeQuery() is used to obtain the results of executing a select query. The result of executing a select query are available as instance of ResultSet.<\/li>\n<li style=\"text-align: justify;\">The information about the columns in a ResultSet are available from the instance of ResultSetMetaData associated with the ResultSet. The ResultSet has methods for row-wise navigation and for extracting the values in the various columns of the curren row. The result sets which are obtained, can be updatable, depending on the setting in the statement instance being used to execute the select query. The ResultSet has methods for making updates in the resultset in case it is updatable.<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on JDBC2<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/Hvcq2RLLtnk\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"hanging-indent\"><strong style=\"font-size: 1em;\">Suggested Reading:<\/strong><\/p>\n<\/div>\n<ol>\n<li style=\"text-align: justify;\">Core Java Volume 2 by Cay Horstmann &amp; Gary Cornell, Ninth Edition, Pearson Education.<\/li>\n<li style=\"text-align: justify;\">The class of JAVA by Pravin Jain, Pearson Education.<\/li>\n<li style=\"text-align: justify;\">Database programming with JDBC and Java by George Reese. O&#8217;Reilly Media<\/li>\n<li style=\"text-align: justify;\">https:\/\/docs.oracle.com\/javase\/tutorial\/jdbc\/basics\/<\/li>\n<\/ol>\n","protected":false},"author":4,"menu_order":9,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["mr-pravin-jain"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-67","chapter","type-chapter","status-publish","hentry","contributor-mr-pravin-jain"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":11,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/67\/revisions"}],"predecessor-version":[{"id":396,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/67\/revisions\/396"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapters\/67\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/media?parent=67"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/pressbooks\/v2\/chapter-type?post=67"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/contributor?post=67"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp16\/wp-json\/wp\/v2\/license?post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}