儘管這個解決方案可能不是最佳,據一些比較厲害的人說會存在sql注入等等問題,但是作爲一個內部小工具,有些思想可供參考
我題目的意思表達尚不清楚,確切來說,我前幾天想的是實現這樣一個功能。譬如,jtable中現在存在三條數據,0 麪包 1牛奶 2方便麪。
現在我利用jtable中的getselectedrow方法,將其中的牛奶刪除,然後jtable表格實現重繪,出現的數據爲 0 麪包 1 方便麪
這裏沒有用到什麼hibernate等框架,用的是純jdbc。
所建數據庫表格名稱爲rr,wor及num爲其屬性
{
GetConn getconn=new GetConn();//獲取連接,爲了封裝,所以連接函數是獨立開的
Connection conn=getconn.getConn();
int i=0;//i一定是在這裏定義的
try{
String sql="select * from rr where wor='"+text+"'";
Statement stat=conn.createStatement();
ResultSet rs=stat.executeQuery(sql);
while(rs.next())
{
i=rs.getInt("num");//查詢後的結果給i賦值
System.out.print(i);//爲了檢查錯誤,我習慣性的插入一些輸出語句檢查中間運行結果是否出錯
}
conn.close();//關閉連接
}catch(Exception e)
{
e.printStackTrace();
}
return i;
}
public void updated(int index)//將要刪除的序號傳遞作爲該函數的參數
{
GetConn getconn=new GetConn();//獲取連接
Connection conn=getconn.getConn();
try{
String sql="update rr set num=num-1 where num>'"+index+"'";//實現序號更改
Statement stat=conn.createStatement();
stat.executeUpdate(sql);
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}