DAY17-JDBC 5.url Introduction

Source: Internet
Author: User

The URL identifies the location of the database and identifies which database to look for.

Summary: The URL is the path, in fact, is to determine which database. Used to determine which database I was using, and to tell me which database the connection or this drivermanager was looking for when getting this connection.

Package Cn.itcast.jdbc;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.ResultSet;import   Java.sql.sqlexception;import Java.sql.statement;import com.mysql.jdbc.driver;//Solve the problem of loading driver public class JdbcDemo2 { public static void Main (string[] args) throws SQLException, ClassNotFoundException {//1. Register driver//drivermanager.registe  Rdriver (New Driver ());//A registered driver, the driver is loaded into memory so that we can use it to load two driver Class.forName ("Com.mysql.jdbc.Driver");//advantage is to load once Replace Registerdriver () This method is not coupled, it is useless mysql-driven things do not mean that it does not have to drive, just do not rely on the jar package//In the code, the performance is not seen at all the driver package is the common type of interface (Connection, Dr Ivermanager, ResultSet, SQLException, Statement), which is the common interface//programming provided in the JDK, knows that it is best not to rely on anything, because there is no coupling, but that is impossible. So try to get the coupling down//load MySQL driver class.forname ("Oracle.jdbc.driver.OracleDriver");//Load Oracle driver//How does it know if this is an Oracle driver or a MySQL driver?    How is it differentiated?  String url = "Jdbc:mysql://localhost:3306/day17";      String url = "Jdbc:mysql:///day17";    2. Get the Connection object connection con = drivermanager.getconnection (URL, "root", ""); System.out.prinTln (con);  Connection is the interface, is the driver, the driver is sure to implement this interface to achieve programming with the top-level to receive operation will be more convenient front is the parent, followed by the child with the topmost receive//3. Gets the operation of the SQL statement through the Connection object Statement Statement st =    Con.createstatement ();    4. Operation SQL statement String sql = "SELECT * from user"; ResultSet rs = st.executequery (sql);//resultset is the result of a query, which can be imagined as a collection, but it is not a collection of//5. Traversal result set/* Boolean flag = Rs.next ();        Moves down, the return value is true, and the next record is represented.    SYSTEM.OUT.PRINTLN (flag);    int id = rs.getint (1);    int id1 = Rs.getint ("id");    SYSTEM.OUT.PRINTLN (ID);    System.out.println (ID1);    String username = rs.getstring (2);    String username1 = rs.getstring ("username");    SYSTEM.OUT.PRINTLN (username);    System.out.println (username1); */while (Rs.next ()) {int id = rs.getint ("id");    String username = rs.getstring ("username");    String Password = rs.getstring ("password");    String email = rs.getstring ("email");    System.out.println (id+ "" +username+ "" +password+ "" +email "); }//As long as the Java program is connected to any device, the resource must be freed after it is exhausted. The simplest basic class tells the I/O stream, Java connects to the file, and then closes the file stream after it is exhausted. The database is also a device. JavaThe connection to the database is also closed after it is exhausted. The resource must be released.    6. Release resources rs.close ();//result set st.close ();    Con.close (); The con is closed directly, and the database and program are disconnected. But statement and ResultSet were not released in time, and it was still in memory.    Close () is the release and recycling of resources. Close () The order and the door to go out is the same, is the door when the advanced door, out of time is the last door//=============//java can operate the database}}

DAY17-JDBC 5.url Introduction

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.