How to use Oracle Database in Java Program?

UPDATED: 20 October 2010
Oracle

Access Oracle Database using the Java program is very easy task. All you need is basic database knowledge. Before you try to connect let me give you quick list that you need for your program.


try {
  //Driver to Connect with Database
  Class.forName("oracle.jdbc.OracleDriver"); 
} catch (Exception ex) {
  ex.printStackTrace();
}
try {
  String DBlink = "jdbc:oracle:thin:@vicky-pc:1521:test"; 
  //vicky-pc: Hostname / 1521: port / test: Databasename
  Connection con = DriverManager.getConnection(DBlink, "username", "password");
  Statement smt = con.createStatement();
  ResultSet rs = smt.executeQuery("select * from tab");
  // rs contains all return values for your query
  while(rs.next()){
   String data = rs.getString("Column_Name");
  }
  con.close();
} catch (Exception e) {
  e.printStackTrace();
}

This is simple way to connect to database. Change your connection and table credential in above program.

You might wanna check best practice for database operation(s) in following article: PreparedStatement : Best practice of Database in Java

0 comments :