Java Basics JDBC One: overview and steps

Source: Internet
Author: User

1.        Introduction to JDBC: the class Library (technology) that Java uses to manipulate different databases (DBMS) is essentially classes and interfaces. /*class: DriverManager interface: Driver, Connection, Statement, PreparedStatement, ResultSet*/                //Java Data Base Connectivity, Java Database Connection (technology).                /*Collections: The containers that Java uses to store different types of data, essentially classes and interfaces. Interface: Collection, Set, List, Map class: ArrayList, HashSet, HashMap io stream: Is the technology that Java uses to transmit data,                The essence is some classes and interfaces. Abstract class: InputStream, OutputStream, Reader, Writer implementation class: FileInputStream, Bufferedinputstream        , FileReader, BufferedReader Summary: The top layer of the collection is an interface, and the top layer of the IO stream is an abstract class. */Core Features: A: Connect to the database.        B: Send SQL statements to the database.            C: The result set returned by the operations database. If you are working with an UPDATE statement (incremental deletion), the result set returned is:inttype If the operation is a query statement, the result set returned is: ResultSet type2. What is a driver?is the bridge between the devices to communicate. 3. What is the principle of JDBC: we know that JDBC is used to manipulate different databases, but different databases need to be driven differently. For example: we want to operate the MySQL database, we need to install the MySQL driver, we want to operate the Oracle database, It would be cumbersome to install the driver for the Oracle database, and if we were to operate the SQL Server database, we would need to install the SQL Server database driver.     Because Java already offers a large number of classes and interfaces, it requires additional memory for other classes and interfaces, which increases the difficulty of the programmer's learning. When Sun discovered the problem, it decided, in consultation with major database manufacturers, that Sun would provide a uniform specification (that is, some classes and interfaces), and that the database manufacturer would provide specific implementations.    These classes and interfaces provided by Sun are: JDBC. 4. Procedures for JDBC
1) import driver. 2) Registration driver. 3) Gets the connection object. 4gets the object that can manipulate the SQL statement, based on the connection object. 5executes the SQL statement to get the result set. If you are working with an UPDATE statement (incremental deletion), the result set returned is:inttype If the operation is a query statement, the result set returned is: ResultSet type6) operation result set. 7) to release resources. 5. The JDBC API is detailed in the following: DriverManager (Class) role: Mainly used to drive management and obtain connection objects. Function One: Registration drive. //Mysql/oracle/sqlserver's Drive Public Static voidRegisterdriver (Driver Driver);//The registration driver means that the formal parameter is the driver of who is registered. //the driver class is the drive class provided by the MySQL database. Public classCom.mysql.jdbc.DriverImplementsjava.sql.driver{}//We do not register the driver in the way described above, because the registration-driven method above causes the driver to register two times. //so: We can automatically register the driver by loading the Com.mysql.jdbc.Driver class's bytecode file in a reflection manner.Class.forName ("Com.mysql.jdbc.Driver");//full path of the driver classfunction Two: Gets the connection object. Public StaticConnection getconnection (string URL, string username, string password);//Gets the connection object. /*URL: Database connection string. Format: Connection: dbms://the IP address or hostname of the database to be manipulated: port number/Specific database protocol to operate: sub-protocol: The IP address of the database to be manipulated dbms://or Host Name: port number/specific database to operate for example: jdbc:mysql://127.0.0.1:3306/tempdb jdbc:mysql://localhost:3306/tempdb If you are working on a local database (DBMS), the above notation can be optimized to: JDBC:MYSQL:///TEMPDB Username: Account of the Database (DBMS) to be manipulated password: Database (DBMS) for the*/6. The JDBC API is detailed: Connection (interface) Connection: Connection object action One: Gets the object that can execute the SQL statement. PublicStatement createstatement ();//gets the object that can execute the SQL statement. PublicPreparedStatement preparestatement (String sql);//gets the object that can execute the SQL statement, with the precompilation feature, which resolves the SQL injection attack problem.role two: Transaction management can be carried out. Public voidSetautocommit (BooleanFlag);//set whether to turn on the auto-commit function of the transaction, as we explained yesterday: Turn on the transaction. Public voidCommit ();//Commit a transaction Public voidRollback ();//Transaction Rollback Public voidSettransactionisolation (intlevel);//to set the isolation level of a transactionconstants in the connection interface: Public Static Final intTransaction_none = 0; Public Static Final inttransaction_read_uncommitted = 1; Public Static Final inttransaction_read_committed = 2; Public Static Final intTransaction_repeatable_read = 4; Public Static Final intTransaction_serializable = 8; 7. The JDBC API is detailed: Statement (interface) Statement: An object that can manipulate SQL statements. Action One: Executes the SQL statement. PublicResultSet executeQuery (String sql);//Execute Query Statement Public intExecuteupdate (String sql);//Execute UPDATE statementrole two: Batch processing can be performed. //batches are valid only for UPDATE statements. Public voidAddbatch (String sql);//add the SQL statement to the batch (Directive). Public int[] ExecuteBatch ();//Execute Batch (directive) Public voidClearbatch ();//clear the batch (instruction)8. The JDBC API is detailed: ResultSet (interface) ResultSet: The result set object after executing the query statement. Public BooleanNext ();//determine if there is data in the result set. Similar to Iterator#hasnext (). PublicXXX GetXxx (intCOLUMNINDEX);//Gets the column's information according to the column number, XXX is the meaning of the data type PublicXXX getXxx (String columnName);//According to the name of the column, get information about the column, XXX is the data type meaning, recommended in this way. //getInt (), getString (), GetObject ();

Java Basics JDBC One: overview and steps

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.