MySQL Database and JDBC Learning

Source: Internet
Author: User

Connection con = null;

What is null in Java?

----Excerpt from http://www.cnblogs.com/X-World/p/5686122.html

1) First, null is a keyword , like public, static, final.

2) Just as each base type has a default value, such as the default value of 0,boolean for int defaults to FALSE,NULL is the default value for any reference type , not strictly the default value for all object types. just as you create a Boolean type variable, it will be false as its default value, and any reference variable in Java will be null as the default value. This applies to all variables, such as member variables, local variables, instance variables, and static variables (but when you use a local variable that is not initialized, the compiler warns you). To prove this, you can observe the reference variable by creating a variable and then printing its value.

3)Null is neither an object nor a type, it is only a special value that you can assign to any reference type, or you can convert null to any type.

4)null can be assigned to a reference variable, and you cannot assign null to a primitive type variable , such as int, double, float, Boolean. The compiler will make an error.

Common interfaces:

-----from Bowen Http://www.cnblogs.com/erbing/p/5805727.html

1. Registration driver (only once)

Way One : Class.forName ("com. MySQL.jdbc.Driver ");
It is recommended that this approach does not rely on specific driver classes.
mode two : Drivermanager.registerdriver (Com.mysql.jdbc.Driver);
Will cause two of the same drivers in the DriverManager, and will be dependent on the specific driver class.

Note:

JDBC4.0 and subsequent versions support automatic driver discovery, which does not require the user to preload the database driver. To ensure the class of the program database driver, the location of the class must be included in the program's CLASSPATH when executing the program.

2.Connection interface

Connection the connection (session) to a particular database, executes the SQL statement in the connection context, and returns the result. The Drivermanager.getconnection (URL, user, password) method establishes the database connection connection defined in the JDBC URL.

Connect MySQL database: Connection conn = drivermanager.getconnection ("Jdbc:mysql://host:port/database", "User", "password");

Connect to Oracle database: Connection conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @host:p ort:database", "User", "Password ");

Connect SQL Server database: Connection conn = Drivermanager.getconnection ("Jdbc:microsoft:sqlserver://host:port; Databasename=database "," User "," password ");

Common methods:

    • Createstatement (): Creates a statement object that sends SQL to the database.
    • Preparestatement (SQL): Creates a Preparesatement object that sends precompiled SQL to the database.
    • Preparecall (SQL): Creates the CallableStatement object that executes the stored procedure.
    • Setautocommit (Boolean autocommit): Sets whether the transaction is automatically committed.
    • Commit (): commits the transaction on the link.
  • Rollback (): Rolls back the transaction on this link.

    3.Statement interface

    The object used to execute a static SQL statement and return the result it produces.

    Three types of statement:

        • Statement: Created by createstatement to send a simple SQL statement (without parameters).
        • PreparedStatement: Inherited from the statement interface, created by PreparedStatement, for sending SQL statements that contain one or more parameters. PreparedStatement objects are more efficient than statement objects and can prevent SQL injection, so we generally use preparedstatement.
        • CallableStatement: Inherits from the PreparedStatement interface, created by method Preparecall, to invoke the stored procedure.

    Common statement methods:

        • Execute (String SQL): Runs the statement that returns whether there is a result set
        • ExecuteQuery (String SQL): Runs the SELECT statement, returning the resultset result set.
        • Executeupdate (String SQL): Runs the insert/update/delete operation, returning the number of updated rows.
        • Addbatch (String sql): puts multiple SQL statements into one batch.
        • ExecuteBatch (): Sends a batch of SQL statement execution to the database.

    4.ResultSet interface

    ResultSet provides methods for retrieving different types of fields, which are commonly used:

        • getString (int index), getString (String columnName): Gets a data object of type varchar, char, and so on in the database.
        • GetFloat (int index), getfloat (String columnName): Gets a data object of type float in the database.
        • GetDate (int index), getDate (String columnName): Gets the data of the date type in the database.
        • Getboolean (int index), Getboolean (String columnName): Gets a Boolean type of data in the database.
        • GetObject (int index), getObject (String columnName): Gets any type of data in the database.

    ResultSet also provides a way to scroll the result set:

        • Next (): Move to the next line
        • Previous (): Move to previous line
        • Absolute (int row): Move to the specified line
        • Beforefirst (): Moves the front of the resultset.
        • Afterlast (): Moves to the last face of the resultset.

    Close objects and connections in turn after use: resultset→statement→connection

  • Using the JDBC Step
  • (1) Establish a connection (Connection)-----------Create statement------------> Process Execution Results (ResultSet)-----------> Free resources for executing SQL statements

MySQL Database and JDBC Learning

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.