© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

Home Placements Interview Questions Java Interview Questions And Answers Java Interview Questions On JDBC Conectivity ▼

Java Interview Questions On JDBC Conectivity

15. How can you create JDBC statements?

Answer
A Statement object is what sends your SQL statement to the DBMS. You simply create a Statement object and then execute it, 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. E.g. 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();

16. How to make a query?

Answer

Create a Statement object and calls 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 aDatabase ");

17. How can you retrieve data from the ResultSet?

Answer
Use get methods to retrieve data from 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

18. How to 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:

  • beforeFirst(): Default position. Puts cursor before the first row of the result set.
  • first(): Puts cursor on the first row of the result set.
  • last(): Puts cursor before the last row of the result set.
  • afterLast() Puts cursor beyond last row of the result set. Calls to previous moves backwards through the ResultSet.
  • absolute(pos): Puts cursor at the row number position where absolute(1) is the first row and absolute(-1) is the last row.
  • relative(pos): Puts cursor at a row relative to its current position where relative(1) moves row cursor one row forward.
Previous Next
1 2 3 4 5 6 7 8
SLogix Student Projects
bottom