In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve Java using JDBC to connect to the database in IDEA software". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preparatory work
1. Import the jar package of mysql into the lib directory
two。 Add the imported jar package to the project
Click the jar package to select
3. Create a TestConnection class
Five ways are as follows: / * * @ author * @ date 2019 * * / import org.junit.Test; import java.io.InputStream;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties / * JDBC connection * / public class ConnectionTest {/ / method 1 @ Test public void testConnection1 () throws SQLException {/ / get the object Driver driver=new com.mysql.jdbc.Driver () of the driver implementation class; / / url Http://localhost:8080/gmall/hello.jpg String url= "jdbc:mysql://localhost:3306/student"; / / encapsulate the user name and password of the database in Properties Properties info=new Properties (); info.setProperty ("user", "root"); info.setProperty ("password", "root"); / / info.setProperty ("user", "root") / / info.setProperty ("password", "root"); Connection conn = driver.connect (url, info); System.out.println (conn);} / / iteration of mode two to mode one No third-party api is a better portability of the program @ Test public void testConnection2 () throws Exception {/ / get the object reflection of the driver implementation class Class clazz = Class.forName ("com.mysql.jdbc.Driver"); Driver driver= (Driver) clazz.newInstance (); / / 2. Provide the database String url= "jdbc:mysql://localhost:3306/student" to connect to; / / 3. Provide the user password Properties info=new Properties (); info.setProperty ("user", "root"); info.setProperty ("password", "root"); / / 4. Get the link Connection connect = driver.connect (url, info); System.out.println (connect);} / / use drivermanager to replace driver @ Test public void testConneciont3 () throws Exception {/ / 1. Get the implementation class Class clazz=Class.forName ("com.mysql.jdbc.Driver") of Driver; Driver driver= (Driver) clazz.newInstance (); / / 2. Provide another three to get connection information: String url= "jdbc:mysql://localhost:3306/student"; String user= "root"; String password= "root"; / / registered driver DriverManager.registerDriver (driver); / / get connection Connection conn=DriverManager.getConnection (url,user,password); System.out.println (conn) } / / Mode 4 @ Test public void testConneciont4 () throws Exception {/ / 1 provides three access to connection information String url= "jdbc:mysql://localhost:3306/student"; String user= "root"; String password= "root"; / / 2. Loading Driver does not need to show registered driver Class.forName ("com.mysql.jdbc.Driver"); / / mode 3 optimization, omitting the following operations, the implementation class of Driver automatically executes / / Driver driver= (Driver) clazz.newInstance (); / / registered driver / / DriverManager.registerDriver (driver); / / 3. Get connection Connection conn=DriverManager.getConnection (url,user,password); System.out.println (conn);} / / Mode 5 (final) declare the configuration information required for database connection in the configuration file to read the configuration. See, get the link / * benefits * 1. Realize the separation of data and code and decoupling * 2. If you need to modify the configuration file information, you can avoid the program repackaging * @ throws Exception * / @ Test public void TestConnection5 () throws Exception {/ / read the information in the configuration file InputStream is=ConnectionTest.class.getClassLoader () .getResourceAsStream ("jdbc.properties"); Properties pros=new Properties (); pros.load (is). String user=pros.getProperty ("user"); String password=pros.getProperty ("password"); String url=pros.getProperty ("url"); String driverClass=pros.getProperty ("driverClass"); / / 2. Load the driver Class.forName (driverClass); / / 3. Get the link Connection conn=DriverManager.getConnection (url,user,password); System.out.println (conn);}} "how to implement Java in IDEA software to connect to the database using JDBC" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.