需要mysql-connector-java-5.1.7-bin.jar
@Test public void testConnection1() throws SQLException { Driver driver = new com.mysql.jdbc.Driver(); /* * jdbc:主协议 * mysql:子协议 * localhost:ip * 3306:端口 * test:数据库名 */ String url = "jdbc:mysql://localhost:3306/test"; /* * 封装数据库的用户名和密码 * Normally at least a "user" and"password" property should be included. */ Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "123456"); Connection connect = driver.connect(url, info); System.out.println(connect); }