//
you're reading...
JDBC

JDBC Code with Oracle – Delete

import java.sql.*;

public class DeleteTest02 {

	public static void main(String[] args) {
		if (args.length != 1) {
			throw new IllegalArgumentException("Invalided Parameter:" +
		"java DeleteTest02 job");
		}

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
	
		try {
			//new OracleDriver();
			Class.forName("oracle.jdbc.driver.OracleDriver");
	
			String dbUrl = "jdbc:oracle:thin:@192.168.56.30:1521:TESTDB";
			String username = "testdbuser";
			String password = "testdbpwd";
			conn = DriverManager.getConnection(dbUrl, username, password);
	
			int empno = Integer.parseInt(args[0]);
	
			//PreparedStatement,sql
			
			String sql = "delete from emp where empno=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, empno);
			pstmt.executeUpdate();
			System.out.println("Employee deleted succesfully,Employee Number:("  + empno + ")");
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}catch(SQLException e) {
			e.printStackTrace();
		}finally {

			try {
				if (rs != null) {
					rs.close();
				}
				if (pstmt != null) {
					pstmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			}catch(SQLException e){}
		}
	}
}
Advertisements

About daviewning

I am an Oracle DBA

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: