Java JDBC 基础
JDBC API 包含以下几个核心部分:
1:JDBC 驱动
2:Connections (连接)
3:Statements (声明)
4:Result Sets (结果集)
JDBC: 打开数据库连接
Loading the JDBC Driver(加载数据库驱动)
在打开数据库连接前必须先加载数据库驱动
Class.forName("driverClassName");
Opening the Connection(打开连接)
String url = "jdbc:mysql://127.0.0.1:3306/appName"; String user = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url,user,password);
Closing the Connection(关闭连接)
connection.close();
JDBC:查询数据库
对数据库进行操作,你需要创建SQL
Statement,然后发送至数据库,得到返回结果.
Statement statement = conn.createStatement();
创建Statement后执行查询
String sql = "select * from table"; ResultSet result = statement.executeQuery(sql);
对结果集进行迭代
while(result.next()){ String name = result.getString("name"); int age = result.getInt("age"); } //根据数据库字段名获取对应的数据 result.getString("columnName"); result.getInt("columnName"); result.getLong("columnName"); result.getDouble("columnName"); result.getBigDecimal("columnName"); //通过字段索引顺序获取字段数据,索引从1开始 result.getInt(1); result.getString(2); //通过字段名找到数据库对应字段索引的位置 int columnIndex = result.findColumn("columnName");
如果需要迭代大量数据,使用索引的效率要比查询字段名获取数据要快.
完成数据迭代后需要对resultSet和statement进行关闭.
result.close();
statement.close();
JDBC:更新数据库
更新数据库包含更新数据库记录和删除数据库记录
excuteUpdate()方法可以用来更新和删除数据库记录.
更新记录
Statement statement = conn.createStatement(); String sql = "update table set name = ’name‘ where id = 1“; int rowAffected = statement.executeUpdate(sql);
rowsAffected代表有多少条记录被更新了.
删除记录
像上面那样更新直接执行sql语句
JDBC:结果集
创建结果集
可以通过执行Statement和PreparedStatement来创建结果集.
Statement statement = conn.createStatement(); String sql = "select * from table"; ResultSet result = statement.executeQuery(sql); PrepareStatement statement = conn.preparedStatement(); ResultSet result = statement.executeQuery(sql);
更新结果集
result.updateString("name","alex"); result.updateInt("age",33); result.updatBigDecimal("cofficient",new BigDecimal("0.1343"); result.updateRow();
数据将在调用updateRow()方法后才将数据真正更新至数据库,如果updateRow()执行与事物中则需等到事物提交才将数据提交更新.
JDBC:PreparedStatement
PreparedStatement特性:
-
更容易操作sql statement 参数
-
可对preparedstatement的重用
-
更容易的批处理
操作示例
String sql = "update people set firstname=? , lastname=? where id=?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, "Gary"); preparedStatement.setString(2, "Larson"); preparedStatement.setLong (3, 123); int rowsAffected = preparedStatement.executeUpdate();
创建PreparedStatement
String sql = "select * from people where firstname=? and lastname=?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, "John"); preparedStatement.setString(2, "Smith"); ResultSet result = preparedStatement.executeQuery();
PreparedStatement的重用
String sql = "update people set firstname=? , lastname=? where id=?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, "Gary"); preparedStatement.setString(2, "Larson"); preparedStatement.setLong (3, 123); int rowsAffected = preparedStatement.executeUpdate(); preparedStatement.setString(1, "Stan"); preparedStatement.setString(2, "Lee"); preparedStatement.setLong (3, 456); int rowsAffected = preparedStatement.executeUpdate();
Statement Batch Updates
Statement statement = null; try{ statement = conn.createStatement(); statement.addBatch("update people set firstname=‘John‘ where id=123"); statement.addBatch("update people set firstname=‘Eric‘ where id=456"); int[] recordsAffected = statement.executeBatch(); }finally{ if(statent != null) statement.close(); }
PreparedStatement Batch Updates
String sql = "update people set firstname=? , lastname=? where id=?"; PreparedStatement preparedStatement = null; try{ preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, "Gary"); preparedStatement.setString(2, "Larson"); preparedStatement.setLong (3, 123); preparedStatement.addBatch(); preparedStatement.setString(1, "Stan"); preparedStatement.setString(2, "Lee"); preparedStatement.setLong (3, 456); preparedStatement.addBatch(); int[] affectedRecords = preparedStatement.executeBatch(); }finally { if(preparedStatement != null) { preparedStatement.close(); } }
开始事务
conn.setAutoCommit(false);
事务回滚
conn.rollback();
提交事务
conn.commit();
示例
Connection connection = ... try{ connection.setAutoCommit(false); Statement statement1 = null; try{ statement1 = connection.createStatement(); statement1.executeUpdate( "update people set name=‘John‘ where id=123"); } finally { if(statement1 != null) { statement1.close(); } } Statement statement2 = null; try{ statement2 = connection.createStatement(); statement2.executeUpdate( "update people set name=‘Gary‘ where id=456"); } finally { if(statement2 != null) { statement2.close(); } } connection.commit(); } catch(Exception e) { connection.rollback(); } finally { if(connection != null) { connection.close(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。