Familiar with Java. SQL packages

Source: Internet
Author: User

 

Today, I looked at JDBC, java. SQL package, and sqljdbc. jar of sqlserver2005. In general, I understand that JDBC is a set of database implementation standards released by Sun. It only provides interfaces, not specific implementations. The specific implementation process is determined by the database development vendors. Each database development vendor must provide a. jar package to implement the preceding interfaces.

In this way, if you change the database one day (such as converting from sqlserver to Oracle database), you only need to change the database driver in the Java program, and change the sqljdbc. jar changed to Oracle. jar package. Of course, if Oracle does not recognize other functions, you can change the function. You do not need to modify the Java code. Java code does not use classes implemented by specific database developers, but uses interfaces defined in Java. SQL. It's really convenient.

 

Main classes and interfaces in the Java. SQL package include:

1 connection interface, defined as: public interface connection

2 driver interface, defined as: public interface driver

3 drivermanager class, defined as: public class drivermanager

4. preparedstatement interface, defined as: public interface preparedstatement extends statement

5. The resultset interface is defined as public interface resultset.

6 sqlexception class, defined as: public class sqlexception extends java. Lang. Exception

7. Statement interface, defined as: public interface statement

 

The JDBC mode is as follows:

1. Load the database driver

2. Connect data

3. Execute the SQL statement and return the execution status and query results.

4. Disable database connection

 

Convert to code:

  1. Import java. SQL .*;
  2. Public class testdbconnection {
  3. Public static void main (string [] ARGs ){
  4. Try {
  5. // Define a connection
  6. Connection con;
  7. // Load the JDBC driver implemented by database developers
  8. // MySQL scenario: COM. MySQL. JDBC. Driver
  9. Class. forname ("com. MySQL. JDBC. Driver ");
  10. // Connect to the database (different database developers)
  11. // Either of the following write methods is acceptable (For MySQL)
  12. Con = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/mydb", "root", "sa ");
    // Con = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/mydb? User = root & Password = sa ");

  13. // Create a statement object or preparedstatement object
  14. // Statement: a general statement object
  15. // Preparedstatement: Specifies the statement object used for dynamic SQL statements.
  16. Statement ST = con. createstatement ();
  17. String SQL = "select * From Table1 order by ID ";
  18. // Define the resultset object
  19. // Execute SQL operations (query, update)
  20. // Executequery: Query
  21. // Executeupdate: Update
  22. // Execute: generally not used
  23. Resultset rs = st.exe cutequery (SQL );
  24. While (Rs. Next ()){
  25. // Retrieve data based on the column Index
  26. System. Out. println (Rs. getstring (1 ));
  27. // Obtain data based on the column name
  28. System. Out. println (Rs. getstring ("columnname "));
  29. }
  30. // Close the resource
  31. If (RS! = NULL) Rs. Close ();
  32. If (st! = NULL) ST. Close ();
  33. If (con! = NULL) con. Close ();
  34. } Catch (exception e ){
  35. E. printstacktrace ();
  36. }
  37. }
  38. }

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.