在数据库中执行下面的语句可以正常更新数据库。
update model_info1 set model_name='LG' ,model_OP='中国垃圾' where model_name='dadlk'但是用PreparedStatement不行,若给为statement则可以!这又是什么原因???好着急哦,知道的吼一声!!!代码如下:public class ModelService { public boolean updateModel(String modelOP,String modelNewName,String modelOldName){

String mOP = modelOP;
String mOName = modelOldName;
String mNName = modelNewName; boolean flag = false; // 向第一个表中插入一条记录
//String sql = "update model_info1 set model_name= ?, model_OP=? where model_name=?" ;
String sql = "update model_info1 set model_name='" +mNName+ "', model_OP='" +mOP +"' where model_name='"+mOName+"'" ;
// 声明一个Connection
Connection conn = null;
// 声明一个preparedStatement
//PreparedStatement pst = null;
Statement st = null;
try{
// 建立连接
conn = DBConnection.getConnection(); st = conn.createStatement();

//pst = conn.prepareStatement(sql); //pst.setString(1,mNName);

//pst.setString(2,mOP);

//pst.setString(3,mOName);

//int n = pst.excuteUpdate(sql);           这里得到的n的值为0,错误

int n = st.executeUpdate(sql);             这里得到的n的值为1,正确

System.out.println("更新了"+n+"条记录");

if(n>0){

flag = true;

}

return flag;

} catch (Exception e){
e.printStackTrace();
System.out.println("更新表Model_info1的数据失败!");
} finally{
// 关闭PreparedStatement
//if (pst != null){
// try{
// pst.close();
// } catch (SQLException e){
// TODO Auto-generated catch block
// e.printStackTrace();
// System.out.println("关闭preaparedStatement失败!");
// }
// }
// 关闭Statement
if (st != null){
try{
st.close();
} catch (SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("关闭preaparedStatement失败!");
}
}
// 关闭connection
if (conn != null){
try{
conn.close();
} catch (SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("关闭连接失败!");
}
}
}
return flag;
}


public static void main(String args[]){
ModelService ms = new ModelService();
String mNName="LG";
String mOName="dadlk";
String mOP="中国垃圾";
String[] fName = {"COMPACCOUNT"};
int [] fStart = {20};
int [] fEnd={30};
int [] fLen= {40};
System.out.println(ms.updateModel(mOP,mNName,mOName));

}
}

解决方案 »

  1.   

    //int n = pst.excuteUpdate();<-------这里不加sql   
      

  2.   


    无报错信息,控制太显示的是false
      

  3.   


    这是手误,这里的确没有加sql。
      

  4.   

    我知道你出错的原因:
    pst = conn.prepareStatement(sql);这句话是对的
    int n = pst.excuteUpdate(sql);      这里的参数sql要去掉。中间没有参数的,你的sql吻加载了两次,把这个参数去掉就ok了
      

  5.   


                //pst = conn.prepareStatement(sql);            //pst.setString(1,mNName);
                
                //pst.setString(2,mOP);
                
                //pst.setString(3,mOName);
    sql里并没有设问号...所以下面重复了...除非在sql里把参数设为问号
      

  6.   


    public class ModelService {    public boolean updateModel(String modelOP,String modelNewName,String modelOldName){
            
            String mOP = modelOP;
            String mOName = modelOldName;
            String mNName = modelNewName;        boolean flag = false;        // 向第一个表中插入一条记录
            //String sql = "update model_info1 set model_name= ?, model_OP=? where model_name=?" ;
            String sql = "update model_info1 set model_name='" +mNName+ "', model_OP='" +mOP +"' where model_name='"+mOName+"'" ;
            // 声明一个Connection
            Connection conn = null;
            // 声明一个preparedStatement
            //PreparedStatement pst = null;
            Statement st = null;
            try{
                // 建立连接
                conn = DBConnection.getConnection();            st = conn.createStatement();    
                
                //pst = conn.prepareStatement(sql);            //pst.setString(1,mNName);
                
                //pst.setString(2,mOP);
                
                //pst.setString(3,mOName);
                
                //int n = pst.executeUpdate();           这里得到的n的值为0,错误
                
                int n = st.executeUpdate(sql);             这里得到的n的值为1,正确
                    
                System.out.println("更新了"+n+"条记录");
                
                if(n>0){
                
                    flag = true;
                
                }
                
                return flag;
            
            } catch (Exception e){
                e.printStackTrace();
                System.out.println("更新表Model_info1的数据失败!");
            } finally{
                // 关闭PreparedStatement
                //if (pst != null){
                //    try{
                //        pst.close();
                //    } catch (SQLException e){
                        // TODO Auto-generated catch block
                //        e.printStackTrace();
                //        System.out.println("关闭preaparedStatement失败!");
                //    }
                //    }
                // 关闭Statement
                if (st != null){
                    try{
                        st.close();
                    } catch (SQLException e){
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("关闭preaparedStatement失败!");
                    }
                }
                // 关闭connection
                if (conn != null){
                    try{
                        conn.close();
                    } catch (SQLException e){
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("关闭连接失败!");
                    }
                }
            }
            return flag;
        }
        
        
        public static void main(String args[]){
            ModelService ms = new ModelService();
            String mNName="LG";
            String mOName="dadlk";
            String mOP="中国垃圾";
            String[] fName = {"COMPACCOUNT"};
            int [] fStart = {20};
            int [] fEnd={30};
            int [] fLen= {40};
            System.out.println(ms.updateModel(mOP,mNName,mOName));
                
        }
    }
    还是不对!高手继续!
      

  7.   

    难道楼主是mOP="中国垃圾"??????
      

  8.   

    看看PreparedStatement的引用对吗?
      

  9.   

    楼主你都把model_name='dadlk'的字段变成model_name='LG'了
    楼主你PreparedStatement的mOName传的什么?
      

  10.   

    你都把model_name的值改成'LG'了,你在用PreparedStatement去修改model_name为'dadlk'的数据,当然返回为false啊
      

  11.   

    那为什么在sqlplus中可以执行,并且数据中的数据也进行了更新???很纳闷!
      

  12.   

    我不用PreparedStatement,改为Statement 便可成功,这事什么原因?