首先,使用java程序访问数据库的前提

  数据库的主机地址(ip地址)

  端口

  数据库用户名

  数据库用户密码

  连接的数据库

代码:

	private static String url = "jdbc:mysql://localhost:3306/user";
	
	private static String username = "root";
	
	private static String password = "131411";
	
	private static String classDriver = "comm.mysql.jdbc.Driver";

 方式一:通过驱动driver类来连接数据库

		Driver driver = new Driver();					//注册驱动对象
		Properties prop = new Properties();				//读取信息
		prop.setProperty("user", username);
		prop.setProperty("password", password);
		Connection connect = driver.connect(url, prop);	//获取连接对象
		System.out.println(connect);

 方式二:通过DriverManager,注册Driver,来连接数据库

		DriverManager.registerDriver(new Driver());
		Connection connection = DriverManager.getConnection(url, username, password);
		System.out.println(connection);

 方式三:通过反射Driver,加载Driver里面的静态代码块,实现自动注册Driver,在DriverManager连接数据库

		Class.forName(classDriver);
		Connection connection = DriverManager.getConnection(url, username, password);
		System.out.println(connection);

 方式四:

		Driver driver = new Driver();
		Connection conn = DriverManager.getConnection(url, username, password);
		System.out.println(conn);

 方式五:

		Connection connection = DriverManager.getConnection(url, username, password);
		System.out.println(connection);