eWebProgrammer eweb





JDBC - Part 2  «Prev  Next»
  1. What technique can be used to load the drivers?
    Answer:
    Loading the driver or drivers you want to use is involves just one line of code.
    If you want to use the JDBC-ODBC Bridge driver, the following code will load it:
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    

    Your driver documentation will give you the class name to use.
    If the class name is jdbc.DriverXYZ , you would load the driver with the following line of code:
    Class.forName("jdbc.DriverXYZ");
    
  2. What will
    Class.forName
    
    do while loading the drivers?
    Answer:

    It is used to create an instance of a driver and register it with the DriverManager.
    After you have loaded a driver, it becomes available for making a connection with a DBMS.
  3. How is a JDBC connection established?
    Answer:
    When establishing a connection, it is necessary to have the appropriate driver connect to the DBMS.
    The following line of code illustrates the concept:
    String url = "jdbc:odbc:Fred";
    Connection con = DriverManager.getConnection(url, "Fernanda", "J8");
    

  4. How can you create JDBC statements?
    Answer:

    A Statement object is what sends your SQL statement to the DBMS.
    Create a Statement object and then execute it while supplying the appropriate execute method with the SQL statement you want to send.
    For a SELECT statement, the method to use is executeQuery.
    For statements that create or modify tables, the method to use is executeUpdate.
    It takes an instance of an active connection to create a Statement object.
    In the following example, we use our Connection object con to create the Statement object stmt :
    Statement stmt = con.createStatement();
    
  5. How does one create a query?
    Answer:

    Create a Statement object and call the Statement.executeQuery method to select data from the database.
    The results of the query are returned in a ResultSet object.
    Statement stmt = con.createStatement();
    ResultSet results = stmt.executeQuery("SELECT data FROM Database ");
    
  6. How can you retrieve data from the ResultSet?
    Answer:

    Use getter methods to retrieve data from the returned ResultSet object.
    ResultSet rs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");
    String s = rs.getString("COF_NAME");
    

    The method getString is invoked on the ResultSet object rs , so getString will retrieve (get) the value stored in the column COF_NAME in the current row of rs.
  7. How does one navigate the ResultSet?
    Answer:

    By default, the result set cursor points to the row before the first row of the result set.
    A call to next() retrieves the first result set row.
    The cursor can also be moved by calling one of the following ResultSet methods:
    1. beforeFirst(): Default position. Puts cursor before the first row of the result set.
    2. first(): Puts cursor on the first row of the result set.
    3. last(): Puts cursor before the last row of the result set.
    4. afterLast() Puts cursor beyond last row of the result set.
      Calls to previous moves backwards through the ResultSet.
    5. absolute(pos): Puts cursor at the row number position where absolute(1) is the first row and absolute(-1) is the last row.
    6. relative(pos): Puts cursor at a row relative to its current position where relative(1) moves row cursor one row forward.
  8. What are the different types of JDBC Statements?
    Answer:
    1. Statement (use createStatement method)
    2. Prepared Statement (Use prepareStatement method)
    3. Callable Statement (Use prepareCall)
  9. If you want to use the percent sign (%) as the percent sign and not have it interpreted as the SQL wildcard used in SQL LIKE queries, how is this written?
    Answer:

    Use the escape keyword. For example:
    stmt.executeQuery("select tax from sales where tax like '10\%' {escape '\'}");
    
  10. How does one escape the ' symbol found in an input line?
    Answer:

    You may use a method to do so:
    static public String escapeLine(String s) {
      String retvalue = s;
      if (s.indexOf ("'") != -1 ) {
        StringBuffer hold = new StringBuffer();
        char c;
        for(int i=0; i < s.length(); i++ ) {
          if ((c=s.charAt(i)) == '\'' ) {
          hold.append ("''");
        }else {
          hold.append(c);
        }
      }
      retvalue = hold.toString();
      }
      return retvalue;
    }
    
    Note that such method can be extended to escape any other characters that the database driver may interpret.