我创建了一个将数据插入到 sql server 的应用程序。连接到服务器没有问题。但是当用户输入已插入数据库的相同数据时,我想使用一个对话框。但我不知道为什么它不起作用...
我的代码在这里......
public void insert(){
final String fname=name.getText().toString();
final String fcity=city.getText().toString();
ResultSet rs;
try{
Statement statement1=connect.createStatement();
rs=statement1.executeQuery("SELECT * FROM FORM1");
List<Map<String,String>>data1=null;
data1=new ArrayList<Map<String,String>>();
while(rs.next()){
Map<String,String>datanum1=new HashMap<String,String>();
datanum1.put("a", rs.getString("NAME"));
datanum1.put("b", rs.getString("CITY"));
data1.add(datanum1);
}
if(data1.contains(fname)!=false){
AlertDialog.Builder adb=new AlertDialog.Builder(context);
adb.setTitle("My Records");
adb.setMessage(fname+" Already exist... do u want to overwrite???");
adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
try{
Statement statement2=connect.createStatement();
statement2.executeUpdate("UPDATE FORM1 SET NAME='"+fname+"',CITY='"+fcity+"' WHERE NAME='fname'");
}catch(Exception e){
Log.e("ERROR", e.getMessage());
}
}
});
adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
AlertDialog adbc=adb.create();
adbc.show();
}
else{
Statement statement3=connect.createStatement();
statement3.executeUpdate("INSERT INTO FORM1(NAME,CITY) VALUES('"+name.getText().toString()+"','"+city.getText().toString()+"')");
}
}catch(Exception e){
Log.e("ERROR", e.getMessage());
}
}
如果有人请告诉我我犯了什么错误以及使用什么正确的代码....