Skip to main content

Posts

Showing posts from November, 2016

JDBC FAQ Part14

31:  how to update the database through PreparedStatement object. import java.sql.*; public class PreparedUpdateEx {             public static void main(String[] args)throws Exception             {                         Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con = DriverMadoeer.getConnection(“jdbc:odbc:doe”,”system”,”jhon”); PreparedStatement pst = con.prepareStatement(“update emp1 set esal = esal+? Where esal<?”); Pst.setInt(1,500); Pst.setFloat(2,10000.0f); Int count = pst.executeUpdate(); System.out.println(“no. of records updated:”+count);                         }             } 32: how to fetch the data from database through PreparedStatement object. import java.sql.*; public class UpdateResEx {             public static void main(String[] args)throws Exception             {                         Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con = DriverMadoeer.getCon

JDBC FAQ Part13

28:  how to display the data with the respective field names import java.sql.*; public class RSMD1 {             public static void main(String[] args)throws Exception             {                         Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); Connection con = DriverMadoeer.getConnection(“jdbc:odbc:doe”,”system”,”jhon”); Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSEITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs = st.executeQuery(“select * from emp1”);                                     ResultSetMetaData rsmd = rs.getMetaData(); System.out.println(rsmd.getColumnName(1)+”     “+rsmd.getColumnName(2)+”   “+rsmd.getColumnName(3)+”       “+rsmd.getColumnName(4)); System.out.println(“********************************”); while(rs.next()) { System.out.println(rs.getInt(1)+”    “+rs.getString(2)+”    “rs.getFloat(3)+”    “+rs.getString(4));                                     }                         }             }

JDBC FAQ Part12

26:   How to perform  updations on Database throws Updatable ResultSet? By using Updatable ResulSet object we are able to perform some updations on to the database. To perform updations on to the database through Updatable ResultSet object we will use the following steps. Step1:   Get the Updatable ResultSet objectd with the fetched data. Step2:   Move ResultSet cursor to a record where we want to perform updations, for this we will use the following method.                         public void absolute(int recordno.) Step3:   Perform Temporary updations on to the particular record, for this we will use following method.                         public void updateXxx(int fieldno,xxx value) Step4:   Make the temporary updation as a parmenent updation on to the Updatable ResultSet object as well as to the database. For this we will use the following method.                         public void updateRow() The following example demon

JDBC FAQ Part11

23:How to iterate the data from Scrollable ResultSet objuect in both forward and backword direction? to iterate the data in forward direction from a ResultSet object we will use the following 2 methods. public Boolean next() public xxx getXxx(int fieldno.)             Where xxx may be byte, short, char, int, long, float, double. To iterate the data in backward direction from Scrollable ResultSet object we will use the following 2 methods. public Boolean previous() public xxx getXxx(int fieldno)             Where previous() is a Boolean method, which can be used to check whether the previous record is available or not, if it is available then cursor will be moved to previous record position. The following example demonstrates how to iterate the data in both forward

JDBC FAQ Part10

20: What is ment by ResultSet and What are the types of ResultSets are available in JDBC application?             In jdbc applications ResultSets could be classified in the following two ways. On the basis of ResultSet privilizations (Concurancy):-  There are 2 types of ResultSets. Read only ResultSet Updatable ResultSet Read only ResultSet: -   It is a ResultSet, which will allow the users to read the        data only. To refer this ResultSet, we will use the following constant from ResultSet interface.                         public static final int CONCUR_READ_ONLY;  Updatable ResultSet:-   If is a ResultSet object, which will allow users to perform s

JDBC FAQ Part9

18: If we use selection group SQL query to executeUpdate() ,what happened? If we use selection group sql query as a parameter to executeUpdate(…) then JVM will send that sql query to the DBE, DBE will fetch the data and send  back to the java application here java application will store the fetched data in the form of ResultSet object. But executeUpdate() is expecting records  updated count value.             Due to this contradiction JVM will rise an exception like java.lang.SQLException. If we handle the above exception properly then we will get ResultSet abject and we will get the data from Database import java.sql.*; class Test { public static void main(String[] args) {             Statement st=null;             try             {                             Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);