tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Koen Dejonghe" <koen_dejon...@hotmail.com>
Subject parameter value to JDBC type conversion
Date Tue, 30 May 2000 23:00:01 GMT
Hi,

My servlet opens a window on the contents of a database table. It allows the 
user to make modifications to a record through an html form. The contents of 
this form is passed as parameters to the servlet in a post transaction. I 
retrieve the value of the parameters with 
'getParameterValues(fieldName)[0]'. Now I need to store the parameter value, 
which is a string, in the corresponding field of the table, which can be 
anything defined in java.sql.Types.
Question: How do I make the conversion ?

I have a solution attached, but I'm wondering if there is something better 
out there. I don't have a solution yet for the lines that are commented out.

Cheers.
Koen Dejonghe.

----------------------------------------------------------
String sColumn = rsmd.getColumnName(i);
String sValue = req.getParameterValues(sColumn)[0];
Object o = Str2Obj(sValue, rsmd.getColumnType(i));
pstmt.setObject(i, o); //pstmt is a prepared statement

private Object Str2Obj (String s, int iType) {
  switch (iType) {
  //case java.sql.Types.ARRAY:
  case java.sql.Types.BIGINT:
    return new Long(s);
  //case java.sql.Types.BINARY:
  case java.sql.Types.BIT:
    return new Boolean(s);
  //case java.sql.Types.BLOB:
  //case java.sql.Types.CHAR:
  //case java.sql.Types.CLOB:
  case java.sql.Types.DATE:
    return new java.sql.Date(0).valueOf(s);
  case java.sql.Types.DECIMAL:
    return new BigDecimal(s);
  //case java.sql.Types.DISTINCT:
  case java.sql.Types.DOUBLE:
    return new Double(s);
  case java.sql.Types.FLOAT:
    return new Float(s);
  case java.sql.Types.INTEGER:
    return new Integer(s);
  //case java.sql.Types.JAVA_OBJECT:
  //case java.sql.Types.LONGVARBINARY:
  case java.sql.Types.LONGVARCHAR:
  case java.sql.Types.VARCHAR:
    return s;
  //case java.sql.Types.NULL:
  case java.sql.Types.NUMERIC:
    return new BigDecimal(s);
  //case java.sql.Types.OTHER:
  //case java.sql.Types.REAL:
  //case java.sql.Types.REF:
  case java.sql.Types.SMALLINT:
    return new Short(s);
  //case java.sql.Types.STRUCT:
  case java.sql.Types.TIME:
    return new Time(0).valueOf(s);
  case java.sql.Types.TIMESTAMP:
    return new Timestamp(0).valueOf(s);
  case java.sql.Types.TINYINT:
    return new Byte(s);
  //case java.sql.Types.VARBINARY:
  }

  return null;

}


________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com


Mime
View raw message