数据库基础(二):JDBC

1.JDBC读取数据库的对象
2.JDBC读取示例
3.读取大文本大二进制
4.批处理

1.JDBC读取数据库的对象

           (1).读取步骤

                      ^1.导入数据库驱动

                      ^2.加载数据库驱动

                      ^3.建立连接(Connection)

                      ^4.创建发送SQ的对象,并做操作(Statement,PreparedStatement)

                      ^5.取得查到的数据(ResultSet)

                      ^6.关闭数据库

           (2).常用的数据库URL地址写法:

                      Mysql:

                                 jdbc://mysql://localhost:3306/sid

                      Oracle:

                                 jdbc:oracle:thin:@localhost:1521:sid

           (3).Connection:用于代表数据库的连接,客户端与数据库交互都是通过Connection完成的

                      createStatement():创建statement对象

                      prepareStatement(sql):创建PreparedStatement

                      prepareCall(sql):创建存储过程对象CallableStatement

                      setAutoCommit(boolean autoCommit):设置是否自动提交

                      commit():提交事务

                      rollback():回滚事务

           (4).Statement:用于向数据库发送sql

                      executeQuery(String sql):发送查询语句

                      executeUpdate(String sql):发送增删改语句

                      execute(String sql):发送任意sql

                      addBatch(String sql):多条语句放到一个批处理中

                      executeBatch():批处理执行

           (5).preparedStatement:用于向数据库发送sql(预编译机制)

                      方法同上

                      (5)比(4)好在哪里:

                                 ^1.避免sql注入

                                 ^2.避免频繁编译sql造成内存溢出

                                 ^3.简化书写

           (6).ResultSet:用于封装结果

                      getObject(int index):获取任意数据类型

                      getString(int index):获取指定数据类型

                      next():下一行结果

                      previous():上一行结果

                      absolute(int row):指定行

                      beforeFirst():第一行

                      afterLast():最后一行

           

2.JDBC读取示例

           public void query(){

                      Connection conn = null;

                      PreparedStatement ps = null;

                      ResultSet rs = null;

                      try{

                                 Class.forname("com.mysql.jdbc.Driver");//注册驱动

                                 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306:jdbc","root","root");//获取连接

                                 ps = conn.prepareStatement("select * from tb1 where id = ?");

                                 ps.setInt(1,1);

                                 rs = ps.executeQuery();

                                 while(rs.hasNext()){

                                            String name = rs.getString("name");

                                 }

                      }catch(Exception e){

                                 e.printStackTrace();

                      }finally{

                                 if(rs!=null){

                                            try{

                                                       rs.close();

                                            }catch(SQLException e){

                                                       e.printStackTrace();

                                            }finally{

                                                       rs= null;

                                            }

                                 }

                                 if(ps!=null){

                                            try{

                                                       ps.close();

                                            }catch(SQLException e){

                                                       e.printStackTrace();

                                            }finally{

                                                       ps= null;

                                            }

                                 }

                                 if(conn!=null){

                                            try{

                                                       conn.close();

                                            }catch(SQLException e){

                                                       e.printStackTrace();

                                            }finally{

                                                       conn = null;

                                            }

                                 }

                      }

           }

           注:一般把加载数据库那一段和关闭对象那一段提取出个方法

3.读取大文本大二进制

           (1).大文本:

                      Mysql:Text

                      Oracle:Clob

           (2).大二进制:Blob

           (3).三个注意点:

                      ^1.ps.setCharacterStream(2,new FileReader(file),(int)file.length())

                           ps.setBinaryStream(2,new FileReader(file),(int)file.length());

                      ^2.JVM内存需要设置一下

                                 cmd:java -Xms 64m -Xmx 128m

                                 VM arguments:-Xms 64m -Xmx 128m 

                      ^3.数据包的大小也要设置

                                 my.ini==>max_allowed_packet=64m

4.批处理

           批处理有两种方式:

           (1).statement.addBatch(sql)

                      批处理执行语句:executeBatch();

                      清除批处理指令:clearBatch();

                      优:可以执行多条结构不同的sql语句

                      缺:无预编译,效率低。当执行多条结构相同但是参数不同的数据时,sql的主干还需要写很多次

           (2).preparedStatement.addBatch()

                      写法同上

                      优:有预编译机制,执行多条结构相同参数不同的数据,不需要重复写主干

                      缺:不能执行结构不同的sql

           (3).示例

                      ps = conn.prepareStatement("insert into demo values(null,?)");

                      for(int i = 0;i<1000;i++){

                                 ps.setLong(1,"name"+i);

                                 ps.addBatch();

                                 if(i%100==0){

                                            ps.executeBatch();

                                            ps.clearBatch();

                                 }

                      }

                      ps.executeBatch();

猜你喜欢

转载自blog.csdn.net/qq_40594696/article/details/86426098
今日推荐