JDBC (Connection and use of databases)

Source: Internet
Author: User

I. Introduction to JDBC

    1. Overview of JDBC:
      Jdbc:java Database Connectivity The Java code to operate the databases JDBC is also a set of specifications for most of the JDBC interface, the implementation is a large database vendor is the JDBC interface implementation of the JAR package.
    2. What are the JDBC specifications
      1. Java.sql.DriverManager class: The portal responsible for driver registration and JDBC operations
      2. Java.sql.Connection interface: Responsible for connecting the database
      3. Java.sql.Statement interface: Responsible for operating the database
      4. Java.sql.ResultSet interface: Responsible for encapsulating the result data of the query
      5. Java.sql.PreparedStatement: Responsible for pre-compiling SQL statements in the operations database
    3. Quick Start
      Preparatory work:
      1. Actionable data in the database
      2. Preparing the drive Jar package for the database
TwoCoding phase:
  1. General process for manipulating databases
    1. Registration driver
    2. Link Database
    3. Execute SQL Statement Operations database
    4. Traversing result Sets
    5. Close Resource
      1. //(1) 注册驱动
      2. DriverManager.registerDriver(newDriver());
      3. //(2) 连接数据库
      4. Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/android","root","didi848484");
      5. //(3) 执行sql语句操作数据库
      6. //1.创建一个 Statement 对象来将 SQL 语句发送到数据库。
      7. Statement stem=conn.createStatement();
      8. //2.执行SQL语句
      9. ResultSet rs=stem.executeQuery("select * from user");
      10. //(4) 遍历结果集
      11. while(rs.next())
      12. {
      13. System out print ( "ID:" + rs Span class= "pun". getint "id"
      14. System out print ( + rs getstring
      15. System.out.print(",password:"+rs.getString("password"));
      16. //.....
      17. System.out.println();
      18. }
      19. //(5) 关闭资源
      20. rs.close();
      21. stem.close();
      22. conn.close();
  2. A detailed database connection process
    1. Registration driver
      Generally there are 2 ways:
      1. In the first way, use the method provided by DriverManager to register
      Format: Drivermanager.registerdriver (New Driver ())///Here's new Driver () Be sure to import the classes in the corresponding database jar package
      Example: Drivermanager.registerdriver (New Driver ()); Half of this method is not used, the reason is the tracking source discovery, the source code to drive duplicate registration, affecting efficiency.

      2. Second way: Use reflection to register
      Format: Class.forName ("Driver's all-inclusive name");
      Example:class.forname ("Com.mysql.jdbc.Driver");
      1. Class.forName("com.mysql.jdbc.Driver");
      In development, it is common to use reflection to register drivers. The benefit has two points, one: The driver registers only once, improves the efficiency. Second: Can be driven by the full package name to extract the configuration file to facilitate later maintenance
    2. Connecting to a database
      Format: Connection conn = drivermanager.getconnection (Url,name,password);
      URL: The address of the database
      Format: Master Protocol: Sub-Protocol://HOST: Port/Database name
      Example: JDBC:MYSQL://LOCALHOST:3306/JDBC
      Note: If the host is a native (localhost) port and the default of 3306, it can be abbreviated as: JDBC:MYSQL:///JDBC
      Name: User Name
      Password: password
    3. Creating statement to execute SQL statements
        1. Create a statement object
          Format: Statement stmt = Conn.createstatement ();
        2. Execute SQL statement
          Execute SQL statements using the 2 methods under the statement object
          A.int executeupdate (String sql): return value int is the number of rows affected//used primarily to execute: Add, delete, change statements, or SQL statements that do not return any content (such as SQL DDL statements)
          B.resultset executeQuery (String sql): Executes a query statement that returns results resultset//primarily for receiving the results of a SELECT statement query
    4. ResultSet result set
      ResultSet internally encapsulates the data returned by the query: ResultSet is the encapsulation of the results of the query can be seen as a table with its internal maintenance of a cursor (pointer) at the beginning of the cursor pointing at the top of the first data, when we execute the next () method, The cursor will look down to see if there is a next piece of data, if there is a next method that returns true, and the cursor moves down one line, and if no next data returns FALSE, when the cursor points to a row, the data in one of the fields in this row can be obtained through the GetXXX () method. How to fetch data from a resultset
      1. First Snoop through the next () method to see if there is a row of data in the result set, return FALSE if it returns true and the result set pointer moves to the next line
      2. When the next () method returns True, the data can be fetched by means of the ResultSet object, such as: GetInt () getString () getDate () GetObject () in parentheses parameter: Field name (queried field name)
      1. //创建ResultSet对象来接收查询结果
      2. ResultSet rs=stem.executeQuery("select * from user");
      3. //通过next()和各类get方法取出数据
      4. while(rs.next())
      5. {
      6. System out print ( "ID:" + rs Span class= "pun". getint "id"
      7. System out print ( + rs getstring
      8. System.out.print(",password:"+rs.getString("password"));
      9. //.....
      10. System.out.println();
      11. }
    5. Close Resource
      Principle: Resources related to the database are all closed from small to large
      Rresultset---->statement------->connection



From for notes (Wiz)



JDBC (Connection and use of databases)

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.