软件2班20240520

在这里插入图片描述

package com.yanyu;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest01 {
    public static void main(String[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
//           ctrl   alt  v
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
//        System.out.println(password);//  复制当前行  CTRL  d
//        System.out.println(driver);//  复制当前行  CTRL  d
//        System.out.println(url);//  复制当前行  CTRL  d
//        System.out.println(user);//  复制当前行  CTRL  d
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            Class.forName(driver);//  alt   enter
            con = DriverManager.getConnection(url,user,password);
            con.setAutoCommit(false);
            String sql = "insert into user values(1,'yy')";
            st = con.createStatement();
            st.execute(sql);






            con.commit();//  连接对象
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            if (con != null) {//  避开  空引用异常
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }


            throw new RuntimeException(e);
        }finally {
//               rs   st   con
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }


        }


    }
}

在这里插入图片描述

package com.yanyu;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest02 {
    public static void main(String[] args) {

        ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
//           ctrl   alt  v
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
//        System.out.println(password);//  复制当前行  CTRL  d
//        System.out.println(driver);//  复制当前行  CTRL  d
//        System.out.println(url);//  复制当前行  CTRL  d
//        System.out.println(user);//  复制当前行  CTRL  d
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,user,password);
            con.setAutoCommit(false);
            String sql = "update user set name = ? where id = ?";
//                                              1               2
//            预编译
            ps = con.prepareStatement(sql);//  操作对象
            ps.setInt(2,1);
            ps.setString(1,"yanyu");
            ps.execute();






            con.commit();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }



            throw new RuntimeException(e);
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }
    }
}

package com.yanyu;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest03 {
    public static void main(String[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
//           ctrl   alt  v
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
//        System.out.println(password);//  复制当前行  CTRL  d
//        System.out.println(driver);//  复制当前行  CTRL  d
//        System.out.println(url);//  复制当前行  CTRL  d
//        System.out.println(user);//  复制当前行  CTRL  d
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,user,password);
            con.setAutoCommit(false);
            String sql = "delete from user where id = ?";
//            String sql = "delete from user where id = 3";
            ps = con.prepareStatement(sql);
            ps.setInt(1,3);
            ps.execute();



            con.commit();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }

    }
}

package com.yanyu;

import java.sql.*;
import java.util.ResourceBundle;
/*
*    1.  建空工程    建  ,模块   
* 2.  建立包
* 3.属性配至
* driver=com.mysql.cj.jdbc.Driver
#    包名。   . 类名  、、、接口    首字母大写
url=jdbc:mysql://localhost:3306/soft02
user=root
password=root
4.读取配置文件
* 5.注册驱动
* 
* 
* 
* */
public class JDBCTest04 {
    public static void main(String[] args) {
        ResourceBundle bundle = ResourceBundle.getBundle("com/resources/db");
//           ctrl   alt  v
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
//        System.out.println(password);//  复制当前行  CTRL  d
//        System.out.println(driver);//  复制当前行  CTRL  d
//        System.out.println(url);//  复制当前行  CTRL  d
//        System.out.println(user);//  复制当前行  CTRL  d
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,user,password);
            con.setAutoCommit(false);
            String sql = "select * from user";
//            String sql = "delete from user where id = 3";
            ps = con.prepareStatement(sql);
//            ps.setInt(1,3);
            rs = ps.executeQuery();//   结果集
//遍历    结果集
            while (rs.next()){
                String name = rs.getString("name");
                String id = rs.getString("id");
                System.out.println(id + "---->"  + name);
            }
            con.commit();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值