Steps for the Java Foundation--JDBC linked database

Source: Internet
Author: User

Basic steps for the JDBC Operational database

1) Load (register) the database driver (to the JVM).

2) Establish (get) the database connection.

3) Create (GET) database operations object.

4) Define the SQL statement for the operation.

5) Perform database operations.

6) Get and manipulate the result set.

7) Close the object, reclaim the database resources (close the result set and close the database Action Object – closes the connection).

Package COM.YANGSHENGJIE.JDBC;  Import java.sql.Connection;  Import Java.sql.DriverManager;  Import Java.sql.ResultSet;  Import java.sql.SQLException;    Import java.sql.Statement;          public class Jdbctest {/** * uses JDBC to connect and manipulate MySQL database */public static void main (string[] args) {          String driver = "Com.mysql.jdbc.Driver" for the database-driven class name;          Database connection string String url = "Jdbc:mysql://127.0.0.1:3306/jdbctest";          User name String username = "root";          Password String password = "Mysqladmin";          Connection conn = null;          Statement stmt = null;          ResultSet rs = null;              try {//1, LOAD Database driver (after a successful load, an instance of the driver class is registered to the DriverManager Class) Class.forName (driver);              2. Get database Connection conn = drivermanager.getconnection (URL, username, password);              3, Get database operation object stmt = Conn.createstatement (); 4. SQL statement that defines the operation String sql = "seLect * FROM user where id = 100 ";              5. Perform database operation rs = stmt.executequery (SQL);                  6. Get and manipulate the result set while (Rs.next ()) {System.out.println (Rs.getint ("id"));              System.out.println (rs.getstring ("name"));          }} catch (Exception e) {e.printstacktrace ();                      } finally {//7, close object, recycle database resource if (rs! = null) {//Close result set object try {                  Rs.close ();                  } catch (SQLException e) {e.printstacktrace ();                  }} if (stmt! = null) {///Close Database Operation object try {stmt.close ();                  } catch (SQLException e) {e.printstacktrace (); }} if (conn! = null) {///Close Database Connection object try {if (!conn.isclo Sed ()) {Conn.close ();                  }} catch (SQLException e) {e.printstacktrace ();   }              }          }      }  }

Steps for the Java Foundation--JDBC linked database

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.