Java know how much (107) Several important Java database access classes and interfaces

Source: Internet
Author: User

Writing a Java program that accesses a database also requires several important classes and interfaces.

DriverManager class

The DriverManager class handles the loading of the driver and establishes a new database connection. DriverManager is the class that is used in the java.sql package to manage database drivers. Typically, an application uses only the getconnection () static method of the class DriverManager to establish a connection to the database and return the connection object:

Static Connection getconnection (String url,string username,string password)

Specifies the URL user name and password for the data to create the database connection object. The syntax format for URLs is:

jdbc:< database connection mechanism >:<ODBC database name >.

Connection class

The connection class is the class that is used in the java.sql package to handle connection to a particular database. The connection object is the object used to represent the database connection, and the Java program operates on the database on this object. The main methods of the connection class are:

    1. Statement createstatement (): Creates a Statement object.
    2. Statement createstatement (int resultsettype,int resultsetconcurrency): Creates a Statement object that produces a result set of a specific type.
    3. void commit (): commits a change to the database and frees the lock for the currently held database.
    4. void rollback (): Rolls back all changes in the current transaction and frees the lock on the database held by the current connection.
    5. String GetCatalog (): Gets the current directory of the Connection object.
    6. Boolean isclose (): Determines whether the connection is closed.
    7. Boolean isreadonly (): Determines whether the connection is read-only mode.
    8. void Setreadonly (): Sets the connection as read-only mode.
    9. void Close (): Releases the database and JDBC resources for the Connection object.
Statement class

The statement class is the class in the java.sql package that is used to process SQL statements in a specified connection. The main point of database programming is to embed SQL commands in your program. The program needs to declare and create a connection object that connects to the database and have the object connect to the database. The static method called the class DriverManager getconnection () Obtains the connection object, which implements the connection between the program and the database. Then, declare the SQL statement object with the statement class and call the Createstatement () method of the Connection object to create the SQL statement object. For example, the following code creates a statement object sql:

1     NULL ; 2     Try {3         sql = con.createstatement (); 4     } Catch (SQLException e) {}

ResultSet class

With the SQL statement object, the method that invokes the statement object ExecuteQuery () executes the SQL query and stores the query results in an object declared with the ResultSet class, for example, the following code reads the Student scores table in the RS object:
ResultSet rs = sql.executequery ("SELECT * from Ksinfo");
The ResultSet object is actually a table of query result data, a tubular data set consisting of a uniform form of data row, one row for a query record. A cursor is implied in the ResultSet object, and only the data row currently referred to by the cursor can be obtained at a time, with the next method to remove a data row. Use the field (column) name or position index of the data row (starting at 1) to call the shape as GetXXX () method to get the recorded word Genzhi. Here are some of the methods of the ResultSet object:

    1. byte getbyte (int columnindex): Returns the byte value of the specified field.
    2. Date getDate (int columnindex): Returns the date value of the specified field.
    3. float getfloat (int columnindex): Returns the floating-point value of the specified field.
    4. int getInt (int columnindex): Returns the integer value of the specified field.
    5. String getString (int columnindex): Returns the string value of the specified field.
    6. Double getdouble (String columnName): Returns the double value of the specified field.
    7. Long Getlong (String columnName): Returns the long integer value of the specified field.
    8. Boolean next (): Returns whether there is another field.

The columnindex in the above method is the positional index, which specifies the field, and ColumnName is the field name.

The user needs to browse on the query result set, or move or display the specified record of the result set, which is called a scrollable result set. The program obtains a scrollable result set, as long as two parameters of the specified result set are added when the SQL statement object is obtained. For example, the following code:

    Statement stmt = con.createstatement (type,concurrency);     = stmt.executequery (SQL statement)

The SQL query of the statement object stmt can get the result set of the corresponding type.

    • The type int parameter type determines how scrollable sets are scrolled:
      • Resultset.type_forword_only, the cursor for the result set can only scroll down.
      • Resultset.type_scroll_insensitive, cursors can move up and down, and the current result set is unchanged when the database changes.
      • ResultSet. Type_scroll_sensitive, cursors can move up and down, and when the database changes, the current result set changes synchronously.
    • The type int parameter concurrency determines whether the database is synchronized with the scrollable set:
      • Resultset.concur_read_only, the table in the database cannot be updated with the result set.
      • Resultset.concur_updatetable, the table in the database can be updated with the result set.


For example, the following code takes advantage of connection object Connect, creates a statement object stmt, specifies that the result set is scrollable, and reads the database read-only:
stmt = Connect.createstatement (resultset.type_scroll_sensitive,
RESULTSET.CONCUR_READ_ONLY);
Some other common methods on scrollable sets are as follows:

    1. Boolean previous (): Moves the cursor up and returns False when the first row of the result set is moved.
    2. void Beforefirst (): Moves the cursor before the first row of the result set.
    3. void Afterlast (): Moves the cursor after the last row of the result set.
    4. void First (): Moves the cursor to the top row.
    5. void Last (): Moves the cursor to the final row.
    6. Boolean isafterlast (): The cursor is sentenced after the last row.
    7. Boolean Isbeforefirst (): Determines whether the cursor precedes the first row.
    8. Boolean islast (): Whether the cursor is in the last row.
    9. Boolean IsFirst (): Whether the cursor is in the first row.
    10. int GetRow (): Gets the line that is currently referred to (the line number is numbered from 1, the result set is empty, and 0 is returned).
    11. Boolean absolute (int row): Moves the cursor to the row row.

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text areaJava know how much (87) Select boxes and radio buttonsJava know how many (88) lists and combo boxesJava know how many (89) lists and combo boxesJava know how much (90) menuJava know how much (91) dialog boxJava know how much (92) scroll barJava know how much (93) mouse EventsJava know how much (94) keyboard EventsJava know how much (95) Drawing BasicsJava know how much (96) set the font and color of the drawingJava know how much (97) Drawing Mode OverviewHow much Java knows (98) the drawing method of the Graphics classJava know how much (graphics2d) the drawing method of the classJava know how much (100) Image processing BasicsJava know how much (101) image buffering TechnologyJava know how much (102) Multimedia FoundationJava know how much (103) network programming IP address and InetAddress classJava know how much (104) the Uniform Resource Locator URL for network programmingJava know how much (105) socket (socket)Java know how much (106) program and database connection

Java know how much (107) Several important Java database access classes and interfaces

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.