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.
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!