编辑这个页面须要登录或更高权限!
以下是使用Result Set教程中描述的ResultSet.CONCUR_UPDATABLE和ResultSet.TYPE_SCROLL_INSENSITIVE示例。本示例将说明对表的INSERT,UPDATE和DELETE操作。
应该注意的是,您正在处理的表应正确设置主键。
该示例代码是根据前几章中的环境和数据库设置编写的。
复制并粘贴以下示例到JDBCExample.java中,如下编译并运行:
//步骤1.导入所需的软件包 import java.sql.*; public class JDBCExample { // JDBC驱动程序名称和数据库URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // 数据库凭证 static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; try{ //步骤2:注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); //步骤3:建立连接 System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //步骤4:执行查询以创建陈述 // RS示例的必需参数。 System.out.println("Creating statement..."); Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //步骤5:执行查询 String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); System.out.println("List result set for reference...."); printRs(rs); //步骤6:循环浏览结果集,并增加5个年龄 //移至BFR位置,以便while循环正常工作 rs.beforeFirst(); //步骤7:从结果集中提取数据 while(rs.next()){ //按列名检索 int newAge = rs.getInt("age") + 5; rs.updateDouble( "age", newAge ); rs.updateRow(); } System.out.println("List result set showing new ages..."); printRs(rs); // 在表中插入一条记录。 //移动以使用updateXXX()插入行并添加列数据 System.out.println("Inserting a new record..."); rs.moveToInsertRow(); rs.updateInt("id",104); rs.updateString("first","John"); rs.updateString("last","Paul"); rs.updateInt("age",40); //提交行 rs.insertRow(); System.out.println("List result set showing new set..."); printRs(rs); // 从表中删除第二条记录。 // 首先将位置设置为第二条记录 rs.absolute( 2 ); System.out.println("List the record before deleting..."); //按列名检索 int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //显示值 System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); //删除行 rs.deleteRow(); System.out.println("List result set after \ deleting one records..."); printRs(rs); //步骤8:清理环境 rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //处理JDBC错误 se.printStackTrace(); }catch(Exception e){ //处理Class.forName的错误 e.printStackTrace(); }finally{ //用于关闭资源 try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!"); }//结束main public static void printRs(ResultSet rs) throws SQLException{ //确保我们从第一行开始 rs.beforeFirst(); while(rs.next()){ //按列名检索 int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //显示值 System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } System.out.println(); }//结束printRs() }//结束JDBCExample
现在让我们编译上面的示例,如下所示:
C:\>javac JDBCExample.java C:\>
运行时JDBCExample,它将产生以下结果-
C:\>java JDBCExample Connecting to database... Creating statement... List result set for reference.... ID: 100, Age: 33, First: Zara, Last: Ali ID: 101, Age: 40, First: Mahnaz, Last: Fatma ID: 102, Age: 50, First: Zaid, Last: Khan ID: 103, Age: 45, First: Sumit, Last: Mittal List result set showing new ages... ID: 100, Age: 38, First: Zara, Last: Ali ID: 101, Age: 45, First: Mahnaz, Last: Fatma ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal Inserting a new record... List result set showing new set... ID: 100, Age: 38, First: Zara, Last: Ali ID: 101, Age: 45, First: Mahnaz, Last: Fatma ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal ID: 104, Age: 40, First: John, Last: Paul List the record before deleting... ID: 101, Age: 45, First: Mahnaz, Last: Fatma List result set after deleting one records... ID: 100, Age: 38, First: Zara, Last: Ali ID: 102, Age: 55, First: Zaid, Last: Khan ID: 103, Age: 50, First: Sumit, Last: Mittal ID: 104, Age: 40, First: John, Last: Paul Goodbye! C:\>