import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyAutoGeneratedKeys {
public static void main(String a[]){
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.
getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
,"user","password");
String query = "insert into emps (name, dept, salary) values (?,?,?)";
pstmt = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, "John");
pstmt.setString(2, "Acc Dept");
pstmt.setInt(3, 10000);
pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
if(rs != null && rs.next()){
System.out.println("Generated Emp Id: "+rs.getInt(1));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try{
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch(Exception ex){}
}
}
No comments:
Post a Comment