zoukankan      html  css  js  c++  java
  • JDBC增加、更新、删除数据

    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();
            }
        }
    }
  • 相关阅读:
    Linux网络设置
    用户权限 文件或目录权限
    开始写博客了
    php开发中如何判断 是否微信访问
    Linux——安装docker以及docker常用命令
    Java——下划线转驼峰
    前端——JS实现多条件过滤数组
    Linux——通过docker搭建禅道
    免安装版MySQL(windows解压版)安装详细教程以及过程中的问题解决
    数据库——SQL通过某字段的取值范围进行分组汇总
  • 原文地址:https://www.cnblogs.com/xtuxiongda/p/9000444.html
Copyright © 2011-2022 走看看