The Artima Developer Community
Sponsored Link

Java Buzz Forum
JDBC Updatable ResultSet Demo

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
JDBC Updatable ResultSet Demo Posted: Aug 3, 2014 10:14 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: JDBC Updatable ResultSet Demo
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement


Click the below Image to Enlarge

JDBCUpdatableResultSetDemo Project Dir Structure
JDBC Updatable ResultSet Demo
JDBC Updatable ResultSet Demo
JDBCUpdatableResultSetDemo.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCUpdatableResultSetDemo
{
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/world";

// Database credentials
static final String USERNAME = "root";
static final String PASSWORD = "root";

public static void main(String[] args)
{

JDBCUpdatableResultSetDemo jdbcUpdatableResultSetDemo =
new JDBCUpdatableResultSetDemo();
jdbcUpdatableResultSetDemo.updateOrInsertCityInformation();

}

private void updateOrInsertCityInformation()
{
Connection connection = null;
PreparedStatement preparedStatement = null;
try
{
/*
* Register the JDBC driver in DriverManager
*/


Class.forName(JDBC_DRIVER);

/*
* Establish connection to the Database using DriverManager
*/


connection = DriverManager
.getConnection(DB_URL, USERNAME, PASSWORD);

String sql = "select * from city limit ?";

/*
* Execute the query
*/

preparedStatement = connection.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
preparedStatement.setInt(1, 10);

ResultSet rs = preparedStatement.executeQuery();

/*
* Moves the cursor to the fifth row of rs and update the column
* information
*/


rs.absolute(5);
rs.updateInt("Population", 50000);
rs.updateRow();
System.out.println("5th row population column is updated");

/*
* Moves cursor to the insert row
*/


rs.moveToInsertRow();
rs.updateInt(1, 4080);
rs.updateString(2, "Chennai");
rs.updateString(3, "IND");
rs.updateString(4, "TamilNadu");
rs.updateInt(5, 200000);
rs.insertRow();

System.out.println("4080th row has been inserted in city table");

rs.close();

}
catch (SQLException se)
{
/*
* Handle errors for JDBC
*/

se.printStackTrace();
}
catch (ClassNotFoundException e)
{
/*
* Handle errors for Class.forName
*/

e.printStackTrace();
}
finally
{
/*
* finally block used to close resources
*/

try
{
if (preparedStatement != null)
{
preparedStatement.close();
}
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
try
{
if (connection != null)
{
connection.close();
}
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}

}

}

Output
5th row population column is updated
4080th row has been inserted in city table

Environment Used 

JDK version : 1.7.0_51
Mysql Server version : 5.6.19 

To Download JDBCUpdatableResultSetDemoApp Project Click the below link

https://sites.google.com/site/javaee4321/jdbc/JDBCUpdatableResultSetDemoApp.zip?attredirects=0&d=1

See also:
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Read: JDBC Updatable ResultSet Demo

    Topic: Keyword extraction and similarity calculation among textual content Previous Topic   Next Topic Topic: Clean JUnit Throwable-Tests with Java 8 Lambdas

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use