从今天开始加更数据库中的内容了,满足大家需求
首先更新如何去连接数据库
这是连接数据库的第一种方式,以及里面的具体内容
public void testconnetction1() throws SQLException {
//获取Driver实现类对象,加载驱动
Driver driver=new com.mysql.cj.jdbc.Driver();
// jdbc:mysql://localhost:8080/test
//jdbc:mysql协议
//localhost:ip地址
//3306:mysql默认端口号
//test:使用的数据库名称
String url ="jdbc:mysql://localhost:3306/test";
//将用户名和密码封装在 Properties
Properties info =new Properties();
info.setProperty("uer","root");
info.setProperty("password","password");
Connection connect = DriverManager.getConnection(url,"root","password");
System.out.println(connect);
}
数据库连接的第二种方式
//对方式一的迭代,在如下的程序中不出现第三方api,使得程序具有可移植性
@Test
public void testconnetction2() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
//1.使用Driver实现类对象,使用反射。
Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver)clazz.newInstance();
//2.提供要连接的数据库
String url="jdbc:mysql://localhost:3306/test";
//提供连接所需要的用户名和密码
Properties in=new Properties();
in.setProperty("user","root");
in.setProperty("password","password");
//4.获取链接
Connection connect = driver.connect(url, in);
System.out.println(connect);
}
这是连接数据库的两种方式,大家可以去试试
给大家提供连接成功的参考图,可以试试。