CodesJava

Easy learning with example program codes

JDBC PreparedStatement updates a record example


The JDBC PreparedStatement is used to execute parameterized queries against the database. Let us study JDBC PreparedStatement by updates a record example.

Example:

JDBCTest.java

import java.sql.Connection;
import java.sql.PreparedStatement;
import com.codesjava.util.JDBCUtil;
 
/**
 * This class is used to update a record in DB table
 * using PreparedStatement.
 * @author codesjava
 */
public class JDBCTest {
	public static void main(String args[]){
		Connection conn = null;
		PreparedStatement preparedStatement = null;
 
		String query = "update EMPLOYEE set " +
		"SALARY = ? " + 
		"where EMPLOYEE_ID = ? ";
 
		try{			
			//get connection
			conn = JDBCUtil.getConnection();
 
			//create preparedStatement
			preparedStatement = conn.prepareStatement(query);
 
			//set values
			preparedStatement.setInt(2, 1);
			preparedStatement.setInt(1, 65000);
 
			//execute query
			preparedStatement.executeUpdate();
 
			//close connection
			preparedStatement.close();
			conn.close();
 
		      System.out.println("Record updated successfully.");
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}

JDBCUtil.java

import java.sql.Connection;
import java.sql.DriverManager;
 
/**
 * This is a utility class for JDBC connection.
 * @author codesjava
 */
public class JDBCUtil {
	//JDBC and database properties.
	private static final String DB_DRIVER = 
		           "oracle.jdbc.driver.OracleDriver";
	private static final String DB_URL = 
		        "jdbc:oracle:thin:@localhost:1521:XE";
	private static final String DB_USERNAME = "system";
	private static final String DB_PASSWORD = "oracle";
 
	public static Connection getConnection(){
		Connection conn = null;
		try{
			//Register the JDBC driver
			Class.forName(DB_DRIVER);
 
			//Open the connection
			conn = DriverManager.
			getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
 
			if(conn != null){
			   System.out.println("Successfully connected.");
			}else{
			   System.out.println("Failed to connect.");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}	
}

Output:

Record updated successfully.

Download this example.
 
Next Topic: JDBC PreparedStatement select records example.
Previous Topic: JDBC PreparedStatement inserts a record example.

Related Topics:

JDBC PreparedStatement creates a table example.
JDBC PreparedStatement inserts a record example.
JDBC PreparedStatement updates a record example.
JDBC PreparedStatement select records example.
JDBC PreparedStatement deletes a record example.
JDBC PreparedStatement batch update example.

 

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki