JDBC Operations Database

Source: Internet
Author: User

the DriverManager in the JDBC program is used to load the driver and create a link to the database. Common methods of this API: Drivermanager.registerdriver (New Driver ()); drivermanager.getconnection (Url,user,password); Note: It is not recommended to use the Registerdriver method to register drivers in actual development for two reasons: 1. Looking at the source code of driver, you can see that if this is the case, it will cause the driver to register two times, that is, there will be two driver objects in memory. 2. The program relies on the MySQL API, out of the MySQL jar package, the program will not compile, the future program to switch the underlying database will be very cumbersome. Recommended way: Class.forName ("Com.mysql.jdbc.Driver"); using this method does not cause the drive object to recur in memory, and in this way, the program simply needs a string to be dependent on the specific driver, making the program more flexible.   URL is used to identify the location of the database, the programmer through the URL address to tell the JDBC program to connect to which database, the URL is written as: jdbc:mysql://localhost:3306/test? key=value     Common Database URL address notation: Oracle notation: MySQL url:jdbc:mysql://localhost:3306/database name Oracle Url:jdbc:oracle:thin: @localhost: 1521:sid   Jdbc:oracle:thin: @localhost: 1521:sid Mysql-jdbc:mysql://localhost:3306/sid The short form of the MySQL URL address: Jdbc:mysql:///sid Common Properties: Useunicode=true&characterencoding=utf-8     //Registration driver The first type: Drivermanager.registerdriver (new Driver ()); the second type: Class.forName ("Com.mysql.jdbc.Driver"); in the first case, there is a static block of code in the Com.mysql.jdbc.Driver class:   sttic { try { Java.sql.DriverManager.registerDriver (New Driver ()); } catch (SQLException ex) { throw new RuntimeException ("Can ' t Register driver"); } }   the problem with the above code: 1, two MySQL drivers will be loaded in the drive manager. Solution: Use reflection class.forname ("Com.mysql.jdbc.Driver"); Analysis: What are the benefits of using reflection to load drivers? A. Load only once, loading a driver object, B. Reduce the coupling and not rely on the driver.     Importjava.sql.Connection; Import Java.sql.DriverManager; Import java.sql.SQLException; Import java.sql.Statement;  Import Org.gjt.mm.mysql.Driver;Registered driver Drivermanager.registerdriver (new Driver ()); Connection TEST Connection conn = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mydb1", "root", "root");//Through the Connection object Create an Action SQL statement object Statementstatement st = Conn.createstatement ();//Operation SQL statement String t_sql = "SELECT * from Employee";//Execute SQL return Query result set Resultset;java.sql.resultset rs = St.executequery (T_sql); Traverse the result set while (Rs.next ()) {int id = rs.getint ("id"); String name = rs.getstring ("name"); String gender = rs.getstring ("gender"); SYSTEM.OUT.PRINTLN ("Serial number:" + ID + ", Name:" + name + ", Position:" + gender+ ";");}//Release Resourcesrs.close ();st.close ();conn.close ();

JDBC Operations 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.