JDBC增加、更新、删除数据

st.executeUpdate(sql) 进行插入、更新、删除操作
返回的是受影响的记录的条数

注意:输入的sql语句中,vachar类型记住加单引号

完整代码如下:

public class JDBCTest {
//建立连接
public static Connection getConnection(){
Connection conn=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
conn=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=FALSE&serverTimezone=UTC","root","xb199795");
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//插入数据
public static void insert() {
String sql="insert into tbl_user(name,password,email)"+
"values('xiongda','123','xiongda@qq.com')";
Connection conn =getConnection();
try {
Statement st=conn.createStatement();
int count =st.executeUpdate(sql);
System.out.println("插入了"+count+"条记录!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//更新数据
public static void update() {
String sql="update tbl_user set email='xiongda@163.com' where name='xiongda'";
Connection conn =getConnection();
try {
Statement st=conn.createStatement();
int count =st.executeUpdate(sql);
System.out.println("更新了"+count+"条记录!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//删除数据
public static void delete() {
String sql="delete from tbl_user where name='xiongda'";
Connection conn =getConnection();
try {
Statement st=conn.createStatement();
int count =st.executeUpdate(sql);
System.out.println("删除了"+count+"条记录!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

最新文章

  1. Java实现JDBC连接数据库实例
  2. [Android Rro] SDK JAR
  3. 使用Template控制Editor显示方式
  4. Java核心知识点学习----使用Condition控制线程通信
  5. springmvc controller junit 测试
  6. [Ogre]纹理设置
  7. PHP中 对象自动调用的方法:__set()、__get()、__tostring()
  8. shell if判断(曾经被一个字符串相等的判断纠结半小时,最后只是if后少了个空格!) 和 awk引用外部变量判断
  9. 2015.9.11模拟赛 codevs4162 bzoj1774【无双大王】
  10. c# 打开、保存文件对话框 和 文件夹选择对话框
  11. <原>ASP.NET 学习笔记之HTML helper中参数何时会是路由参数,何时又会是query string?
  12. WM_ERASEBKGND官方解释(翻译),以及Delphi里所有的使用情况(就是绘制窗口控件背景色,并阻止进一步传递消息)
  13. Asp.net中Request.Url的各个属性对应的意义介绍
  14. Visual Studio 2010利用libxl读写excel表格数据
  15. Server Tomcat v7.0 Server at localhost failed to start.解决办法
  16. SYM File
  17. go基础之数组和切片
  18. 睡前小dp-poj1276-多重背包+二进制优化
  19. Windows下设置oracle数据库定时备份
  20. 011.Zabbix的拓扑创建

热门文章

  1. Laravel5.5 使用第三方Vendor添加注册验证码
  2. Luogu P1342 请柬 题解
  3. initializer_list
  4. 最新版IntelliJ IDEA2019.1破解教程(2019.04.08更新)
  5. 平衡树 替罪羊树(Scapegoat Tree)
  6. po'j2559 Largest Rectangle in a Histogram 单调栈(递增)
  7. Xamarin Mono For Android 4.6.07004看不到新建android
  8. (转)一张图学会Dockerfile
  9. docker 日志管理
  10. 全网最详细的Git学习系列之安装各个Git图形客户端(Windows、Linux、Mac系统皆适用ing)(图文详解)