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();
            }
        }
    }
相关推荐
00后程序员张3 分钟前
如何在不同 iOS 设备上测试和上架 uni-app 应用 实战全流程解析
android·ios·小程序·https·uni-app·iphone·webview
米豆同学2 小时前
SufraceFlinger图像合成原理(3)-SurfaceFlinger中Layer的创建和销毁
android
米豆同学2 小时前
SufraceFlinger图像合成原理(2)-SurfaceFlinger与应用进程间的通信
android
用户2018792831672 小时前
uses-library:系统应用报NoClassDefFoundError问题
android
叽哥2 小时前
Kotlin学习第 4 课:Kotlin 函数:从基础定义到高阶应用
android·java·kotlin
mg6682 小时前
安卓玩机工具----安卓“搞机工具箱”最新版 控制手机的玩机工具
android·智能手机
诺诺Okami2 小时前
Android Framework- Activity启动2
android
米豆同学2 小时前
SystemUI plugin 开发
android
lichong9514 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之video 的各种状态和生命周期调用说明
android·vue.js·macos
app出海创收老李4 小时前
海外独立创收日记(1)-我是如何从0到1在Google Play获得睡后被动收入的?
android·程序员