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();
            }
        }
    }
相关推荐
恋猫de小郭15 分钟前
2026 Android I/O ,全新 AI 手机、 Android PC 和自动驾驶
android·人工智能·智能手机
三产30 分钟前
Hermes 教程 03:Skills 系统
android·java·数据库
AI玫瑰助手31 分钟前
Python流程控制:for循环遍历字符串列表字典
android·java·python
2501_9159184137 分钟前
iOS性能数据监控:从概念到工具实践,让应用运行更流畅
android·macos·ios·小程序·uni-app·cocoa·iphone
帅次1 小时前
测试分层:JVM 单测、ViewModel 测试与 Compose UI Test
android·jvm·ui·kotlin·compose·modifier
背包客(wyq)1 小时前
开源中文语音模型Android端部署测试
android·开源
yewq-cn1 小时前
Android Compatibility
android
马占柱1 小时前
MediaSession相关介绍
android·多媒体·mediasession·车载多媒体开发
张风捷特烈1 小时前
状态管理大乱斗#08 | MobX 源码评析 - 透明魔法
android·前端·flutter
i220818 Faiz Ul1 小时前
个人健康系统|健康管理|基于java+Android+微信小程序的个人健康系统设计与实现(源码+数据库+文档)
android·java·vue.js·spring boot·微信小程序·毕设·个人健康系统