当前位置:编程学习 > JAVA >>

小菜鸟求教JAVA向MYSQL插入数据

public boolean alter(AttributeInfo abi){
String sql="update sinfo set name=?, price =?,pic =?,note =?  where id=?";
try {
con=(Connection) getConnection();
ps=(PreparedStatement) con.prepareStatement(sql);
ps.setInt(1, abi.getId());
ps.setString(2, abi.getName());
ps.setDouble(3, abi.getPrice());
ps.setString(4, abi.getPic());
ps.setString(5, abi.getNote());
return ps.executeUpdate()>0?true:false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally{
close(con, ps, rs);
}

}
\\测试代码
public static void main(String[] args) {
ProdInfoDao p=new ProdInfoDao();
AttributeInfo a=new AttributeInfo("C+++++开发", 45, "10.jpg", "hghqfkdfdlfj,4");
p.alter(a);
}

执行完后什么都不报,但是数据是没有更新过来, --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 放到最后边也是一样,也更新不过来,也不报错。。。 --------------------编程问答-------------------- import导错包了 --------------------编程问答-------------------- ps.setInt(1, abi.getId());
ps.setString(2, abi.getName());
ps.setDouble(3, abi.getPrice());
ps.setString(4, abi.getPic());
ps.setString(5, abi.getNote());

这里的类型对吗??? --------------------编程问答--------------------
引用 4 楼 ydb7459022 的回复:
ps.setInt(1, abi.getId());
ps.setString(2, abi.getName());
ps.setDouble(3, abi.getPrice());
ps.setString(4, abi.getPic());
ps.setString(5, abi.getNote());

这里的类型对吗???

貌似这顺序有问题,给ps 设值是你得按SQL语句的顺序来设 --------------------编程问答-------------------- con=(Connection) getConnection();
你的这个方法:getConnection();是怎么写的。还有强制类型转换一下,按理讲,应该是不要的吧。 --------------------编程问答-------------------- 楼主的
AttributeInfo a=new AttributeInfo("C+++++开发", 45, "10.jpg", "hghqfkdfdlfj,4");
最后一个属性写一块了。 --------------------编程问答-------------------- 还收就是楼主的顺序写反了。
这里的顺序应该对应上问号的顺序。
ps.setInt(1, abi.getId());
ps.setString(2, abi.getName());
ps.setDouble(3, abi.getPrice());
ps.setString(4, abi.getPic());
ps.setString(5, abi.getNote());
正确的如下:
ps.setString(1, abi.getName());
ps.setDouble(2, abi.getPrice());
ps.setString(3, abi.getPic());
ps.setString(4, abi.getNote());
ps.setInt(5, abi.getId());
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,