码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • JDBC连接


        public static void main(String[] args) {
    //        JDBC
            Connection conn=null;
            Statement sta=null;
            try {
    //   1.加载驱动
    //       jdbc5  com.mysql.jdbc.Driver
    //         jdbc8  com.mysql.cj.jdbc.Driver
            String driver_m="com.mysql.cj.jdbc.Driver";
                Class.forName(driver_m);
    
    //  2.获取数据库连接
    //            "jdbc:mysql://localhost/test01?" localhost:本地连接  test01:数据名
                String url_mysql = "jdbc:mysql://localhost/hzh0904?" + "useUnicode=ture&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8";
    //            用户名
                String user_name="root";
    //            密码
                String user_pass="root";
    //            获取连接
                conn = DriverManager.getConnection(url_mysql,user_name,user_pass);
    //   3.设置事务(非必要)
            conn.setAutoCommit(false);//手动提交
    //   4.创建语句对象
                sta=conn.createStatement();
    
    //   5.执行sql语句
    // 增删改 用executeUpdate 接收用 int(影响了几条数据)
                String sql ="insert into student values (21,'一一',12,'女',90,2)";
                String sql3 ="delete from  student where id='21'";
                int count = sta.executeUpdate(sql3);
                System.out.println("插入了"+count+"条数据");
    
    // 查询用 executeQuery 接收用 ResultSet 
    String sql="select * from student";
    ResultSet re= sta.executeQuery(sql);

    // 第二种

    //         4.创建预编译语句对象
                String sql= "insert into student values(?,?,?,?,?,?)";
    
                pre=conn.prepareStatement(sql);
    //            5.执行sql
    //            pre.setString(字段的位置(非下标),字段内容);
    
    
                pre.setString(1,"23");
    ResultSet re= pre.executeQuery();

    //1.  Statement 先创建对象在写sql 在 execute中存储sql,并运行

    // 2. PreparedStatement 先写sql 在创建对象里运行sql,用execute运行

    //   6.提交事务
                conn.commit();
    
    
    
    
    
    
    
    
            } catch (ClassNotFoundException | SQLException e) {
    
                e.printStackTrace();
            }finally {
    //            7.关闭连接
    
                try {
                    sta.close();
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    // 判断表是否存在

    DatabaseMetaData meta = m_sqlCon.getMetaData();

    ResultSet rsTables = meta.getTables(null , null, “YourTableName”, null);

    if(rsTables.next()){ System.out.println("The Table exsits."); }

    else{ System.out.println("The Table not exsits."); }

  • 相关阅读:
    第二十三章 反射和注解
    Python学习笔记--初识 Python 正则表达式
    【带头学C++】----- 七、链表 ---- 7.5 学生管理系统(链表--下)
    论文解读:SlowFast Networks for Video Recognition
    审计和风控做什么——企业审计和风控工作的相同和不同
    教程详解从照片到倾斜摄影模型、影像地形点云的成果输出,网页端Web发布展示
    Pytorch入门(4)—— Tensor和Module的保存与加载
    Fast way to filter a file line by line and save as new format in a new file
    如何在CentOS7上搭建自己的GitLab仓库
    ApplicationContext种类
  • 原文地址:https://blog.csdn.net/lcatake/article/details/132753800
  • 最新文章
  • 【FHE】我们如何实现同态加密推理(八):SiLU 的密文化——两条路径,和一个 8 字节的开关
    SQLcl 中 c/old/new 替换变量为何失效?先定位再替换
    不让模型写作文,直接从它脑子里读答案:Jev 决策在 .NET 的两条路线
    Halo插件:支持GitHub的图床附件策略
    电视投屏界的“一股清流”!SimpleTVCast 6.0.0-beta 抢先体验:无需TV端App,界面大换血!✨
    华三VXLAN集中式网关实战指南:Spine多活网关 + 双活接入(M-LAG)
    软件更好写之后口碑会更重要
    大 O、大 Omega、大 Theta 的区别,O(n) 复杂度是什么意思
    Java标签跳转:一个鲜为人知但却很实用的小技巧
    多智能体系统论文速读
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号