Statement stmt = connection.createStatement();
ResultSet result = stmt.executeQuery("select * from Students");
Or
String sql = "select * from Students";
PreparedStatementstmt = con.prepareStatement(sql);
ResultSet result = stmt.executeQuery();
Methods | Description |
---|---|
public boolean absolute(int row) | Moves the cursor to the specified row in the ResultSet object. |
public void beforeFirst( ) | It moves the cursor just before the first row i.e. front of the ResultSet. |
public void afterLast() | Moves the cursor to the end of the ResultSet object, just after the last row. |
public boolean first() | Moves the cursor to first value of ResultSet object. |
public boolean last( ) | Moves the cursor to the last row of the ResultSet object. |
public boolean previous ( ) | Just moves the cursor to the previous row in the ResultSet object. |
public boolean next( ) | It moves the curser forward one row from its current position. |
public intgetInt(intcolIndex) | It retrieves the value of the column in current row as int in given ResultSet object. |
public String getString( intcolIndex) | It retrieves the value of the column in current row as int in given ResultSet object. |
public void relative(int rows) | It moves the cursor to a relative number of rows. |
import java.sql.*;
classResultSetTest
{
public static void main(String args[])
{
Connection con = null;
Statement stmt = null;
ResultSetrs = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "scott", "tiger");
stmt = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("Select * from Student");
rs.absolute(5); //Accessing 5th row
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
rs.close();
stmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Methods | Description |
---|---|
public String getColumnClassName(int column) | It returns the name of the Java class whose instances are created. |
public intgetColumnCount() | It returns the number of columns in the ResultSet object. |
public String getColumnName(int column) | Returns the column name from the ResutlSet object. |
public intgetColumnType(int column) | It retrieves the column’s which is designated in SQL. |
public String getSchemaName(int column) | Return the table’s schema which designed with column. |
public String getTableName(int column) | Returns the designed SQL table’s name. |
//RSMDTest.java
import java.sql.*;
classRSMDTest
{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "scott", "tiger");
PreparedStatementps = con.prepareStatement("select * from Students");
ResultSetrs = ps.executeQuery();
ResultSetMetaDatarsmd = rs.getMetaData();
for (int i = 1; i <= cols; i++)
{
String colName = rsmd.getColumnName(i);
String colType = rsmd.getColumnTypeName(i);
System.out.println(colName+" of type "+colType);
}
rs.close();
ps.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Methods | Description |
---|---|
public Connection getConnection( ) | It retrieves the connection that produced the given metadata object. |
public intgetDatabaseMajorVersion() | Returns the major version number of the database. |
public intgetDatabaseMinorVersion() | Returns the minor version number of the database. |
public String getDatabaseProductName( ) | Used to retrieve the name of this database product. |
public String getDriverName( ) | Retrieves the name of the JDBC driver which is used in application. |
public String getURL( ) | It returns the URL of the current DBMS. |
import java.sql.*;
classDatabaseMetaDataDemo
{
public static void main(String args[])
{
Connection conn = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger");
DatabaseMetaDatadbmd = conn.getMetaData();
System.out.println("Driver name "+dbmd.getDriverName());
System.out.println("Driver Version "+dbmd.getDriverVersion());
System.out.println("Database Product name "+dbmd.getDatabaseProductName());
System.out.println("Database product version "+dbmd.getDatabaseProductVersion());
System.out.println("Username "+dbmd.getUserName());
Sysetm.out.println("URL of database "+dbmd.getURL());
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
if(conn != null)
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
}