Saturday, November 21, 2009

ADF : Insert the Row into Table.

INSERT

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleDriver;
public class TestClass {
public TestClass() {
}
Get the Connection
----------------------
public static Connection getConnection() throws SQLException {
String username = "hr";
String password = "hr";
String thinConn = "jdbc:oracle:thin:@10.18.2.49:1521:XE";
DriverManager.registerDriver(new OracleDriver());
Connection conn =
DriverManager.getConnection(thinConn, username, password);
conn.setAutoCommit(true);
return conn;
}

CODE
-------

public String insert() throws SQLException {
// Add event code here...
System.out.println("Inside insert::");
Connection conn= null;
PreparedStatement ps=null;
ResultSet rs= null;
String query ="INSERT INTO DEPARTMENTS(DEPARTMENT_ID,DEPARTMENT_NAME) values(?,?)";
System.out.println("Query:::>> "+query);
conn=getConnection();
ps=conn.prepareStatement(query);
System.out.println("Inserting Values::");
ps.setString(1,"999");
ps.setString(2, "CID");
ps.executeUpdate();
ps.close();
System.out.println("Exiting::");
return "success";
}
}