Demo Updatable ResultSet : ResultSet Updatable « Database SQL JDBC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Database SQL JDBC » ResultSet Updatable 




Demo Updatable ResultSet
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DemoUpdatableResultSet {
  public static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "name";
    String password = "password";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static void main(String[] args) {
    ResultSet rs = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
      conn = getConnection();
      String query = "select id, name, age from employees where age > ?";
      pstmt = conn.prepareStatement(query, ResultSet.TYPE_SCROLL_SENSITIVE,
          ResultSet.CONCUR_UPDATABLE);
      pstmt.setInt(120)// set input values
      rs = pstmt.executeQuery()// create an updatable ResultSet
                                               // update a column value in the current row.
      rs.absolute(2);                          // moves the cursor to the 2nd row of rs
      rs.updateString("name""newName");      // updates the 'name' column of row 2 to newName
      rs.updateRow();                          // updates the row in the data source
                                               // insert column values into the insert row.
      rs.moveToInsertRow();                    // moves cursor to the insert row
      rs.updateInt(11234);                   // 1st column id=1234
      rs.updateString(2"newName");           // updates the 2nd column
      rs.updateInt(399);                     // updates the 3rd column to 99
      rs.insertRow();
      rs.moveToCurrentRow();
    catch (Exception e) {
      e.printStackTrace();
    finally {
      try {
        rs.close();
        pstmt.close();
        conn.close();
      catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}

           
         
  














Related examples in the same category
1.If database support updatable result sets
2.Using UpdatableResultSet to insert a new row
3.Delete Row from Updatable ResultSet for MySQL
4.Insert Row to Updatable ResultSet from MySQL
5.Make updates in Updatable ResultSet
6.Updatable resultset with Oracle Driver
7.Determining If a Database Supports Updatable Result Sets: An updatable result set allows modification to data in a table through the result set.
8.Creating an Updatable Result Set
9.Determining If a Result Set Is Updatable
10.Updating a Row in a Database Table Using an Updatable Result Set
11.Cancelling Updates to an Updatable Result Set
12.Inserting a Row into a Database Table Using an Updatable Result Set
13.Deleting a Row from a Database Table Using an Updatable Result Set
14.Refreshing a Row in an Updatable Result Set
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.