jdbc总结

来源:互联网 发布:什么动物知天下事 编辑:程序博客网 时间:2024/04/29 13:52

jdbc:
String classname = “oracle.jdbc.OracleDriver”;
String url = “jdbc:oracle:thin:@127.0.0.1:1521:orcl”;
String username = “scott”;
String userpwd =”1234”;

    //声明连接    Connection connection = null;    Statement statement = null;    ResultSet resultSet = null;    try {        //1:加载驱动:通过java.lang.Class类.forName加载驱动        名称是驱动类的全限定名称,加载后放在程序当中,        底层用反射加载到了驱动类        Class.forName(classname);        //2:创建连接:通过驱动管理器类.getConnection("url","uname","pwd");        connection = DriverManager.getConnection(url,username,userpwd);        //3:创建sql命令发生器:通过数据库连接对象创建执行器,发送sql        的statement对象,        statement = connection.createStatement();        //4:创建sql发送sql获取结果集:        结果集:执行器对象.executeQuery();就可以执行了        对于dml是executeUpdate(sql),select 是executeQuery(sql);        String sql = " select * from student where sname = '"+uname+"' and pwd = '"+pwd+"' ";        System.out.println("HelloJdbc5.main(sql):"+sql);        resultSet = statement.executeQuery(sql);        //5:处理结果:先判断结果集对象的下一个对象是否存在        if(resultSet.next()){            System.out.println("HelloJdbc5.main(登陆成功)");        }else{            System.out.println("HelloJdbc5.main(登陆失败)");        }    } catch (ClassNotFoundException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }finally{        //6:关闭连接:关闭连接需要先判断连接是否为空        //否则就会产生空指针异常        //判断是否为空        if(resultSet!=null){            try {                resultSet.close();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        if(statement!=null){            try {                statement.close();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        if(connection!=null){            try {                connection.close();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}

记住:
a.jdbc六步:
1.加载驱动,2.获取连接3.创建sql命令发生器4.执行sql
5.得到结果集 6.关闭数据库对象,后打开的先关闭
b.倒序遍历结果集:
//3:如果需要倒序遍历 就需要指定两个参数,第一个参数是结果集类型滚动敏感和结果集的光标只读
statement =connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

        //4:发送sql,获取结果 ,主键可以用序列自动生成,时间获取当前的时间        String sqlall = "select * from emp order by empno";        //结果集        resultSet = statement.executeQuery(sqlall);        //需要先把结果集的指针移动到数据的最后一行之后,相当于光标倒着滚动        resultSet.afterLast();        //5:处理结果集  resultSet.previous():如果resultSet里面有上一个值就返回ture        //遍历结果集用while,判断结果集光标前面是否存在对象就用previous()        while (resultSet.previous()) {            int empno = resultSet.getInt("empno");//number(m)            String ename = resultSet.getString("ename");//varchar2(),varchar()            String job = resultSet.getString("job");            int mgr = resultSet.getInt("mgr");            Date date = resultSet.getDate("hiredate");//date            double sal = resultSet.getDouble("sal");//number(m,n)            double comm = resultSet.getDouble("comm");            int deptno = resultSet.getInt("deptno");            System.out.println("HelloJdbc3.main(用户信息):"+empno+" "+ename+" "+job+" "+mgr+" "+date+" "+sal+" "+comm+" "+deptno);        }  加载驱动的方式:  DriverManager.registerDriver(new Driver());    Class.forName("com.mysql.jdbc.Driver");
原创粉丝点击