关于在java中删除数据库的一条记录

关于在java中删除数据库的一条记录,第1张

你好,我用的是sqlserver2005数据库代码如下:import javasql;

public class Demo {

public static void main(String agrs[]) {

Connection con = null;

PreparedStatement pstmt = null;

String sql = "delete from user where username=";

try {

ClassforName("commicrosoftsqlserverjdbcSQLServerDriver"); //设置数据库连接的驱动

con = DriverManagergetConnection("jdbc:sqlserver://localhost:1433;databaseName=数据库"); //设置数据库连接的 URL,用户名,密码

pstmt = conprepareStatement(sql);

pstmtsetString(1, "aaa"); // 设置SQL语句中username的值

int count = pstmtexecuteUpdate();

if (count > 0) {

Systemoutprintln(" *** 作成功");

} else {

Systemoutprintln(" *** 作失败");

}

} catch (Exception e) {

eprintStackTrace();

}

}

}

其实,也就那几步而已:

别忘了,加载jar文件

加载驱动

获得Connection

获得PreparedStatement /PreparedStatement

执行sql语句

处理结果集ResultSet

关闭连接

mport javasqlConnection;

import javasqlPreparedStatement;

import javasqlResultSet;

import javautilArrayList;

import javautilList;

import EntityJIAJU;

public class JiaJu {

 public JIAJU selectExe(int shouhinId) {

  JIAJU jia = new JIAJU();

  try {

   Connection con = ConnectionManagergetConnection();

   String sql = "select  from jiaju where shouhinId=";

   PreparedStatement ps = conprepareStatement(sql);

   pssetInt(1, shouhinId);

   ResultSet rs = psexecuteQuery();

   if (rs != null) {

    while (rsnext()) {

     jiasetShouhinId(rsgetInt("shouhinId"));

     jiasetShouhinName(rsgetString("shouhinName"));

     jiasetShouhinColor(rsgetString("shouhinColor"));

     jiasetShouhinPrice(rsgetInt("shouhinPrice"));

     jiasetShouhinPai(rsgetString("shouhinPai"));

     jiasetShouhinShi(rsgetString("shouhinShi"));

     // listadd(jia);

    }

   }

  } catch (Exception e) {

   eprintStackTrace();

  }

  return jia;

 }

 public void insertJia(JIAJU jia) {

  try {

   Connection con = ConnectionManagergetConnection();

   String sql = "insert into jiaju values(,,,,)";

   PreparedStatement ps = conprepareStatement(sql);

   pssetString(1, jiagetShouhinName());

   pssetString(2, jiagetShouhinColor());

   pssetInt(3, jiagetShouhinPrice());

   pssetString(4, jiagetShouhinPai());

   pssetString(5, jiagetShouhinShi());

   psexecuteUpdate();

  } catch (Exception e) {

   eprintStackTrace();

  }

 }

 public List selectJia() {

  List list = new ArrayList();

  

  try {

   Connection con = ConnectionManagergetConnection();

   String sql = "select  from jiaju ";

   PreparedStatement ps = conprepareStatement(sql);

   ResultSet rs = psexecuteQuery();

   if (rs != null) {

    while (rsnext()) {

     JIAJU jia = new JIAJU();

     jiasetShouhinId(rsgetInt("shouhinId"));

     jiasetShouhinName(rsgetString("shouhinName"));

     jiasetShouhinColor(rsgetString("shouhinColor"));

     jiasetShouhinPrice(rsgetInt("shouhinPrice"));

     jiasetShouhinPai(rsgetString("shouhinPai"));

     jiasetShouhinShi(rsgetString("shouhinShi"));

        listadd(jia);

    }

   }

  } catch (Exception e) {

   eprintStackTrace();

  }

  return list;

 }

 public JIAJU selectbuy(int shouhinId) {

  JIAJU jia = new JIAJU();

  try {

   Connection con = ConnectionManagergetConnection();

   String sql = "select  from jiaju where shouhinId=";

   PreparedStatement ps = conprepareStatement(sql);

   pssetInt(1, shouhinId);

   ResultSet rs = psexecuteQuery();

   if (rs != null) {

    while (rsnext()) {

     jiasetShouhinId(rsgetInt("shouhinId"));

     jiasetShouhinName(rsgetString("shouhinName"));

     jiasetShouhinColor(rsgetString("shouhinColor"));

     jiasetShouhinPrice(rsgetInt("shouhinPrice"));

     jiasetShouhinPai(rsgetString("shouhinPai"));

     jiasetShouhinShi(rsgetString("shouhinShi"));

    }

   }

  } catch (Exception e) {

   eprintStackTrace();

  }

  return jia;

 }

  public void updateLou(JIAJU jia){

   try{

    Connection con = ConnectionManagergetConnection();

   String sql = "update jiaju set shouhinPrice= where  shouhinId=";

   PreparedStatement ps = conprepareStatement(sql);  

   pssetInt(1,jiagetShouhinPrice());

   pssetInt(2, jiagetShouhinId());

   psexecuteUpdate();

   }catch(Exception e){

    eprintStackTrace();

   }

  }

  public void deleteLou(JIAJU jia){

   try{

    Connection con = ConnectionManagergetConnection();

   String sql = "delete from jiaju where shouhinId=";

   PreparedStatement ps = conprepareStatement(sql);

   pssetInt(1, jiagetShouhinId());

   psexecuteUpdate();

   }catch(Exception e){

    eprintStackTrace();

   }

  }

}

以上就是关于关于在java中删除数据库的一条记录全部的内容,包括:关于在java中删除数据库的一条记录、java 如何连接数据库,如何完成数据库记录添加,删除,修改以及查询。、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9431412.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存