Home  >  Article  >  Java  >  How to test the link to mysql in java?

How to test the link to mysql in java?

little bottle
little bottleOriginal
2019-05-09 15:17:432496browse

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);
	}
}

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!

Statement:
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