JDBC简介及其用Java连接数据库

一、JDBC的主要作用:用Java代码来操作数据库(oracle,mySQL)

1、数据的添加

2、数据的删除

3、数据的更新

4、数据的查询

5、事物

二、编写JDBC的步骤

1、加载驱动——不同的数据库加载不同的驱动。

2、获取数据库连接  connection

3、构建statement 或者preparedstatement 对象   ——执行的SQL

4、执行SQL语句 ——返回结果

5、解析结果

6、关闭statement 或者preparedstatement对象和连接

实现代码如下:

 1 public class last 
 2 {
 3     public static void main(String[] args)
 4     {
 5         Connection conn = null;
 6         PreparedStatement ptem = null;
 7         ResultSet resu = null;
 8         
 9         try {
10             //1、加载驱动——固有写法
11             Class.forName("oracle.jdbc.driver.OracleDriver");
12             //2、连接数据库——固有写法
13             String url = "jdbc:oracle:thin:@localhost:1521:orcl";
14             conn = DriverManager.getConnection(url, "scott", "sbc111");
15             System.out.println(conn);
16             //3、构建preparedstatement对象执行SQL语句
17             String sql = "select empno,ename,job,mgr from emp";
18             ptem = conn.prepareStatement(sql);
19             //4、执行SQL语句
20             resu = ptem.executeQuery(sql);
21             //5、用while遍历 来解析结果
22             while(resu.next())
23             {
24                 int a = resu.getInt("empno");
25                 String b = resu.getString("ename");
26                 String c = resu.getString("job");
27                 String d = resu.getString("mgr");
28                 //e = resu.getDate("hiredate");
29                 //resu.getInt("sal");
30                 //resu.getInt("comm");
31                 //resu.getString("deptno");
32                 
33                 System.out.println(a + " : " + b + " : " + c + " : " + d);
34             }
35             
36         } catch (ClassNotFoundException e) {
37             e.printStackTrace();
38         } catch (SQLException e) {
39             e.printStackTrace();
40         }finally //关闭资源
41         {
42             if(conn !=null)
43             {
44                 try {
45                     conn.close();
46                 } catch (SQLException e) {
47                     e.printStackTrace();
48                 }
49             }
50             if(ptem !=null)
51             {
52                 try {
53                     ptem.cancel();
54                 } catch (SQLException e) {
55                     e.printStackTrace();
56                 }
57             }
58             if(resu !=null)
59             {
60                 try {
61                     resu.close();
62                 } catch (SQLException e) {
63                     e.printStackTrace();
64                 }
65             }
66         }
67     }
68 }

 JDBC实现oracle分页

1、基于内存:

特点:运行速度快,但是非常占用资源,一般用在小数据

  1 public class JDBCSort 
  2 {
  3     /**
  4      * 
  5      * @param page 当前要显示的页数
  6      * @param pageSize 每页显示多少行
  7      * 编写步骤:
  8      *     1. 判断pageSize是否越界
  9      *  2. 判断page是否越界
 10      *  3. 创建一个可以滚动的Statement对象
 11      *  4. 执行sql语句
 12      *  5. 让结果集(ResultSet)里面的指针指向当前分页的第一行
 13      *  6. 往下next pageSize次获取当前分页要显示的所有行
 14      *  7. 关闭所有的资源
 15      * 
 16      * 
 17      */
 18     public static void getData(int page,int pageSize)
 19     {
 20         String sql = "select * from emp";
 21         String count = "select count(*) from emp";
 22         Connection conn = DBUtils.getConnection();
 23         int maxCount;
 24         PreparedStatement pstm = null;
 25         ResultSet set = null;
 26         Statement st = null;
 27         ResultSet set1 = null;
 28         try {
 29             pstm = conn.prepareStatement(count);
 30             set = pstm.executeQuery();
 31             maxCount = 0;
 32             while(set.next())
 33             {
 34                 maxCount = set.getInt(1);
 35             }
 36             
 37             //判断每页显示的行数是否越界
 38             if(pageSize < 10)
 39             {
 40                 pageSize = 10;
 41             }else if(pageSize > maxCount)
 42             {
 43                 pageSize = maxCount;
 44             }
 45             
 46             //计算当前的总条数可以分多少页
 47             if(maxCount % pageSize == 0)
 48             {
 49                 maxCount = maxCount/pageSize;
 50             }else
 51             {
 52                 maxCount = maxCount/pageSize + 1;
 53             }
 54             
 55             //判断page是否有越界
 56             if(page < 1)
 57             {
 58                 page = 1;
 59             }
 60             if(page > maxCount)
 61             {
 62                 page = maxCount; //如果传递过来的页数超过了最大的分页数,那么显示最后一页
 63             }
 64             
 65             /**
 66              * ResultSet.TYPE_SCROLL_INSENSITIVE 设置得到的结果集可以滚动显示
 67              * ResultSet.CONCUR_READ_ONLY : 设置结果集数据为只读
 68              */
 69             st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
 70             set1 = st.executeQuery(sql);
 71             
 72             set1.absolute((page-1)*pageSize + 1);// 定位到当前要显示也的第一行
 73             
 74             for(int i = pageSize; i > 0; i--)
 75             {
 76                 String ename = set1.getString("ename");
 77                 System.out.println(ename);
 78                 
 79                 if(!set1.next())// set1.next()如果没有下一行则返回false,然后在取反,终止循环
 80                 {
 81                     break;
 82                 }
 83             }
 84             
 85             
 86             /*//显示第十一行记录的员工名称
 87             set1.absolute(10); //直接指向结果集里面的某一行记录
 88             System.out.println(set1.getString("ename"));
 89             set1.next(); //指向结果集里面的下一行
 90             System.out.println(set1.getString("ename"));
 91             set1.previous(); // 指向上一行
 92             System.out.println(set1.getString("ename"));
 93             set1.relative(3);//相对于现在的位置移动3个位置
 94             System.out.println(set1.getString("ename"));*/
 95             
 96         } catch (SQLException e) {
 97             e.printStackTrace();
 98         }finally
 99         {
100             DBUtils.closeResultSet(set);
101             DBUtils.closeResultSet(set1);
102             DBUtils.closeStatement(st);
103             DBUtils.closePreparedStatement(pstm);
104             DBUtils.closeConnection(conn);
105         }
106         
107     }
108 }

2、基于数据库

特点:占用资源少,但是速度较内存蛮,而且跟数据库交互多,但是可以应对大数据

 1 public class JDBCSort1
 2 {
 3     /**
 4      * 编程步骤:
 5      *     1. 判断pageSize是否越界
 6      *  2. 判断page是否越界
 7      *  3. 计算当前页要显示的第一行和最后一行
 8      *  4. 设置参数,然后执行sql语句
 9      *  5. 解析结果
10      *  6. 关闭资源
11      * @param page
12      * @param pageSize
13      */
14     public static void getData(int page,int pageSize)
15     {
16         String sql = "select r,empno,ename from ( "
17                     +    " select rownum r,empno,ename from emp where rownum <= ? "
18                     +" ) where r>= ? ";
19         String count ="select count(*) from emp";
20         int countMax = 0;
21         Connection conn = DBUtils.getConnection();
22         PreparedStatement pstm = null;
23         ResultSet set = null;
24         try {
25             pstm = conn.prepareStatement(count);
26             set = pstm.executeQuery();
27             while(set.next())
28             {
29                 countMax = set.getInt(1);//得到总行数
30             }
31             DBUtils.closeResultSet(set);
32             DBUtils.closePreparedStatement(pstm);
33             
34             if(pageSize < 10)
35             {
36                 pageSize = 10;
37             }else if(pageSize > countMax)
38             {
39                 pageSize = countMax;
40             }
41             
42             //获取总分页数
43             if(countMax % pageSize == 0)
44             {
45                 countMax = countMax/pageSize;// 得到总分页数
46             }else
47             {
48                 countMax = countMax/pageSize + 1;
49             }
50             
51             if(page < 1)
52             {
53                 page = 1;
54             }else if(page > countMax)
55             {
56                 page = countMax;
57             }
58             
59             //就算当前页的起始位置和结束位置
60             int start = (page-1)*pageSize + 1;
61             int end = page*pageSize;
62             
63             pstm = conn.prepareStatement(sql);
64             pstm.setInt(1, end);
65             pstm.setInt(2, start);
66             
67             set = pstm.executeQuery();
68             while(set.next())
69             {
70                 System.out.println(set.getString("ename"));
71             }
72             
73         } catch (SQLException e) {
74             e.printStackTrace();
75         }finally
76         {
77             DBUtils.closeResultSet(set);
78             DBUtils.closePreparedStatement(pstm);
79             DBUtils.closeConnection(conn);
80         }
81     }
82 }
原文地址:https://www.cnblogs.com/cyg-06/p/6044457.html