Java implements a simple example of assigning values to SQL statements

  • 2020-04-01 03:17:55
  • OfStack

The code itself is simple. When splicing SQL? It's hard to count. It's easy to assign.



 public static void setParam(PreparedStatement pt, List list) throws SQLException{
  for(int i = 0;i<list.size();i++){
   if (list.get(i) instanceof String) pt.setString(i+1, list.get(i).toString());
   else if(list.get(i) instanceof Integer) pt.setInt(i+1, Integer.parseInt(list.get(i).toString()));
   else if(list.get(i) instanceof Short)pt.setShort(i+1, Short.parseShort(list.get(i).toString()));
   else pt.setObject(i+1, list.get(i));
  }
 }


Related articles: