程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-如何利用jtable刪除數據庫一條信息

java-如何利用jtable刪除數據庫一條信息

編輯:編程綜合問答
如何利用jtable刪除數據庫一條信息

public class userinfo {
private String USERACCOUNT;
private String USERNAME;
private String NICKNAME;
private String PASSWORD;
private int SEX;
private String PHONE;
private String EMAIL;
private String REGION;
private String MYINFO;
private String HEADPIC;
private int DEPTID;
public userinfo(){}
public userinfo(String USERACCOUNT,String USERNAME,String NICKNAME,String PASSWORD,int SEX,String PHONE,
String EMAIL, String REGION,String MYINFO,String HEADPIC,int DEPTID)
{
this.USERACCOUNT=USERACCOUNT;
this.USERNAME=USERNAME;
this.NICKNAME=NICKNAME;
this.PASSWORD=PASSWORD;
this.SEX=SEX;
this.PHONE=PHONE;
this.EMAIL=EMAIL;
this.REGION=REGION;
this.MYINFO=MYINFO;
this.HEADPIC=HEADPIC;
this.DEPTID=DEPTID;
}
public String getUSERACCOUNT(){
return USERACCOUNT;
}
public void setUSERACCOUNT(String USERACCOUNT){
this.USERACCOUNT=USERACCOUNT;
}
public String getUSERNAME(){
return USERNAME;
}
public void setUSERNAME(String USERNAME){
this.USERNAME=USERNAME;
}
public String getNICKNAME(){
return NICKNAME;
}
public void setNICKNAME(String NICKNAME){
this.NICKNAME=NICKNAME;
}
public String getPASSWORD(){
return PASSWORD;
}
public void setPASSWORD(String PASSWORD){
this.PASSWORD=PASSWORD;
}
public int getSEX(){
return SEX;
}
public void setSEX(int SEX){
this.SEX=SEX;
}
public String getPHONE(){
return PHONE;
}
public void setPHONE(String PHONE){
this.PHONE=PHONE;
}
public String getEMAIL(){
return EMAIL;
}
public void setEMAIL(String EMAIL){
this.EMAIL=EMAIL;
}
public String getREGION(){
return REGION;
}
public void setREGION( String REGION){
this.REGION=REGION;
}
public String getMYINFO(){
return MYINFO;
}
public void setMYINFO(String MYINFO){
this.MYINFO=MYINFO;
}
public String getHEADPIC(){
return HEADPIC;
}
public void setHEADPIC(String HEADPIC){
this.HEADPIC=HEADPIC;
}
public int getDEPTID(){
return DEPTID;
}
public void setDEPTID(int DEPTID){
this.DEPTID=DEPTID;
}
void show(){
System.out.print( USERACCOUNT+" "+USERNAME+" "+NICKNAME+" "+PASSWORD+" "+SEX+" "+PHONE+" "+
EMAIL+" "+REGION+" "+MYINFO+" "+HEADPIC+" "+DEPTID+" "+"\n");
}
}

public  void delete(userinfo u){
Connection con=BaseDAO.getConnection();
try{

String sql ="delete from t_UserInfo where  USERACCOUNT=? and USERNAME=? and NICKNAME=? and PASSWORD=? and SEX=? and PHONE=? and EMAIL=? and REGION=? and MYINFO=? and HEADPIC=? and DEPTID=?   ";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,u.getUSERACCOUNT() );
pstmt.setString(2, u.getUSERNAME());
pstmt.setString(3, u.getNICKNAME());
pstmt.setString(4, u.getPASSWORD());
pstmt.setInt(5, u.getSEX());
pstmt.setString(6, u.getPHONE());
pstmt.setString(7, u.getEMAIL());
pstmt.setString(8, u.getREGION());
pstmt.setString(9, u.getMYINFO());
pstmt.setString(10, u.getHEADPIC());
pstmt.setInt(11, u.getDEPTID());
int num=pstmt.executeUpdate();

  if(num>0){
        System.out.println("刪除成功");
        }else{
        System.out.println("刪除失敗");
        }
  BaseDAO.closeStatement(pstmt);
  BaseDAO.closeConnection(con);
}catch(Exception e){
e.printStackTrace();
}
}
      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

}                         
    在jbutton2中添加什麼樣的代碼才能實現鼠標選中jtable中的一行的時候點擊刪除,直接刪除,而且連同數據庫的一起刪除![圖片說明](http://img.ask.csdn.net/upload/201601/05/1451984879_753791.png)

最佳回答:


if(e.getSource==jbutton2) {
int rownum = jtable.getSelectRow();
//得到選定行,然後執行刪除操作
sql = delete from t_UserInfo where ,u.getUSERACCOUNT() =tablemodel.getValueAt(rownum,0)
………………

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved