Saturday, May 12, 2012

Connection to Oracle DB.

Connection to Oracle DB

တစ္ခါတစ္ေလ အင္တာဗ်ဴးမွာ DB တစ္ခုခုကို connect လုပ္ပါဆုိျပီးေရးခုိင္းတတ္ပါတယ္။ IDE မပါရင္ စိတ္ညစ္စရာပါ။ အျပင္မွာလည္း တစ္ခါမွ မေရးၾကပါဘူး။ ေရးျပီးသားတစ္ေနရာရာက ကူးလုိက္တာမ်ားပါတယ္။ ဘယ္လုိလုပ္သလဲဆိုတာသိရင္ ကူးျပီးျပင္လုိက္လုိ႕ရပါတယ္။  ဒါေပမယ္႔ အင္တာဗ်ဴးဆိုတာကေတာ႔ ကိုယ္က လိုခ်င္လုိ႕သြားရတယ္ဆုိေတာ႔ ေမးတာေလးေတာ႔ ေျဖရေသးတာပဲမဟုတ္လား။

package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBConnection {

public static void getDBConnection(){

String url="odbc:oracle:thin:@10.22.3.61:1521:dev";
String userName="usr";
String dBPwd="pwd";

Connection con=null;
try{
Class.forName("oracle.jdbc.OracleDriver");
con=DriverManager.getConnection(url, userName, dBPwd);
System.out.println("Connection is created");
Statement stmt = con.createStatement();
ResultSet rset = stmt.executeQuery("select ffscode,name,nvl(nric,foreign_nric)tstnric,status from t_mcs_endor where status='P'  ");
while (rset.next()){
System.out.print(rset.getString("ffscode"));
System.out.print("|");
System.out.print(rset.getString("name"));
System.out.print("|");
System.out.print(rset.getString("tstnric"));
System.out.print("|");
System.out.println(rset.getString("status"));
}

con.close();
System.out.println("Connection is closed");
}catch(ClassNotFoundException e){
System.out.println("Class Nof Found, Connection is Failed");
}catch(Exception exp){
System.out.println("Connection Error");
exp.printStackTrace();
}

}

public static void main(String[] args){
//myBeautifulDiamond(-5);
getDBConnection();
}
}


No comments: