Guest User

Untitled

a guest
Dec 14th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.Properties;
  7.  
  8. /**
  9. * Simple Java Program to connect Oracle database by using Oracle JDBC thin driver
  10. * Make sure you have Oracle JDBC thin driver in your classpath before running this program
  11. * @author
  12. */
  13. public class OracleJdbcExample {
  14.  
  15. public static void main(String args[]) throws SQLException, ClassNotFoundException {
  16. //URL of Oracle database server
  17. String url = "jdbc:oracle:thin:@localhost:1632:DEVROOT32";
  18.  
  19. //properties for creating connection to Oracle database
  20. Properties props = new Properties();
  21. props.setProperty("user", "scott");
  22. props.setProperty("password", "tiger");
  23.  
  24. Class.forName("oracle.jdbc.driver.OracleDriver");
  25.  
  26. //creating connection to Oracle database using JDBC
  27. Connection conn = DriverManager.getConnection(url,props);
  28.  
  29. String sql ="select sysdate as current_day from dual";
  30.  
  31. //creating PreparedStatement object to execute query
  32. PreparedStatement preStatement = conn.prepareStatement(sql);
  33.  
  34. ResultSet result = preStatement.executeQuery();
  35.  
  36. while(result.next()){
  37. System.out.println("Current Date from Oracle : " + result.getString("current_day"));
  38. }
  39. System.out.println("done");
  40.  
  41. }
  42. }
  43.  
  44. Output:
  45. Current Date from Oracle : 2012-04-12 17:13:49
  46. done
Add Comment
Please, Sign In to add comment