在数据库中执行下面的语句可以正常更新数据库。
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));
}
}
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));
}
}
解决方案 »
- 分页处理
- JasperReport如何实现套打呢?有谁知道吗?
- IIS 有时候可以正常解析asp文件,有时候又不行,求救!!
- 请求高手:帮我看一下http://www.beihf.com.cn/怎么自动跳到http://www.beihf.com.cn/cms/的
- 如何在CSDN Blog标题处加上一幅图片?
- 求分页的思路,我想过一个,不过感觉在数据量很大时,效率不高,请高手指点
- java SSH关于service层接口定义
- struts2.3.15.3 版本中<s:datetimepicker>控件为什么不显示
- 请教:JSP最简单的测试环境如何配置!
- 关于js打印预览(ExecWB) 去除页眉页脚的问题
- 关于request.getParameter方法获取值乱码问题
- jfreechart显示问题
无报错信息,控制太显示的是false
这是手误,这里的确没有加sql。
pst = conn.prepareStatement(sql);这句话是对的
int n = pst.excuteUpdate(sql); 这里的参数sql要去掉。中间没有参数的,你的sql吻加载了两次,把这个参数去掉就ok了
//pst = conn.prepareStatement(sql); //pst.setString(1,mNName);
//pst.setString(2,mOP);
//pst.setString(3,mOName);
sql里并没有设问号...所以下面重复了...除非在sql里把参数设为问号
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));
}
}
还是不对!高手继续!
楼主你PreparedStatement的mOName传的什么?