Java connection to database step resolution (Oracle MySQL)

  • 2020-05-26 08:25:11
  • OfStack

1.java connects to Oracle database

Use the following code in three steps:

Download the ojdbc.jar package and import it into the project. Put the following code where you think it should be. Change the code: change MyDbComputerNameOrIP to IP on the computer where the Oracle database is located, and change UserName and Password to the user name and password defined previously.

Then, you can use it.


Connection connect = null;
Class.forName("oracle.jdbc.driver.OracleDriver");// join oracle "Is the path of the driver 
String url = "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:myorcl";//  Database connection, oracle The one that stands for link is oracle Database; thin:@MyDbComputerNameOrIP This is where the database is located IP Address (can be kept thin: ); 1521 Represents the port number of the linked database; myorcl Represents the database name  
String UserName = "******";//  Database user login name  
String Password = "******";//  password  
connect = DriverManager.getConnection(url, UserName, Password);

2.Java connects to MySQL database

Use the following code in three steps:

Download the mysql-jdbc.jar package and import it into the project. Again, put the following code where you think it should be. Change the code: change localhost to IP on the computer where the MySQL database is located, change test to the database name, change root, root to the user name and password as defined previously.

Then, you can use it.


Class.forName("com.mysql.jdbc.Driver");
   Connection connect = DriverManager.getConnection( 
   "jdbc:mysql://localhost:3306/test" , //
   "root" , // The user name 
   "root"// password 
   );

Related articles: