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);
复制代码
//   6.提交事务
            conn.commit();








        } catch (ClassNotFoundException | SQLException e) {

            e.printStackTrace();
        }finally {
//            7.关闭连接

            try {
                sta.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
相关推荐
杉氧1 小时前
原生交互:如何在 Flutter 中嵌入 Android/iOS 原生组件并解决手势冲突
android·flutter·dart
Android打工仔1 小时前
Continuation 的链式关系 —— 一个 suspend 函数如何把结果交还给调用者?
android·kotlin
hunterandroid1 小时前
[Android 从零到一] Kotlin Channel 与复杂并发场景:从生产者-消费者到结构化并发治理
android
FlightYe2 小时前
linux系统编程(八):阻塞与非阻塞IO对比
android·linux·运维·服务器·github·vim
恋猫de小郭2 小时前
Flutter iOS Deep Link 为什么会突然失效:一系列难以言喻的问题
android·前端·flutter
Kapaseker3 小时前
小白都看得懂的 Skill 教程 - 初识 Skill
android·kotlin·vibecoding
神奇小梵3 小时前
安全和开发的需要了解的知识————下载内容的安全,和下载内容如何运行
android·windows·安全·个人开发
2501_915909063 小时前
iOS 证书创建与管理 类型限制 p12 密码与云备份实操
android·ios·小程序·https·uni-app·iphone·webview
黄毛火烧雪下4 小时前
Service 推送 / Stub·Proxy 对调
android
zhangphil4 小时前
Android Coil 3 ImageRequest.Builder error() 和 fallback()异同
android·kotlin