Tip: After the article is written, the catalog can be generated automatically, how to generate the help document to the right
JDBC: Five ways to get a database connection
Before we can get a database connection, we need to do some preparatory work:
- Import database connection related database drivers (also known as third-party api) into project projects. I'm using the mysql database here. Database Driven Package, please get it manually: Link: https://pan.baidu.com/s/1JMWJQD5R4oWzUchOtkjpGQ
Extraction code: ye59
Be careful:
- If the database driver is not imported, the program cannot run.
- The first four methods are not recommended in actual project development, and the fifth method is used eventually. Each connection method is optimized from the previous one for reference only.
Mode 1:
//1. Get the Driver implementation class object, where com.mysql.jdbc.Driver() //api from third party Driver driver = new com.mysql.jdbc.Driver(); //2. Provide a database to connect to String url = "jdbc:mysql://localhost:3306/test" /* url = "jdbc:mysql://localhost:3306/test" Understanding 1,jdbc:mysql Yes, jdbc is the master protocol, mysql is the sub-protocol 2,localhost: ip Address, local machine 3,3306: mysql Server port number 4,test: Database name, if the table used in the project is in another database, modify the database name */ //3. Properrties object encapsulates user and password of database Properties pro = new Properties(); pro.setProperty("user","Database User Name"); pro.setProperty("password","Database Password"); //4. Connect Connection conn = driver.connect(url,pro); return conn;
about
pro.setProperty("user", "database user name");
pro.setProperty("password", "database password");
Interpretation of database user name and database password in:
! [Insert picture description here] ( https://img-blog.csdnimg.cn/6b245614defa49549f578f7ea33ef11b.png?x -oss-process=image/watermark, type_ ZHJvaWRzYW5zmFsbGJhY2s, shadow_ 50, text_ Q1NETiBA6JKZ5aic5Li96I6O5bCP5b6u56yR, size_ 14, color_ FFFFFF, _ 70, g_ Se, x_ 16
Optimize mode one (no third-party api in code, better portability)
//1. Get Driver implementation class object by reflection class aclass = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver)aclass.newInstance(); //The steps and methods below are the same //2. Provide a database to connect to String url = "jdbc:mysql://localhost:3306/test" //3. Properrties object encapsulates user and password of database Properties pro = new Properties(); pro.setProperty("user","Database User Name"); pro.setProperty("password","Database Password"); //4. Connect Connection conn = driver.connect(url,pro); return conn;
Optimize Mode 2 (replace Driver with DriverManager)-
Java. Sql. The Driver interface is the interface that all JDBC drivers need to implement. This interface is provided to database vendors, which provide different implementations.
- Instead of directly accessing the classes that implement the Driver interface in the program, these Driver implementations are invoked by the Driver Manager class (java.sql.DriverManager).
- Oracle driver: oracle.jdbc.driver.OracleDriver
- mySql driver: com.mysql.jdbc.Driver
//1. Get the object of the Driver implementation class Class aClass = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) aClass.newInstance(); //2. Register (Load) Database Driver DriverManager.registerDriver(driver); //3. Provide three elements to get a database connection: url, user, password //Get url (indicating the connected database) String url = "jdbc:mysql://localhost:3306/test"; //Database User Name String user = "xxxx";//root if the connection is to a native database //Database Password String password = "xxxx" //4. Get Connections Connection conn = DriverManager.getConnection(url, user, password); return conn;
Mode 4: Optimizing Mode 3
//1. Provide three elements to get connected String url = "jdbc:mysql://localhost:3306/test"; String user = "Database User Name"; String password = "Database Password"; //2. Registration Driver Class.forName("com.mysql.jdbc.Driver"); //3. Get Connections Connection conn= DriverManager.getConnection(url, user, password); return conn;
Mode 5 (final version):
Declare the four basic information required for a database connection (user, password, url, and information needed to load the driver) in the configuration file jdbc.Properties to get the connection by reading the configuration file
Configuration file jdbc.Properties:
Jdbc. Contents in Properties:
user=Database User Name password=Database Password url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true driverClass=com.mysql.jdbc.Driver
Get the main code for the connection:
//Load jdbc.Properties file, stored in the corresponding stream object InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.Properties"); //A new Properties object Properties pro = new Properties(); //Load the is stream file into the corresponding Properties object pro pro.load(is); //Get JDBC from pro. ket-value in Properties String user = pro.getProperty("user"); String password = pro.getProperty("password"); String driverClass = pro.getProperty("driverClass"); String url = pro.getProperty("url"); //Load Driver Class.forName(driverClass); //Get Connections Connection conn = DriverManager.getConnection(url, user, password); return conn;
The benefit of comparing the fifth method with the first is that:
1, Using a configuration file will get the elements of a database connection (User name, password, url, database driver) is encapsulated to avoid hard-coding code in the program. This way, if the database user name or password we are using is modified, or if another database is used, the program code does not need to be modified, only the corresponding content in the configuration file needs to be modified. This is accomplished. Code decoupling.
2. If you need to modify the configuration file information, you can avoid the program repackaging.