Basic steps for JDBC Applications __java development

Source: Internet
Author: User
basic steps for JDBC Applications

Basic steps for the JDBC application:

1. Registration Driver

2. Establishing a database connection

3. Create a Database Action object

4. Execute SQL

5. Processing result Sets

6. Close the JDBC Object

--------------------------------------------------------------------------------------------------------------- ----------

1. Register a DRIVER driver

Three ways to register a driver:

Mode one: Class.forName ("Oracle.jdbc.driver.OracleDriver");

Mode two: Driver driver=new Oracle.jdbc.driver.OracleDriver ();

Drivermanager.registerdriver (driver);

Mode three: Load the driver in the virtual machine at compile time

javac-d jdbc.drivers = Oracle.jabc.driver.OracleDriver Xxx.java

java-d jabc.drivers = driver Full Name Class name

Load Drive-D for system property assignment using system property name

Attached: Driver full name of MySQL: com.mysql.jdbc.Driver

SQL Server's driver full name: Com.microsoft.jdbc.sqlserver.SQLServerDriver

2. Establish a connection

Conn=drivermanager.getconnection

("Jdbc:oracle.thin:@192.168.0.254:1521/test", "username", "password");

The above brackets are: Oracle Self-Protocol database instance name, database user name, user password.

MySQL URL: jabc:mysql/192.168.254:3306/test

3. Get a Statement object

STA = Conn.createstatement ();

4. Execute SQL statements via statement

Sta.excutequery (SQL);//Returns a query result set

Sta.executeupdate (SQL);//The return value is int, representing the number of bars that affect the record

The SQL statement is sent to the database through a connection to perform operations on the database.

5. Processing result Sets

Use the Connection object to obtain a executequery (String sql) method in a stagement,statement that can be queried using a SELECT statement and return a result set ResultSet. By traversing this result set, you get the query results of the SELECT statement. The next () method of the ResultSet will manipulate a cursor to begin reading from the first record until the last day of the record. Executeupdate (String SQL) methods are used to execute DDL/DML statements, such as Update,delete.

Only SELECT statements can return result sets.

Example: Statement sta = con.createstatement (); Creating statement objects

String sql = INSERT INTO Test (Id,name) VALUES (1 "+" "" "+" King "+" "" + ")";

Sta.executeupdate (SQL); Execute SQL statement

String sql = "SELECT * from Test";

Reslutset rs = sta.executequery (sql);//The result set after executing the SQL statement

Traversal processing result set

while (Rs.next ()) {

System.out.println (Rs.getint ("id"));

System.out.println (rs.getstring ("name"));

}

6. Close the database connection (free resources) call the closing () method

Rs.close ();           Sta.close (); Conn.close ();

ResultSet Statement connetction are dependent in turn.

        Note: To close the resource in the order of first ResultSet, then statement, and finally connetction. Because ResultSet and statement can be used in connection, there may be other statement connected after the bundle is used, so it is not possible to close connection first.

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.