为什么此修改表数据的方法不能够修改呢?请高手赐教
为什么此修改表数据的方法不能够修改呢?请高手赐教
代码如下:
/** * 修改员工信息 * @param name * @param age * @param sex * @param position * @param salary * @param birthday * @throws Exception */ public void modifyInfo(String name,int age,String sex,String position,float salary, java.sql.Date birthday)throws Exception{ Connection con=null; PreparedStatement prepStmt=null; //ResultSet rs=null; employees=new ArrayList(); try{ con=getConnection(); String sql="update employees set name=?,age=?,sex=?,position=?,salary=?,birthday=? where name=?"; prepStmt=con.prepareStatement(sql); prepStmt.setString(1, name); prepStmt.setInt(2, age); prepStmt.setString(3, sex); prepStmt.setString(4, position); prepStmt.setFloat(5, salary); prepStmt.setDate(6, birthday); prepStmt.setString(7, name); prepStmt.executeUpdate(); prepStmt.close(); }catch(Exception e){ e.printStackTrace(); } finally{ //closeResultSet(rs); closeConnection(con); } }
这么多东西,不烦吗?
好维护吗?如果将来要加一个属性怎么办?改代码吗?郁闷。。
你最好把这些东西全部封装成一个JAVABEAN,通过JAVABEAN的get和set方法来对值进行操作。
至于你说的数据全部变成“??”,是你的数据库字符编码的问题,你可以在连接数据库的时候加上字符编码解决这个问题。
像这样:
String url="jdbc:mysql://localhost/"+DB_NAME+" user="+LOGIN_NAME+"&password="+LOGIN_PASSWORD+"&characterEncoding=GBK";
characterEncoding=GBK 指定数据库字符编码为GBK。
你试试看!