计应3班--31

在这里插入图片描述

public class Dome1 {
    //    main
    public static void main(String[] args) {
        //ctrl   alt   l
        //读取属性配置文件
        ResourceBundle bundle = ResourceBundle.getBundle("com\\company\\db");
        //  ctrl  alt  v
        String driver = bundle.getString("driver");
        String user = bundle.getString("user");
        String password = bundle.getString("password");
        String url = bundle.getString("url");


        //  放大  变量  作用域
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        //注册驱动
        try {
            Class.forName(driver);
            //类名调用  静态方法
            //alt  enter
            //获取连接对象
            con = DriverManager.getConnection(url, user, password);
            //操作
            st = con.createStatement();
            //写sql语句
            String sql = "insert into user(name,password) values('小明','123456')";
            //执行
            st.execute(sql);

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            //关闭
        } finally {
            //rs    st  con
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }
}


public class Dome2 {
// main
public static void main(String[] args) {
//ctrl alt l
//读取属性配置文件
ResourceBundle bundle = ResourceBundle.getBundle(“com\company\db”);
// ctrl alt v
String driver = bundle.getString(“driver”);
String user = bundle.getString(“user”);
String password = bundle.getString(“password”);
String url = bundle.getString(“url”);

    //  放大  变量  作用域
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;

    //注册驱动
    try {
        Class.forName(driver);
        //类名调用  静态方法
        //alt  enter
        //获取连接对象
        con = DriverManager.getConnection(url, user, password);
        //操作
        st = con.createStatement();
        //写sql语句
        String sql = "select * from compamy";   //查
        rs = st.executeQuery(sql);
        while(rs.next()){
            System.out.println(rs.getString(1));
            System.out.println(rs.getString(2));
            System.out.println(rs.getString(3));
        }   //查
        //执行
        st.execute(sql);

    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
        //关闭
    } finally {
        //rs    st  con
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值