java数据库删除 *** 作

java数据库删除 *** 作,第1张

简单实现代码如下:

EmployeeDao.java

//删除数据

public boolean deleteEmployeeById(int id){

boolean result = false

try{

con = DBCon.getConn()

String sql = "delete from tb_employee where id=?"

pstmt = (PreparedStatement) con.prepareStatement(sql)

pstmt.setInt(1, id)

int i = pstmt.executeUpdate()

if(i == 1)

result = true

}catch(Exception e){

e.printStackTrace()

}finally{

try{

if(pstmt != null){

pstmt.close()

}

}catch(Exception e){

e.printStackTrace()

}

try{

if(con != null){

con.close()

}

}catch(Exception e){

e.printStackTrace()

}

}

return result

}

TestSql2.java

package com.sql.test

import com.sql.dao.EmployeeDao

public class TestSql02 {

public static void main(String[] args){

boolean result = EmployeeDao.getInstance().deleteEmployeeById(1)

if(result == true){

System.out.println("删除成功!")

}else{

System.out.println("删除失败!")

}

}

}

1.增加\x0d\x0aString s1="insert into tableNames (id,name,password) values(myseq.nextval,?,?)"\x0d\x0aClass.forName(driver)\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd)\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s1)\x0d\x0aprepStmt.setString(1,name)\x0d\x0aprepStmt.setString(2,password)\x0d\x0aResultSet rs=stmt.executeUpdate()\x0d\x0a2、删除\x0d\x0aString s2="delete from tbNames where name=?"\x0d\x0aClass.forName(driver)\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd)\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s2)\x0d\x0aprepStmt.setString(1,name)\x0d\x0aResultSet rs=stmt.executeUpdate()\x0d\x0a3、修改\x0d\x0aString s3=“update tbNames set name=? where id=?”\x0d\x0aClass.forName(driver)\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd)\x0d\x0aPreparedStatement prepStmt = conn.prepareStatement(s3)\x0d\x0aprepStmt.setString(1,name)\x0d\x0aprepStmt.setString(2,id)\x0d\x0aResultSet rs=stmt.executeUpdate()\x0d\x0a4、查询\x0d\x0aString s4="select id,name,password from tbNames"\x0d\x0aClass.forName(driver)\x0d\x0aConnection conn = DriverManager.getConnection(url,dbUser,dbPwd)\x0d\x0aStatement stmt=conn.createStatement()\x0d\x0aResultSet rs = stmt.executeQuery(s4)\x0d\x0awhile(rs.next){\x0d\x0aint id=rs.getInt(1)\x0d\x0aString name = rs.getString(2)\x0d\x0aString pwd=rs.getString(3)\x0d\x0aSystem.out.println(id+name+pwd)} \x0d\x0a\x0d\x0a以上四步必须都得关闭连接;!!!\x0d\x0ars.close()\x0d\x0astmt.close()\x0d\x0aconn.close()


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存