1:JDBC 驱动
2:Connections (连接)
3:Statements (声明)
4:Result Sets (结果集)
在打开数据库连接前必须先加载数据库驱动
Class.forName("driverClassName");
Opening the Connection(打开连接)
String url = "jdbc:mysql://127.0.0.1:3306/appName"; String user = "root"; String password = "root"; Connection connection = DriverManager.getConnection(url,user,password);
Closing the Connection(关闭连接)
connection.close();
JDBC:查询数据库
对数据库进行操作,你需要创建SQL Statement,然后发送至数据库,得到返回结果.
Statement statement = connection.createStatement();
创建Statement后执行查询
String sql = "SELECT * FROM people";Result result = statement.excuteQuery(sql);
对结果集进行迭代
//result.next(); 返回true代表还有记录,返回false代表无更多记录 while(result.next()){ String name = result.getString("name"); int age = result.getInt("age"); } // 根据数据库字段名获取对应的数据 result.getString("columnName"); result.getLong("columnName"); result.getInt("columnName"); result.getDouble("columnName"); result.getBigDecimal("columnName"); // 通过字段索引顺序获取字段数据,索引从1开始 result.getString(1); result.getLong(2); // 通过字段名找到数据库对应字段索引的位置 int columnIndex = result.findColumn("columnName");
如果需要迭代大量数据,使用索引的效率要比查询字段名获取数据要快.
完成数据迭代后需要对resultSet和statement进行关闭.
result.close(); statement.close();
更新数据库包含更新数据库记录和删除数据库记录
excuteUpdate()方法可以用来更新和删除数据库记录.
Statement statement = connection.createStatement(); String sql = "UPDATE people SET name = "kevin" WHERE id = 1; int rowsAffected = statement.excuteUpdate(sql);
rowsAffected代表有多少条记录被更新了.
Statement statement = connection.createStatement(); String sql = "DELETE FROM people WHERE id = 1; int rowsAffected = statement.excuteUpdate(sql);
JDBC:结果集
可以通过执行Statement和PreparedStatement来创建结果集.
Statement statement = connection.createStatement(); String sql = "SELECT * FROM people"; Result result = statement.excuteQuery(sql); PreparedStatement statement = connection.preparedStatement(); Result result = statement.excuteQuery(sql);
更新结果集
result.updateString ("name" , "Alex"); result.updateInt ("age" , 55); result.updateBigDecimal ("coefficient", new BigDecimal("0.1323"); result.updateRow();
数据将在调用updateRow()方法后才将数据真正更新至数据库,如果updateRow()执行与事物中则需等到事物提交才将数据提交更新.
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();
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();
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 statement = null; try{ statement = connection.createStatement(); statement.addBatch("update people set firstname='John' where id=123"); statement.addBatch("update people set firstname='Eric' where id=456"); statement.addBatch("update people set firstname='May' where id=789"); int[] recordsAffected = statement.executeBatch(); } finally { if(statement != null) statement.close(); }
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(); } }
开始事务
connection.setAutoCommit(false);
事务回滚
connection.rollback();
提交事务
connection.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(); } }