//
you're reading...
JDBC

JDBC Code with Oracle – update

import java.sql.*;

//Using PreparedStatement to modify the data
public class UpdateTest02 {

	public static void main(String[] args) {
		if (args.length != 2) {
			throw new IllegalArgumentException("Invalided Parameter:" +
		   "java UpdateTest02 job");
		}
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			//First Step:load database driver
			//new OracleDriver();
			Class.forName("oracle.jdbc.driver.OracleDriver");

			//Second Step: get database connection
			String dbUrl = "jdbc:oracle:thin:@192.168.1.10:1521:TESTDB";
			String username = "tester";
			String password = "tester";
			conn = DriverManager.getConnection(dbUrl, username, password);

			//input parameters
			int empno = Integer.parseInt(args[0]);
			String ename = args[1];

			//Third parameter PreparedStatement, execute the sql
			//placeholder
			String sql = "update emp set ename=? where empno=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, ename);
			pstmt.setInt(2, empno);
			pstmt.executeUpdate();
			System.out.println("The employee name "  + 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: