@Test
public void test2() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
//加载Driver(驱动)
Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
//相较于方式三可以省略如下的操作,因为在Mysql的实现类静态代码块中实现了如下的操作
// Driver driver= (Driver) clazz.newInstance();
//2 提供另外三个连接的基本信息
String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
String user="root";
String password="123456";
//注册驱动
// DriverManager.registerDriver(driver);
//获取连接
DriverManager.getConnection(url,user,password);
}
//方式五
@Test
public void getConnection5() throws IOException, ClassNotFoundException, SQLException {
//1 读取配置文件中的四个基本信息
InputStream is =ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(is);
String user=properties.getProperty("user");
String password=properties.getProperty("password");
String url=properties.getProperty("url");
String driverClass=properties.getProperty("driverClass");
//加载驱动
Class<?> clazz = Class.forName(driverClass);
//3 获取连接
DriverManager.getConnection(url,user,password);
}
/*
下面为配置文件
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8;
user=root;
password=123456;
driverClass=com.mysql.jdbc.Driver
*/
使用idea连接数据库使用第一种方法可以连接成功,把信息写入配置文件就报错了这是为什么呢
配置文件不对啊,驱动后面多两个斜杠啥意思