How to manipulate a database using JDBC

Source: Internet
Author: User

Common steps for data database connection and query1. What are the steps to connect to the database? What classes are involved? Under what packages? Database Connection is divided into two steps: Step 1: Class. forname; [ Load driver]The class belongs to the java. lang package, and the forname method returns a class object. The purpose is to load the driver. Step 2: Drivermanager. getconnection(Constring, user, password); where drivermanager belongs to Java. SQL. *; The getconnection method returns a connection object. Why is the driver loaded? Because when the drivermanager. getconnection ("url", "user", "password") method is called, drivermanager queries the loaded drive and asks whether it understands the URL. If a drive can recognize this URL, it returns a connection object, that is, the connection object. 2. How can I query a database after the database is connected? Which classes and methods are involved? Step 1: Create a statement object (connect objects by using con, for example, to connect objects) [ Create a statement object by connecting to the object]: Statement stmt = con. createstatement (); Step 2: return the result set by calling the executequery (sqlstring) method through the statement object (for example, using stmt to represent the statement object) [Execute the statement objectSQLQuery statement]Resultset rs = stmt.exe cutequery (sqlstring); where statement and resultset belong to the java. SQL. * package. 3. How to obtain the query result set? Which classes and methods are involved? Step 1: Call the executequery (sqlstring) method through the statement object (for example, using stmt to represent the statement object) to obtain the result set object (for example, using the RS variable ): [ Run SQL Query to obtain the result set object]Resultset rs = stmt.exe cutequery (sqlstring); Step 2: Call the getmetadata () method of the result set object to obtain the field structure of the result set, that is, the data column of the table, it is called a result set metadata object: [ Obtain the meta data object of the result set through the result set object]Resultsetmetadata rsmd = Rs. getmetadata (); 4. How to obtain the table structure of the database? Step 1: Obtain it by calling the getmetadata () method of the result set object Result set metadata object rsmd ):(Use resultsetmetadata rsmd = rs1.getmetadata (); Step 2: Call the getcolumncount () method of the metadata object to obtain Total number of Columns: Int columnnum = rsmd. getcolumncount (); Step 3: Use the for loop to traverse each field in the table structure and call the getcolumnname (I) method of the metadata object to obtain each column. The column number starts from 1 instead of 0. For (INT I = 1; I <= columnnum; I ++) system. out. print (rsmd. getcolumnname (I) + "/t"); 5. how to traverse the records of database tables? Step 1: first obtain the result set object Step 2: Move the pointer (cursor) by calling the next () method in the result set. If Position indicated by pointerIf a record exists, the return value of the method is true and enters the loop. While (RS. next () {string strdata = ""; for (INT I = 1; I <= columnnum; I ++) strdata = strdata + Rs. getstring (I) + "/t";} finally, it is summarized as follows: in Java. SQL. * The package contains several important class1. drivermangager2. connection3. statement4. resultset5. resultsetmetadata

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.