基於前面練習所出現的問題對其進行修改,可以成功的對資料庫資訊進行增刪改查四項基本操作。
以下是部分修改的代碼,整體的代碼詳見前天的練習2。初始介面就不說了。
首先來到初始介面,點擊瀏覽,會出現資料庫的資訊。
前面所出現的問題是,點擊瀏覽之後,不斷的重複出現資訊添加在表單。 之後在看代碼明白錯誤之後,將其解決。
if (e.getSource()==view){ResultSet rs=null;String v;rs=new UserManage().view();try {while (rs.next()){table.setValueAt(rs.getString("user_name"), ii, 0);table.setValueAt(rs.getString("user_type"), ii, 1);table.setValueAt(rs.getString("startdate"), ii, 2);if (rs.getBoolean("valid")==true)v="啟用";elsev="停用";table.setValueAt(v, ii, 3);ii++;}ii=0;//沒修改之前,點擊瀏覽,不斷出現資料資訊,結果在這裡加入ii=0後,解決了。} catch (SQLException e2) {e2.printStackTrace();}}在表單添加一次全部資訊之後,應該對行數進行重設清零,這樣就可以確保不斷點擊瀏覽只看到一組資料。 然後,註冊,就是增加。 要增加的資訊在上面輸入,然後點擊註冊,顯示註冊成功。表單下面增加一行新資料
之前的代碼是就根本沒有認真工作,是敷衍學者的代碼,在我頓悟後才明白,可能不是敷衍,是幾個變數我沒弄明白究竟是幹什麼,所以就按自己想的代碼解決了
if (e.getSource()==regis){if (new UserManage().addUsers(u_name, u_password, u_type, date, flag)){ResultSet rs=null;String v;rs=new UserManage().view();try {while (rs.next()){table.setValueAt(rs.getString("user_name"), ii, 0);table.setValueAt(rs.getString("user_type"), ii, 1);table.setValueAt(rs.getString("startdate"), ii, 2);if (rs.getBoolean("valid")==true)v="啟用";elsev="停止";table.setValueAt(v, ii, 3);ii++;}ii=0;} catch (SQLException e5){e5.printStackTrace();}}}添加成功之後,用ResultSet,重新添加表單即可。 後面的修改其實也是一個意思的,修改成功之後,重新添加表單。 修改的時候,點擊修改的一行,把修改的使用者名稱寫入,然後輸入新的資訊,點擊修改,出現修改成功,新的資訊會出現。
if (new UserManage().isExist(name)){這裡之前寫的是(。參數),結果後來看到一想,他喵的應該是名字存在了,是真的,就執行啊。都沒有這個人的話,何來修改呢。
if (e.getSource()==edit){//修改成功之後,表單顯示問題if (u_name.equals("")){u_name=(String)table.getValueAt(table.getSelectedRow(), 0);}if (new UserManage().updateUser(u_name, u_password, u_type, date, flag)){ResultSet rs=null;//解決。之前修改後哦,是沒把資訊重新添加表單String v;rs=new UserManage().view();try {while (rs.next()){table.setValueAt(rs.getString("user_name"), ii, 0);table.setValueAt(rs.getString("user_type"), ii, 1);table.setValueAt(rs.getString("startdate"), ii, 2);if (rs.getBoolean("valid")==true)v="啟用";elsev="停用";table.setValueAt(v, ii, 3);ii++;}ii=0;} catch (SQLException e2) {e2.printStackTrace();}}}最後的刪除,點擊要刪除的行,點擊刪除按鈕,刪除成功。
之前問題就是刪除後,表單不更新或者更新有問題
if (e.getSource()==delete){//目前刪除後表單不跟新int k=0;int r=table.getSelectedRow();String ss=(String)table.getValueAt(r, 0);if (new UserManage().deleteUser(ss)){ResultSet rs=null;String v;rs=new UserManage().view();try {while (rs.next()){table.setValueAt(rs.getString("user_name"), k, 0);table.setValueAt(rs.getString("user_type"), k, 1);table.setValueAt(rs.getString("startdate"), k, 2);if (rs.getBoolean("valid")==true)v="啟用";elsev="停用";table.setValueAt(v, k, 3);k++;}table.setValueAt("",k,0);//問題從這裡解決了,不過準確的來說是把上面whiletable.setValueAt("",k,1);//迴圈裡的k確定明白咯,然後在這四行進行刪除後的表單table.setValueAt("",k,2);//跟新,但是這裡的更新不能說是更新,因為上面while裡的內容table.setValueAt("",k,3);//就相當於更新,這裡就把最後一個多餘出來的表單設成空的即可k=0;} catch (SQLException e2) {e2.printStackTrace();}}}刪除之後,重新添加表單,最後一行置為空白即可。 以上一通操作之後,資料庫中的資訊,和表單顯示資訊一致。
在添加那裡,出現了一個插曲
String sql="insert into userInfo(user_name,user_pass,user_type,startdate,valid)values ('"+name+"','"+word+"','"+type+"','"+date+"',"+f+")";開始這裡出現了類似這個錯誤 MySQL遇到check the manual that corresponds to your MySQL server version for the right syntax錯誤
找了一會,有的是關鍵字命名問題,而我的這裡是valid後面多了一個 , 就是它害的,添加語句會有問題。所以,掌握sql語句,不要隨便換行,認真仔細呢。