怎么将温度1、湿度1、温度2、湿度2、温度3、湿度3....,插入到数据库中。用table或者map或者List都行,在数据库库中的格式为1 温度1 湿度1
? 2 温度2 湿度2
? 3 温度3 湿度3
? ....、....
想用2维列表但不知道具体怎么写代码。
------解决方案--------------------
可以把 [序号,温度,湿度]信息保存到一个bean中,或者保存到String数组中?
for example
- Java code
List<String[]> list = new ArrayList<String[]>();for (int i=0; i<5; i++) { //测试数据 list.add(new String[]{String.valueOf(i+1), String.format("温度%d", i+1), String.format("湿度%d", i+1)});}//显示数据System.out.println("序号 温度 湿度");for (String[] sa : list) { System.out.println(Arrays.toString(sa));}//登录数据for (String sa : list) { String sql = String.format( "insert into usertable(id, temperature, humidity) values('%s', '%s', '%s')", sa[0], sa[1], sa[2]); db.executeUpdate(sql); //so on...}
------解决方案--------------------
你应该是 数据库是 只有两个字段 (温度, 湿度) ;你想把页面的温度1、湿度1, 温度2、湿度2,温度3、湿度3,...等一块放过去,然后一起插入库中?
1、
List list = new ArrayList() ;
list.add("温度1") ;
list.add("湿度1") ;
list.add("温度2") ;
list.add("湿度2") ;
list.add("温度3") ;
list.add("湿度3") ;
if (list.size() % 2 == 0){ //判断是否按照规则 ,湿度、温度 是匹配放入list中的
? int index = list.size() / 2 ;
? for (int i = 0; i < index; i++){
? String s1 = (String)list.get(i *2) ;//温度
? String s2 = (String)list.get(i *2 +1 ) ;//湿度
? //入库
? //...
? }
}
以上代码 ,必须是(温度、湿度)这样子的顺序放入list中?
2、public class Weather{
? private float temperature ;
? private float humidity ;
? public Weather(){
? temperature = 0.0f ;
? humidity = 0.0f ;
? }
? public Weather(float temperature, float humidity ){
? this.temperature = temperature;
? this.humidity = humidity ;
? }
? public float getTemperature(){ return this.temperature; };
? public float getHumidity(){ return this.humidity; } ;
? public static void main(String[] args){
? ?
? List list = new ArrayList() ;
? list.add(new Weather(温度1, 湿度1)) ;
? list.add(new Weather(温度2, 湿度2)) ;
? list.add(new Weather(温度3, 湿度3)) ;
? .
? .
? .
? //遍历
? Weather weather = null ;
? for (int i = 0; i < list.size(); i++){
? weather = (Weather)list.get(i) ;
? //保存入库 weather.getTemperature(), weather.getHumidity() ;
? }
? }
}
??