不要使用mysql的关键字,如key. 2014.4.13
1.获取最大值 String device_sql= "select max(id) id from user";
2.创建一条数据
String create_sql="insert into unit (user_id,device_id,index_in_device) values ("+device_user_id+","+device_id+","+unit_id+")";
db1.statement.execute(create_sql);
=== insert into unit (user_id,device_id,index_in_device) values (1,1,11)
不要使用 rs=db1.statement.execute(create_sql); 否则出错.
不能使用 db1.statement.executeQuery(sql);
2.更新数据
String sql= "update unit set "+field_id+" ='"+value+"' where device_id="+device_id+" and index_in_device="+unit_id+"";
int rs;
rs = db1.statement.executeUpdate(sql);
3.插入数据
String create_sql="insert into device (user_id,name,app_key,index_id) values ("+user_id+",'"+device_name+"','"+device_key+"',"+(device_max_index+1)+")";
db1.statement.execute(create_sql);
4.获取刚插入数据的自动序列值
String create_sql="insert into device (user_id,name,app_key,index_id) values ("+user_id+",'"+device_name+"','"+device_key+"',"+(device_max_index+1)+")";
db1.statement.execute(create_sql);
rs=db1.statement.executeQuery("select last_insert_id()");
if(rs.next())
{
newDeviceId=rs.getInt(1);
}