Home > Java > javaTutorial > body text

How to test the link to mysql in java?

little bottle
Release: 2020-09-15 14:41:49
Original
2533 people have browsed it

When you connected mysql to your Java program, did you have any doubts about whether the connection was really successful? Anyway, the editor feels very uneasy every time he encounters this kind of uncertain situation, so let’s test it.

How to test the link to mysql in java?

Add the jdbc jar package that needs to be connected to the project first, create a test class, and test it.

The operation mode is: JUnit Test;

Test if the output connection is successful, it means success!

Package cn.my.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.junit.Test;
import java.sql.Connection;
 
public class JDbctest {
	@Test
	public void jdbcall() throws ClassNotFoundException, SQLException{		
		Class.forName("com.mysql.jdbc.Driver");//加载驱动类
		String url="jdbc:mysql://localhost:3306/info";
		String username="root";
		String password="mysql123";
		Connection conn=DriverManager.getConnection(url,username,password);//用参数得到连接对象
		System.out.println("连接成功!");
		System.out.println(conn);
	}
}
Copy after login

The above is the detailed content of How to test the link to mysql in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!