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();
            }
        }
    }
相关推荐
CYRUS_STUDIO7 小时前
Frida 检测与对抗实战:进程、maps、线程、符号全特征清除
android·逆向
csj508 小时前
安卓基础之《(28)—Service组件》
android
lhbian10 小时前
PHP、C++和C语言对比:哪个更适合你?
android·数据库·spring boot·mysql·kafka
catoop11 小时前
Android 最佳实践、分层架构与全流程解析(2025)
android
ZHANG13HAO11 小时前
Android 13 特权应用(Android Studio 开发)调用 AOSP 隐藏 API 完整教程
android·ide·android studio
田梓燊12 小时前
leetcode 142
android·java·leetcode
angerdream12 小时前
Android手把手编写儿童手机远程监控App之JAVA基础
android
菠萝地亚狂想曲13 小时前
Zephyr_01, environment
android·java·javascript
sTone8737513 小时前
跨端框架通信机制全解析:从 URL Schema 到 JSI 到 Platform Channel
android·前端
sTone8737513 小时前
Java 注解完全指南:从 "这是什么" 到 "自己写一个"
android·前端