combobox key value pair using java

          key value pair combobox example

step 1: Create a class with two variables , key and value

public class customcombo {
String key;
int value;
public customcombo(String key, int value) {
this.key= key;
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key= key;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString(){
return key;
}
}


Step2: set combobox model(static or dynamic)


case 1: static data ,do the following way

Object[] items=new Object[]{new customcombo("INDIA",1),new customcombo("UAE",2),new customcombo("USA",3)};
javax.swing.JComboBox jComboBox1 = new javax.swing.JComboBox();
DefaultComboBoxModel mod=new DefaultComboBoxModel(items);
jComboBox1.setModel(mod);

case 2: dynamic(database) value , do the following way

Object[] items = null;
ResultSet rs = st.executeQuery("select id,country from tb");
if (rs.next()) {
rs.last();
items = new customcombo[rs.getRow()];
rs.beforeFirst();
}
int i = 0;
while (rs.next()) {
items[i++] = new customcombo(Integer.parseInt(rs.getString(1)), rs.getString(2));
}


step 3: When program run ,combobox display name(key). To get value of the display name, use the following code


customcombo ob=(customcombo) jComboBox1.getSelectedItem();
Now you get both value
int val=ob.value;
String country=ob.key;

show table structure in oracle

To get all details of oracle table use the following query.


SELECT to_char(DBMS_METADATA.GET_DDL('TABLE','tablename')) FROM dual

call stored procedure using java

Here it shows how to call a stored procedure from java program.


CallableStatement cs;
try {

//CASE 1 ----- Call a stored procedure with no parameters

cs = connection.prepareCall("{call myproc}");
cs.execute();
// Call a procedure with one IN parameter
cs = connection.prepareCall("{call myprocin(?)}");
// Set the value for the IN parameter
cs.setString(1, "test");
// Execute the stored procedure
cs.execute();

//CASE 2------- Call a stored procedure with one OUT parameter

cs = connection.prepareCall("{call myprocout(?)}");
// Register the type of the OUT parameter
cs.registerOutParameter(1, Types.VARCHAR);
// Execute the stored procedure and retrieve the OUT value
cs.execute();
String outParam = cs.getString(1); // OUT parameter

//CASE 3 ----- Call a stored procedure with one IN/OUT parameter

cs = connection.prepareCall("{call myprocinout(?)}");
// Register the type of the IN/OUT parameter
cs.registerOutParameter(1, Types.VARCHAR);
// Set the value for the IN/OUT parameter
cs.setString(1, "test");
// Execute the stored procedure and retrieve the IN/OUT value
cs.execute();
outParam = cs.getString(1); // OUT parameter


} catch (SQLException e) {
}

calling sql functions using java

Here it shows how to call a sql function from java program.

CallableStatement cs;
try {

// CASE 1  --- Call a function with no parameters; the function returns a VARCHAR

cs = connection.prepareCall("{? = call myfunc}");
// Register the type of the return value
cs.registerOutParameter(1, 15);
// Execute and retrieve the returned value
cs.execute();
String retValue = cs.getString(1);

//CASE 2  --- Call a function with one IN parameter; the function returns a VARCHAR


cs = connection.prepareCall("{? = call myfuncin(?)}");
// Register the type of the return value
cs.registerOutParameter(1, Types.VARCHAR);
// Set the value for the IN parameter
cs.setString(2, "test");
// Execute and retrieve the returned value
cs.execute();
String retValue = cs.getString(1);

//CASE 3 ---- Call a function with one OUT parameter; the function returns a VARCHAR

cs = connection.prepareCall("{? = call myfuncout(?)}");
// Register the types of the return value and OUT parameter
cs.registerOutParameter(1, Types.VARCHAR);
cs.registerOutParameter(2, Types.VARCHAR);
// Execute and retrieve the returned values
cs.execute();
String retValue = cs.getString(1); // return value
String outParam = cs.getString(2); // OUT parameter

//CASE 4 ---- Call a function with one IN/OUT parameter; the function returns a VARCHAR

cs = connection.prepareCall("{? = call myfuncinout(?)}");
// Register the types of the return value and OUT parameter
cs.registerOutParameter(1, Types.VARCHAR);
cs.registerOutParameter(2, Types.VARCHAR);
// Set the value for the IN/OUT parameter
cs.setString(2, "test");
// Execute and retrieve the returned values
cs.execute();
String retValue = cs.getString(1); // return value
String outParam = cs.getString(2); // IN/OUT parameter

} catch (SQLException e) {
System.out.println(e);
}

Getting a Request Parameter in a JSP Page

In a GET request, the request parameters are taken from the query string . For example, the URL http://javamagics.blogspot.com?param1=v1&param2=v2 contains two request parameters – param1and param2. In a POST request, the request parameters are taken from both query string and the posted data which is encoded in the body of the request.
<%= request.getParameter("name") %>
 Eg:
<%
    if (request.getParameter("name") == null) {
        out.println("Please enter your name.");
    } else {
        out.println("Hi, "+request.getParameter(i)+"...");
    }
%>
If the page was accessed with the URL: http://javamagics.blogspot.com?name=Jerry
the resulting output would be: 
Hi,Jerry... 
If name is not specified on the query string, the output would be: 
Hi,null...

 





Getting the Client's Address in a Servlet


public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    // Get client's IP address
    String addr = req.getRemoteAddr(); // 110.103.113.113

    // Get client's hostname
    String host = req.getRemoteHost(); //java.com
}