Use JDBC to connect to the database in Java and precautions

Source: Internet
Author: User

The following uses JDBC to connect to Oracle 10 Gb as an example. Java can connect to the database through JDBC. Download the JDBC jar package. Details are as follows:

AD:

    The following uses JDBC to connect to Oracle 10 Gb as an example.

    Java can connect to the database through JDBC. Download the JDBC jar package.

    Official Website:

    Http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html

    You can download the ojdbc14.jar version. (Jdk1.4, jdk1.5, and jdk1.6 are supported)

    This version is: (you may need to log on to the Oracle website)

    Http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html

    The instance code is as follows:

     
     
    1.  
    2. Import java. SQL. connection;
    3. Import java. SQL. preparedstatement;
    4. Import java. SQL. resultset;
    5. Import java. util. arraylist;
    6. Import java. util. List;
    7.  
    8. Import javax. SQL. pooledconnection;
    9.  
    10. Import oracle. JDBC. Pool. oracleconnectionpooldatasource;
    11.  
    12. Public class jdbctest {
    13. Private string url = NULL;
    14.  
    15. /*
    16. * Connecting to the Oracle database
    17. */
    18. Public jdbctest (string shostname, string sportnumber, string SSID ){
    19. Url = "JDBC: oracle: thin: @" + shostname + ":" + sportnumber + ":" + SSID;
    20. // If you also use jdk1.6
    21. // URL = "JDBC: oracle: thin: @" + shostname + ":" + sportnumber + "/" + SSID;
    22. }
    23.  
    24. Public list <string> getlist (string susrname, string spassword, string SQL ){
    25. List <string> resultlist = new arraylist <string> ();
    26. Try {
    27. Oracleconnectionpooldatasource ocpds = new oracleconnectionpooldatasource ();
    28. String url1 = system. getproperty ("jdbc_url ");
    29. If (url1! = NULL)
    30. Url = url1;
    31. Ocpds. seturl (URL );
    32. Ocpds. setuser (susrname );
    33. Ocpds. setpassword (spassword );
    34. Pooledconnection Pc = ocpps. getpooledconnection ();
    35. Connection conn = pc. getconnection ();
    36. Preparedstatement pstmt = conn. preparestatement (SQL );
    37. Resultset rset = pstmt.exe cutequery ();
    38. While (rset. Next ()){
    39. Resultlist. Add (rset. getstring (1 ));
    40. }
    41. Rset. Close ();
    42. Pstmt. Close ();
    43. Conn. Close ();
    44. PC. Close ();
    45. } Catch (exception e ){
    46.  
    47. }
    48. Return resultlist;
    49. }
    50.  
    51. /**
    52. * Test the connection.
    53. * @ Param ARGs
    54. */
    55. Public static void main (string [] ARGs ){
    56. // Use you real info
    57. String susrname = "";
    58. String spassword = "";
    59. String SQL = "";
    60.  
    61. Jdbctest = new jdbctest ("localhost", "1521", "orcl ");
    62. List <string> List = jdbctest. getlist (susrname, spassword, SQL );
    63. System. Out. println (list. Size ());
    64. }
    65.  
    66. }

    Note the following:

    Url = "JDBC: oracle: thin: @" + shostname + ":" + sportnumber + "/" + SSID;

    In jdk1.6, you can use the above URL. That is, the port and Sid can be separated. In JDK 1.5, ":" is the only option ":".

    In addition to using the connection pool, you can also use the traditional connection pool.

     
     
    1. Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();  
    2. Connection conn = DriverManager.getConnection(url, suUserName,passWord); 

    The weather is hot. Please eat watermelon.

    Related Article

    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.