昨天是在银河学习的第一天,第一个任务是写一个"登陆".呵呵轻车熟路 我很熟练的写好了. 今天起来后想将他写完善,于是就加入了IDUS.当加入update方法后就就进行了测试.代码如下:
public class Comm {
Connection con;
Statement st;
public Comm() {
con = new Conn().getCon();
try {
st =con.createStatement();
} catch (SQLException e) {
System.out.println("创建Statement对象失败");
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
Comm m = new Comm();
m.UpdateClum("兆强",2);
ArrayList<String> my = m.getClum(2);
for (String string : my) {
System.out.println(string);
}
m.close();
}
// 选择某一列的数据 select语句
public ArrayList<String> getClum(int i) {
String sql = "select * from managers";
ArrayList<String> al = new ArrayList<String>();
ResultSet rs = null;
try {
st = con.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
al.add(rs.getString(i));
}
} catch (SQLException e) {
System.out.println("select 语句不能执行");
e.printStackTrace();
}finally{
try{
rs.close();
st.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return al;
}
// 更新一条数据 update语句
public void UpdateClum(String newname,int i) {
String sql = "update managers set name='" + newname + "' where id="+i;
ResultSet rs = null;
try {
st = con.createStatement();
st.executeUpdate(sql);
} catch (SQLException e) {
System.out.println(sql);
System.out.println("update语句不能执行");
e.printStackTrace();
} finally {
try {
rs.close();
st.close();
con.close();
} catch (SQLException e) {
System.out.println("无法更新");
e.printStackTrace();
}
}
}
结果报错,不能在各自的操作完成之后再次连接,解决办法:公共代码提出来,最后一起关闭数据库.不过这样不利于模块的完整性,所以更好的办法还有待出现.