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:
- Import java. SQL .*;
- Public class testdbconnection {
- Public static void main (string [] ARGs ){
- Try {
- // Define a connection
- Connection con;
- // Load the JDBC driver implemented by database developers
- // MySQL scenario: COM. MySQL. JDBC. Driver
- Class. forname ("com. MySQL. JDBC. Driver ");
- // Connect to the database (different database developers)
- // Either of the following write methods is acceptable (For MySQL)
- Con = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/mydb", "root", "sa ");
// Con = drivermanager. getconnection ("JDBC: mysql: // localhost: 3306/mydb? User = root & Password = sa ");
- // Create a statement object or preparedstatement object
- // Statement: a general statement object
- // Preparedstatement: Specifies the statement object used for dynamic SQL statements.
- Statement ST = con. createstatement ();
- String SQL = "select * From Table1 order by ID ";
- // Define the resultset object
- // Execute SQL operations (query, update)
- // Executequery: Query
- // Executeupdate: Update
- // Execute: generally not used
- Resultset rs = st.exe cutequery (SQL );
- While (Rs. Next ()){
- // Retrieve data based on the column Index
- System. Out. println (Rs. getstring (1 ));
- // Obtain data based on the column name
- System. Out. println (Rs. getstring ("columnname "));
- }
- // Close the resource
- If (RS! = NULL) Rs. Close ();
- If (st! = NULL) ST. Close ();
- If (con! = NULL) con. Close ();
- } Catch (exception e ){
- E. printstacktrace ();
- }
- }
- }