DriverManagerのことで、データベース接続(シリコンバレー、まだノート)を取得2。

DriverManagerは管理クラスを駆動します。

public static Connection getConnection() throws Exception,{
	//1.准备连接数据库的4个字符串
	//驱动的全类名
	String driverClass=null;
	String jdbcUrl=null;
	String user=null;
	String password=null;
	//读取类路径下的jdbc.properties文件中连接数据库的4个字符串
	ClassLoader loader = Thread.currentThread().getContextClassLoader();
	Properties properties=new Properties();
	properties.load(loader.getResourceAsStream("jdbc.properties"));
	driverClass=properties.getProperty("driver");//读取接口类型
	jdbcUrl=properties.getProperty("jdbcUrl");
	user=properties.getProperty("user");
	password=properties.getProperty("password");
	//2.加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块注册驱动)
	//DriverManager.registerDriver((Driver) Class.forName(driverClass).newInstance());
        //可注册多个驱动程序
	Class.forName(driverClass);
	//3.调用DriverManager的getConnection(url,user,password)获取数据库连接
	Connection connection=DriverManager.getConnection(jdbcUrl,user,password);
	return connection;
}

1)のgetConnection()メソッドがオーバーロードされたデータベース接続を介して得ることができる、より便利です。

2)同時に複数のドライバを管理してもよい:登録データベース接続、コールのgetConnection()メソッドの複数の異なるパラメータが渡された場合ならば、即ち、異なるデータベース接続を返します。

公開された90元の記事 ウォン称賛48 ビュー10000 +

おすすめ

転載: blog.csdn.net/Asher_S/article/details/90240815