Wednesday, 1 October 2014

Get column data using name of the column

Column names used as input to getter methods are case insensitive. When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned.

Method
Description
Array getArray(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as an Array object in Java.
InputStream getAsciiStream(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a stream of ASCII characters.
BigDecimal getBigDecimal(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.math.BigDecimal with full precision.
InputStream getBinaryStream(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a stream of uninterpreted bytes.
Blob getBlob(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a Blob object in Java.
boolean getBoolean(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a boolean in Java.
byte getByte(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a byte in Java.
byte[] getBytes(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a byte array in Java.
Reader getCharacterStream(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.io.Reader object.
Clob getClob(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a Clob object in Java.
Date getDate(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.sql.Date object in Java.
Date getDate(int columnLabel, Calendar cal)
Returns the value of the column in the current row of this ResultSet object as a java.sql.Date object in the Java (Using Appropriate Calendar).
double getDouble(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a double in Java.
float getFloat(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a float value in Java.
int getInt(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a int value in Java.
long getLong(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a long value in Java.
Reader getNCharacterStream(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.io.Reader object.
NClob getNClob(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a NClob object in Java.
String getNString(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a String in Java(Intended to use when accessing NCHAR,NVARCHAR and LONGNVARCHAR columns).
Object getObject(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as an Object in java.
T getObject(int columnLabel, Class<T> type)
Returns the value of the column in the current row of this ResultSet object as requested Java data type, if conversion supports.
Object getObject(int columnLabel, Map<String,Class<?>> map)
Returns the value of the column in the current row of this ResultSet object as Object in Java.This method uses the given Map object for the custom mapping of the SQL structured or distinct type that is being retrieved.
Ref getRef(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a Ref object in Java.
RowId getRowId(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.sql.RowId object in Java.
short getShort(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a short in java.
SQLXML getSQLXML(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.sql.SQLXML object in Java.
String getString(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a String in Java.
Time getTime(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.sql.Time object java.
Time getTime(int columnLabel, Calendar cal)
Returns the value of the column in the current row of this ResultSet object as a java.sql.Time object java(uses the given calendar to construct an appropriate millisecond value for the time).
TimeStamp getTimestamp(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.sql.Timestamp object in Java.
TimeStamp getTimestamp(int columnLabel, Calendar cal)
Returns the value of the column in the current row of this ResultSet object as a java.sql.Timestamp object in Java(uses the given calendar to construct an appropriate millisecond value for the time)..
URL getURL(int columnLabel)
Returns the value of the column in the current row of this ResultSet object as a java.net.URL object in Java.

/* Step 1: Import sql package */
import java.sql.*;

public class SampleApp {
    
    /* Update username, password and driver details here */
    static Connection getConnection() throws ClassNotFoundException, SQLException{
         /* Step 2: Load Driver */
        System.out.println("Loading/Registering driver");  
       
        Class.forName("com.mysql.jdbc.Driver");
         
        /* Step 3: Open connection to database */
        System.out.println("Connecting to database");
        String url = "jdbc:mysql://localhost/world";
        String userName = "root";
        String pasword = "tiger";
        return DriverManager.getConnection(url, userName, pasword);
    }
    
    public static void main(String args[]) throws SQLException, ClassNotFoundException{
        
        Connection conn = getConnection();

          /* Create table employee */
        String query = "CREATE TABLE employee (id int, name varchar(30), PRIMARY KEY(id))";
        Statement stmt = conn.createStatement();
        stmt.execute(query);     
        
        /* Insert data to employee table */
        query = "INSERT INTO employee values(1, \"Krishna\")";
        stmt.execute(query);
        query = "INSERT INTO employee values(2, \"Arjun\")";
        stmt.execute(query);        
        query = "INSERT INTO employee values(3, \"Ptr\")";
        stmt.execute(query);

        query = "SELECT * FROM employee";
        ResultSet rs = stmt.executeQuery(query);
        
        while(rs.next()){
            int id  = rs.getInt("ID");
            String name = rs.getString("NaME");
            System.out.println(id +" " + name);
        }
        
        System.out.println("Dropping table employee");
        query = "DROP TABLE employee";
        stmt.execute(query);
        
 rs.close();
        stmt.close();
        conn.close();
    }
}

Output
Loading/Registering driver
Connecting to database
1 Krishna
2 Arjun
3 Ptr
Dropping table employee


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment