How to use Oracle Database in Java Program?
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.
- - Oracle Database installed with proper privileges.
- - Firewall allowing Oracle service to connect it.
- - Download database driver. (http://www.javaquery.com/2011/07/free-java-api-list.html)
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 :