Connecting to Oracle using Java JDBC


/ Published in: Java
Save to your folder(s)

Connecting to Oracle using Java JDBC


Copy this code and paste it in your HTML
  1. public class OracleJdbcTest
  2. {
  3. String driverClass = "oracle.jdbc.driver.OracleDriver";
  4.  
  5.  
  6. {
  7. Properties props = new Properties();
  8. props.load(fs);
  9. String url = props.getProperty("db.url");
  10. String userName = props.getProperty("db.user");
  11. String password = props.getProperty("db.password");
  12. Class.forName(driverClass);
  13.  
  14. con=DriverManager.getConnection(url, userName, password);
  15. }
  16.  
  17. public void fetch() throws SQLException, IOException
  18. {
  19. PreparedStatement ps = con.prepareStatement("select SYSDATE from dual");
  20. ResultSet rs = ps.executeQuery();
  21.  
  22. while (rs.next())
  23. {
  24. // do the thing you do
  25. }
  26. rs.close();
  27. ps.close();
  28. }
  29.  
  30. public static void main(String[] args)
  31. {
  32. OracleJdbcTest test = new OracleJdbcTest();
  33. test.init();
  34. test.fetch();
  35. }
  36. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.