Java jdbc怎么删除SQL表中的一项

Java jdbc怎么删除SQL表中的一项,第1张

最简单的,按uuid删除

public boolean delete(String uuid) {

boolean flag = false

Connection conn = null

final String sql = "delete from TBL_CHILD where UUID = ?"

try {

conn = getConn()

PreparedStatement ps = conn.prepareStatement(sql)

int count = 1

ps.setString(count++, uuid)

final int result = ps.executeUpdate()

if (result==1){

flag = true

}else{

flag = false

}

ps.close()

} catch (Exception e) {

e.printStackTrace()

return false

} finally {

try {

conn.close()

} catch (SQLException e) {

e.printStackTrace()

}

}

return flag

}

我刚写了一个只有插入的,望采纳

import java.sql.*

import java.util.*

public class TestPre {

public static void main(String[] args) {

int i=0,deptno=0//i只做while循环使用,deptno是表dept2中的一个属性,类型是int

String dname=null,loc=null//dname和loc也是表dept2的属性,类型是String

Scanner s=new Scanner(System.in)

System.out.println("请输入3个参数")

while(i<3){

try{

deptno=s.nextInt()

i++

dname=s.next()

i++

loc=s.next()

i++

}catch(InputMismatchException e){

System.out.println("输入的类型不符,退出")

System.exit(-1)

}

}

Connection conn=null

PreparedStatement pstmt=null

try {

Class.forName("com.mysql.jdbc.Driver")

conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?"+ "user=root&password=root")

pstmt=conn.prepareStatement("insert into dept2 values(?,?,?)")

pstmt.setInt(1, deptno)

pstmt.setString(2, dname)

pstmt.setString(3, loc)

pstmt.executeUpdate()

System.out.println("插入完成")

} catch (ClassNotFoundException e) {

System.out.println("连接数据库不成功,程序退出")

System.exit(-1)

} catch (SQLException e) {

System.out.println("连接数据库不成功,程序退出")

System.exit(-1)

}

finally{

try{

if(pstmt!=null){

pstmt.close()

pstmt=null

}

if(conn!=null){

conn.close()

conn=null

}

}catch(SQLException e){

e.printStackTrace()

}

}

}

}


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

原文地址:https://54852.com/sjk/6424777.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存