Through JDBC, a comprehensive understanding of the role of reflection, the role of the interface

Source: Internet
Author: User

 Public voidTestdrivermanager ()throwsexception{//1. Prepare 4 strings to connect to the database. //the full class name of the drive.String Driverclass = "Com.mysql.jdbc.Driver"; //JDBC URLString Jdbcurl = "Jdbc:mysql:///test"; //UserString user = "root"; //PasswordString password = "1230"; //2. Load the database driver (the corresponding Driver implementation class has a registered-driven static code block.)Class.forName (Driverclass); //3. Get the database connection through the DriverManager getconnection () method.Connection Connection =drivermanager.getconnection (jdbcurl, user, password);             System.out.println (connection); }        /*** Driver is an interface: The database vendor must provide an interface for implementation. The database connection can be obtained from it.     * Database connections can be obtained through the Driver implementation class object. * * 1. Added MySQL driver * 1). Unzip Mysql-connector-java-5.1.7.zip * 2). Create a new Lib directory * 3 under the current project). Copy the Mysql-connector-java-5.1.7-bin.jar to the Lib directory * 4). Right-Build-path, add to BuildPath is added to the classpath. S *@throwsSQLException*/@Test Public voidTestdriver ()throwsSQLException {//1. Create an object of the Driver implementation classDriver Driver =NewCom.mysql.jdbc.Driver (); //2. Basic information about preparing a connection database: URL, user, passwordString url = "Jdbc:mysql://localhost:3306/test"; Properties Info=NewProperties (); Info.put ("User", "root"); Info.put ("Password", "1230"); //3. Call the Connect (URL, info) of the Driver interface to get the database connectionConnection Connection =driver.connect (URL, info);    System.out.println (connection); }        /*** Write a common method, without modifying the source program, can get the connection of any database * solution: The database-driven Driver implementation class of the full class name, URL, user, password into a * configuration file, by modifying      The configuration file is implemented in a way that is decoupled from the specific database. * @throwsException*/     PublicConnection getconnection ()throwsexception{String Driverclass=NULL; String Jdbcurl=NULL; String User=NULL; String Password=NULL; //read the Jdbc.properties file under the ClasspathInputStream in =getclass (). getClassLoader (). getResourceAsStream ("Jdbc.properties"); Properties Properties=NewProperties ();        Properties.load (in); Driverclass= Properties.getproperty ("Driver"); Jdbcurl= Properties.getproperty ("Jdbcurl"); User= Properties.getproperty ("User"); Password= Properties.getproperty ("Password"); //by reflecting common Driver objects.Driver Driver =(Driver) class.forname (Driverclass). newinstance (); Properties Info=NewProperties (); Info.put ("User", user); Info.put ("Password", password); //get the database connection through the Driver Connect method.Connection Connection =Driver.connect (Jdbcurl, info); returnconnection; } @Test Public voidTestgetconnection ()throwsexception{System.out.println (getconnection ()); }

Through JDBC, a comprehensive understanding of the role of reflection, the role of the interface

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.