天天看点

java database connectivity前言正文总结

 jdbc是一种用来在java程序中执行sql的api,它为java连接数据库提供了一组接口和类。

连接数据库的过程:java app->jdbc api ->driver;

1.获得当前数据库连接的用户名和密码

2.获得数据库服务器的地址(ip)

3.获得数据库连接的端口号 oracle默认的是1521

4.获得书库的实例sid,即数据库名称

url=jdbc:oracle:thin:@ip:port:sid

classes12.jar或ojdbc14.jar

导入oracle数据库的驱动oracle.jdbc.OracleDriver或者oracle.jdbc.driver.OracleDriver

class.forname("oracle.jdbc.OracleDriver");//通过反射加载驱动程序,在内存中创建oracleDriver的实例

Connection conn=DriverManager.getConnection()url,user,password;;//通过驱动管理器获得连接对象

生成statement实现类对象来编译sql,并将sql语句输送到数据库

Statement stmt=conn.createStatemenr();

方式二:PreparedStatement pstmt=conn.prepareStatement(sql);//sql字符型不用加分号来结束

ResultSet rs=stmt.executeQuery(sql);;

方式二:ResultSet rs=pstmt.executeQuery();

while(rs.next()){

 int empno=rs.getInt(String columnName);

}

if(rs!=null){rs.close();}

if(pstmt!=null){pstmt.close();}

if(conn!=null){rconnclose();}

注:

1.prepareCall(String sql); 用于调用存储过程

2.conn.setAutoCommit(false);//设置当前jdbc的食物处理设置为手动

conn.rollback();

conn.commit();

3preparedStatement和Statement接口的区别

PreparedStatement 允许数据库预编译sql语句,这样在随后的运行中可以节省时间,并增加了查询的可读性;Statement每次执行sql语句相关的数据库都要执行sql语句的编译。

预编译sql会将sql语句预先编译号,执行的时候直接传递参数,不需要再次编译。

PreparedStatement是Statement的子接口,它与Statement的区别有:

1.程序的可读性和可维护性更好

2.更安全

3.执行效率高

select x.* from (select e.* ,rownum rr from emp e)x where x.rr between start and end;

分页有基于查询分页和基于缓存分页。

通过数据库中的伪劣,每次查询一部分数据返回。

1.特点

1)适合大数据量的分页操作

2)分页执行效率低,与数据库交互频繁。

 执行一次sql,将结果集的所有数据查询出来,存储在当前应用服务器的缓冲中,然后通过可滚动的结果集进行分页显示;

1)会将结果集全部放入内存,对于内存压力大

2)不适合大数据量的分页操作

3)分页时 ,第一次执行效率慢,以后执行效率高。

ResultSet默认只能向前遍历,并且只能遍历一次。

1.static int TYPE_SCROLL_INSENSITIVE

The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes to the data that underlies the ResultSet.设置获得的结果集是可滚动的,并且不敏感(数据库的数据变动不能及时反映到结果集)不受ResultSet底层数据更改的影响;而敏感是指将sql结果集的rowid存于缓存中,显示时通过rowId查询。

2.static int TYPE_SCROLL_SENSITIVE

The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes to the data that underlies the ResultSet.

3.static int CONCUR_READ_ONLY

The constant indicating the concurrency mode for a ResultSet object that may NOT be updated.设置结果集只读

4.static int CONCUR_UPDATABLE

The constant indicating the concurrency mode for a ResultSet object that may be updated.设置可更新的结果集。

1).更新

1))void updateString(int columnIndex, String x)

Updates the designated column with a String value. 更新当前记录的字段

2))void updateString(String columnLabel, String x)

Updates the designated column with a String value.

3))void updateRow()

Updates the underlying database with the new contents of the current row of this ResultSet object.更新记录到数据库(同步)

2).定位

1))int getRow()

Retrieves the current row number. 获得行号

2))void beforeFirst()

Moves the cursor to the front of this ResultSet object, just before the first row. 定位到第一条记录之前

3))void afterLast()

Moves the cursor to the end of this ResultSet object, just after the last row. 定位到最后一条记录之后

4))void deleteRow()

Deletes the current row from this ResultSet object and from the underlying database.删除当前行的记录,并同步到数据库。

3).插入

1))void moveToInsertRow()

Moves the cursor to the insert row. 将指针指向将要插入数据的行

2))void updateInt(int columnIndex, int x)

Updates the designated column with an int value.

3))void insertRow()

Inserts the contents of the insert row into this ResultSet object and into the database.同步到数据库

数据库连接池节省创建连接的时间;减少连接打开和关闭所消耗的资源。

commons-dbcp-1.4.jar和commons-pool.jar

ojdbc14.jar

BasicDataSource dbs=new BasicDataSource();

dbs.setUrl(jdbc:oracle:thin:@ip:port:sid);

dbs.setUsername(“username”);

dbs.setPassword("password");

dbs.setDriverClassName("oracle.jdbc.OracleDriver");

Connection conn=bds.getConnection();

大批量执行sql,为了提高效率采用批处理

1.jdbc有一组应用程序的api,用来开发java连接数据库的应用程序;jdbc驱动api提供给数据库厂商,数据库厂商负责实现底层的编码。

  1)) createStatement()  获得Statement

  2)) prepareStatement(String sql) 获得preparedStatement

  3)) prepareCall(String sql) 调用存储过程

   1))getConnection(String url, String user, String password)

   1))executeQuery(String sql)  执行DQL语句

 2))  executeUpdate(String sql) 执行DML语句

   3))executeBatch()    批处理执行SQL

  1)) setXXX方法   用来动态传参

  1)) next() 默认ResultSet对象指向记录的光标在第一条的前面

  2)) getXXX(String/int)  获得对应字段的值

  3)) getInt()  接收整数类型

   4))getDouble() 接收浮点数据

 5))  getString() 接收字符串类型

6))   sql.Date getDate()  接收Date类型数据字段