1 Star 0 Fork 0

曾铖坚 / day24-code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
JDBCTest9.java 1.80 KB
一键复制 编辑 原始数据 按行查看 历史
package com.bjpowernode.jdbc;
import java.sql.*;
public class JDBCTest9 {
public static void main(String[] args) {
//用PreparedStatement实现增删改
Connection conn = null;
PreparedStatement ps = null;
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "246810");
//3.获取预编译数据库操作对象
//增
/*String sql = "insert into dept(deptno,dname,loc) values (?,?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1,60);
ps.setString(2,"销售部");
ps.setString(3,"上海");*/
//改
/*String sql = "update dept set dname = ? , loc = ? where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, "销售1部");
ps.setString(2, "上海2");
ps.setInt(3, 60);*/
//删
String sql = "delete from dept where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, 60);
//4.执行sql
int count = ps.executeUpdate();
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
} finally {
//6.释放资源
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
1
https://gitee.com/zeng-chengjian/day24-code.git
git@gitee.com:zeng-chengjian/day24-code.git
zeng-chengjian
day24-code
day24-code
master

搜索帮助