Pages Navigation Menu

Coding is much easier than you think

JDBC PreparedStatement example to Delete a record

Posted by on Sep 1, 2013 in JDBC Tutorials | 0 comments

drop table
 
In this example let us learn how to delete a record from a table via JDBC PreparedStatement.
 
To issue a delete statement, calls the PreparedStatement.executeUpdate() as show below
 

String deleteQuery = "DELETE EMPLOYEE WHERE USER_ID = ?";
PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery);
preparedStatement.setInt(1, 1000);
// execute delete SQL statement
preparedStatement.executeUpdate();

 
Full example…
 

package com.simplecode.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCPreparedStmtDeleteExample 
{

private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String serverName = "127.0.0.1";
private static String portNumber = "1521";
private static String sid = "XE";
private static final String dbUrl = "jdbc:oracle:thin:@" + serverName + ":"
		+ portNumber + ":" + sid;
private static final String dbUser = "system";
private static final String dbPassword = "admin";

public static void main(String[] argc) 
{
	try 
	{
		deleteRecordFromTable();
	}
	catch (SQLException e) 
	{
		System.out.println(e.getMessage());
	}
}

private static void deleteRecordFromTable() throws SQLException 
{
	Connection connection = null;
	PreparedStatement prepareStmt = null;
	String deleteQuery = "DELETE EMPLOYEE WHERE USER_ID = ?";
try 
{
	connection = getDBConnection();
	prepareStmt = connection.prepareStatement(deleteQuery);
	prepareStmt.setInt(1, 1000);
	// execute update SQL statement
	prepareStmt.executeUpdate();
	System.out.println("Record is deleted!");
}
catch (SQLException e) 
{
	System.err.println(e.getMessage());
} 
	
finally 
{
	if (prepareStmt != null) 
	{
		prepareStmt.close();
	}
	if (connection != null) 
	{
		connection.close();
	}
	}
}

private static Connection getDBConnection() 
{
	Connection dbConnection = null;
	try 
	{
		Class.forName(dbDriver);
	}
	catch (ClassNotFoundException e) 
	{
		System.err.println(e.getMessage());
	}

	try 
	{
	dbConnection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
	return dbConnection;
	}
	catch (SQLException e) 
	{
		System.err.println("Db "+e.getMessage());
	}
	return dbConnection;
}
}

 

Read More

JDBC Statement example to Delete a Record

Posted by on Aug 29, 2013 in JDBC Tutorials | 0 comments

drop table
 
Here is an example to show you how to delete a record from a table via JDBC statement.
 
To issue a delete statement, calls the Statement.executeUpdate() method as shown below :
 

Statement statement = connection.createStatement();
// execute delete  SQL stetement
statement.executeUpdate(deleteTableQuery);

 
Full example…
File: JDBCStatementDeleteExample.java
 

package com.simplecode.jdbc;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;

public class JDBCStatementDeleteExample  
{

private static final String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String serverName = "127.0.0.1";
private static String portNumber = "1521";
private static String sid = "XE";
private static final String dbUrl ="jdbc:oracle:thin:@"+serverName+":"+ portNumber+":"+sid;
private static final String dbUser = "system";
private static final String dbPassword = "admin";


	public static void main(String[] argc) 
	{
		try 
		{
			deleteRecordFromEmployeeTable();
		}
		catch (SQLException e) 
		{
			System.out.println(e.getMessage());
		}
	}

	private static void deleteRecordFromEmployeeTable() throws SQLException 
	{
	Connection connection = null;
	Statement statement = null;

	String deleteTableQuery = "DELETE EMPLOYEE WHERE USER_ID = 1000";

		try 
		{
			connection = getDBConnection();
			statement = connection.createStatement();

			// execute delete SQL statement
			statement.executeUpdate(deleteTableQuery);
			System.out.println("Record is deleted from EMPLOYEE table!");

		}
		catch (SQLException e) 
		{
			System.err.println(e.getMessage());

		}
		finally 
		{
			if (statement != null) 
			{
				statement.close();
			}

			if (connection != null) 
			{
				connection.close();
			}
		}
	}

	private static Connection getDBConnection() 
	{
		Connection dbConnection = null;
		try 
		{
			Class.forName(dbDriver);
		}
		catch (ClassNotFoundException e) 
		{
			System.err.println(e.getMessage());
		}

		try 
		{
		dbConnection = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
		return dbConnection;
		}
		catch (SQLException e) 
		{
			System.err.println(e.getMessage());
		}

	return dbConnection;
	}
}

 
download

Read More