Insert Row to Updatable ResultSet from MySQL : ResultSet Updatable « Database SQL JDBC « Java

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. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Database SQL JDBC » ResultSet UpdatableScreenshots 
Insert Row to Updatable ResultSet from MySQL
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class InsertRowUpdatableResultSet_MySQL {

  public static Connection getConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/databaseName";
    String username = "root";
    String password = "root";
    Class.forName(driver);
    return DriverManager.getConnection(url, username, password);
  }

  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = getConnection();
      String query = "select id, name from employees";
      stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      rs = stmt.executeQuery(query);
      while (rs.next()) {
        String id = rs.getString(1);
        String name = rs.getString(2);
        System.out.println("id=" + id + "  name=" + name);
      }
      // Move cursor to the "insert row"
      rs.moveToInsertRow();
      // Set values for the new row.
      rs.updateString("id""001");
      rs.updateString("name""newName");
      // Insert the new row
      rs.insertRow();
      // scroll from the top again
      rs.beforeFirst();
      while (rs.next()) {
        String id = rs.getString(1);
        String name = rs.getString(2);
        System.out.println("id=" + id + "  name=" + name);
      }
    catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    finally {
      // release database resources
      try {
        rs.close();
        stmt.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. Make updates in Updatable ResultSet
5. Demo 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.