Sunday, August 11, 2013

Oracle: JDBC connectivity


First of all make sure you have proper JDK installation and java home path is set. In order to confirm that JDK configured correctly go to command prompt and type "javac". It should prompt you various javac usage options.
Now make sure that you have correct jdbc drivers. For this go to oracle website and download correct driver if not done already. Now append the location of jdbc drivers into your classpath. For example if I execute "echo %classpath%" into my system it will show me something like this:

D:\ankur\KB\java\JDBC\program>echo %classpath%
.;D:\ankur\KB\java\JDBC;D:\app\achourasia\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar;

You can set classpath into following location on windows machine:

My Compute --> Properties --> Advanced System Settings --> Environment Variables --> User variables --> edit classpath.

In order to test that everything is fine run following small test.

Execute following sql into SCOTT schema:

CREATE TABLE rectangles(
       seq INT,
  length INT, 
  width  INT,
  area NUMBER(19,5)
);

INSERT INTO rectangles (seq, length, width) VALUES (1, 10, 34);
                                                                            
INSERT INTO rectangles (seq, length, width) VALUES (2, 100, 89);

INSERT INTO rectangles (seq, length, width) VALUES (3, 20, 76);

INSERT INTO rectangles (seq, length, width) VALUES (4, 35, 14);

INSERT INTO rectangles (seq, length, width) VALUES (5, 1, 9);

Now execute following java from file system:

import java.sql.*;
public class JdbcRectangle {

public int calculateArea(int length, int width){
return (length * width);
}

public static void main(String args[]) throws SQLException {

try {
String username = "SCOTT";
String password = "SCOTT";
String database = "pune705";
JdbcRectangle j = new JdbcRectangle();

//Create a connection.
Connection conn1 = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:" + database, username, password);;
//stmt for select query
Statement stmt = conn1.createStatement();
//stmt for update query.
Statement stmt1 = conn1.createStatement();

ResultSet rs = stmt.executeQuery("SELECT seq, length, width FROM rectangles where area is null");

boolean b = false;
int seq  = 0;
int len  = 0;
int wdt  = 0;
int area = 0;
int i    = 0;

while(rs.next()){
seq = rs.getInt("seq");
len = rs.getInt("length");
wdt = rs.getInt("width");
area = j.calculateArea(len,wdt);

b = stmt1.execute("UPDATE rectangles SET area = " + area + " WHERE seq = " + seq);

System.out.println("SEQ: " + (++i) + " Length: " + len + " Width " + wdt + " Area: " + area + " is_updated: " + String.valueOf(b));
}

rs.close();
stmt.close();
stmt1.close();
conn1.close();

}

catch(SQLException e) {
e.printStackTrace();
}
}



If everything allright it will show following message into command prompt and reflect changes into database:

D:\ankur\KB\java\JDBC\program>java JdbcRectangle
SEQ: 1 Length: 10 Width 34 Area: 340 is_updated: false
SEQ: 2 Length: 100 Width 89 Area: 8900 is_updated: false
SEQ: 3 Length: 20 Width 76 Area: 1520 is_updated: false
SEQ: 4 Length: 35 Width 14 Area: 490 is_updated: false
SEQ: 5 Length: 1 Width 9 Area: 9 is_updated: false

No comments: