Java Program Connection database common class and interface introduction _java

Source: Internet
Author: User
Tags stmt

Writing a Java program to access a database requires several important classes and interfaces.
DriverManager class

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

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


Specifies the URL user name and password of the data to create the database connection object. The syntax format for the URL 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 connections to a particular database. The connection object is an object used to represent a database connection, and the Java program's operations on the database are performed on this object. The main methods of the connection class are:

    • Statement createstatement (): Creates a Statement object.
    • Statement createstatement (int resultsettype,int resultsetconcurrency): Creates a Statement object that generates a result set of a particular type.
    • void commit (): commits a change to the database and releases the lock on the currently held database.
    • void rollback (): Rolls back all changes in the current transaction and releases the locks on the databases held by the current connection.
    • String GetCatalog (): Gets the current directory of the Connection object.
    • Boolean isclose (): Determines whether the connection is closed.
    • Boolean isreadonly (): Determines whether the connection is read-only.
    • void Setreadonly (): Sets the connection to read-only mode.
    • void Close (): Frees the database and JDBC resources for the Connection object.

Statement class

The statement class is a class that is used in java.sql packages to handle 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 the connection object that connects the database, and let the object connect to the database. The static method that invokes the class DriverManager getconnection () obtains the connection object and realizes the connection between the program and the database. The SQL statement object is then declared with the statement class, and the Createstatement () method of the Connection object is invoked to create the SQL statement object. For example, the following code creates the Statement object sql:

  Statement sql = null;
  try{
    sql = Con.createstatement ();
  } catch (SQLException e) {}


ResultSet class

With the SQL statement object, the method ExecuteQuery () that invokes the statement object executes the SQL query and stores the results of the query in an object declared with the ResultSet class, for example, the following code reads the Student score 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 dataset consisting of data rows in a uniform form, one row for a query record. A cursor is implied in the ResultSet object, only the data row currently being referred to by the cursor can be obtained at a time, and the next method is used to remove a row of data. Use the field (column) name or location index (starting from 1) of the data row to call the GetXXX () method to obtain the word Genzhi for the record. The following are some of the methods of the ResultSet object:

    • byte getbyte (int columnindex): Returns the byte value of the specified field.
    • Date getDate (int columnindex): Returns the date value of the specified field.
    • float getfloat (int columnindex): Returns the floating-point value of the specified field.
    • int getInt (int columnindex): Returns the integer value of the specified field.
    • String getString (int columnindex): Returns the string value of the specified field.
    • Double getdouble (String columnName): Returns the double value of the specified field.
    • Long Getlong (String columnName): Returns a long integer value for the specified field.
    • Boolean next (): Returns whether the next field is available.

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 through the query result set, or move around, or display the specified record of the result set, which is called a scrollable result set. To get a scrollable result set, the program increases the two parameters of the specified result set as soon as the SQL statement object is obtained. For example, the following code:

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


The SQL query that stmt the statement object can get the result set of the corresponding type.
The int parameter type determines how scrollable sets can be scrolled:

    • Resultset.type_forword_only, the cursor of 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 the current result set changes synchronously as the database changes.

The int parameter concurrency determines whether the database synchronizes with the scrollable set:

    • Resultset.concur_read_only, you cannot update a table in the database with a result set.
    • Resultset.concur_updatetable, you can update the tables in the database with the result set.

For example, the following code uses Connection object connect to create a statement object stmt, to specify that the result set scrolls and read the database read-only:

  stmt = Connect.createstatement (resultset.type_scroll_sensitive,
  resultset.concur_read_only);


Some other commonly used methods on scrollable sets are as follows:

    • Boolean previous (): Moves the cursor up and returns False when moved to the first row of the result set.
    • void Beforefirst (): Moves the cursor before the first row of the result set.
    • void Afterlast (): Moves the cursor after the last row in the result set.
    • void First (): Moves the cursor to row one.
    • void Last (): Moves the cursor to the final row.
    • Boolean isafterlast (): Determines whether the cursor is after the last row.
    • Boolean Isbeforefirst (): Determines whether the cursor precedes the first row.
    • Boolean islast (): Determines whether the cursor is on the last line.
    • Boolean Isfirst (): Determines whether the cursor is in the first row.
    • int GetRow (): Gets the currently indicated line (the line number begins with 1, the result set is empty, and returns 0).
    • Boolean absolute (int row): Moves the cursor to a row row.

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.