JDBC六部曲

 public static void main(String []args) {
        Connection con = null;
        Statement sta = null;
        ResultSet rs = null;
        try {
            //注册驱动
            //Class.forName("com.mysql.jdbc.Driver");
            Driver d = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(d);
            //获取数据库连接
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","xucanjie");
            //获取数据库操作对象
            sta = con.createStatement();
            //执行SQL语句
            String sql = "select e.ename,e.sal,d.dname from emp e join dept d on e.deptno=d.deptno";
            rs=sta.executeQuery(sql);
            //处理查询结果集
            while(rs.next()) {
                System.out.println("员工姓名:" + rs.getString("ename") + " 员工部门:" + rs.getString("dname") + " 员工工资:" + rs.getInt("sal"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(sta!=null){
                try {
                    sta.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
Connection con = null;
        //Statement sta = null;
        PreparedStatement psta = null;
        ResultSet rs = null;
        try {
            //注册驱动的两种方式
            //Class.forName("com.mysql.jdbc.Driver");
            Driver d = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(d);
            //获取数据库连接
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","xucanjie");
            //获取数据库操作对象,预编译
            String sql = "select e.ename,e.sal,d.dname from emp e join dept d on e.deptno=d.deptno where sal>?";
            psta = con.prepareStatement(sql);
            psta.setInt(1,2500);
            //执行SQL语句
            rs=psta.executeQuery();
            //处理查询结果集
            while(rs.next()) {
                System.out.println("员工姓名:" + rs.getString("ename") + " 员工部门:" + rs.getString("dname") + " 员工工资:" + rs.getInt("sal"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            //关闭
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(psta!=null){
                try {
                    psta.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值