Two basic methods for connecting to Oracle databases

Source: Internet
Author: User

The following articles describe how to correctly connect to Oracle databases. If you are a beginner in Oracle database application, you can use the following articles to have a better understanding of the two basic methods for connecting to the Oracle database, which are described in detail in this article.

1. the following code uses the jdbc_odbc bridge to connect to Oracle:

 
  1. Public class Test2 {
  2. Public static void main (String [] args ){
  3. String driver = "sun. jdbc. odbc. JdbcOdbcDriver ";
  4. String url = "jdbc: odbc: test1 ";
  5. String uname = "scott ";
  6. String password = "tiger ";
  7. Connection conn = null;
  8. String SQL = "select * from emp ";
  9. Try {
  10. Class. forName (driver );
  11. Conn = DriverManager. getConnection (url, uname, password );
  12. Statement sm = conn. createStatement ();
  13. ResultSet rs = sm.exe cuteQuery (SQL );
  14. While (rs. next ()){
  15. String sname = rs. getString (2 );
  16. System. out. println ("***** name =" + sname );
  17. }
  18. } Catch (Exception e ){
  19. E. printStackTrace ();
  20. }
  21. }
  22. **************************************** ***********************************

2. The code for connecting to the Oracle database using jdbc is as follows:

 
  1. Public class Test3 {
  2. Public static void main (String [] args ){
  3. String driver = "Oracle. jdbc. driver. OracleDriver ";
  4. String url = "jdbc: Oracle: thin :@ localhost: 1521: orcl ";
  5. String uname = "scott ";
  6. String password = "tiger ";
  7. Connection conn = null;
  8. String SQL = "select * from emp ";
  9. Try {
  10. Class. forName (driver );
  11. Conn = DriverManager. getConnection (url, uname, password );
  12. PreparedStatement ptmt = conn. prepareStatement (SQL );
  13. ResultSet rs1_ptmt.exe cuteQuery ();
  14. While (rs. next ()){
  15. System. out. println ("$ name $ =" + rs. getString (2 ));
  16. }
  17. Conn. close ();
  18. } Catch (Exception e ){
  19. E. printStackTrace ();
  20. }
  21. }
  22. ========================================================== ========================

What is the difference between PreparedStatement and Statement? It is best to use PreparedStatement when understanding Oracle databases because PreparedStatement has the role of precompilation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.