前言
概述
JDBC(Java DataBase Connectivity) 是 Java 访问数据库的标准规范(接口),真正怎么操作数据库还需要具体的实现类,也就是数据库驱动。每个数据库厂商根据自家数据库的通信格式编写好自己数据库的驱动。
java程序员只需要面向这套接口进行编程,不需要关注底层。
jdbc接口和类在java.sql和javax.sql包中
JDBC实现连接数据库
前置工作:在项目下创建目录libs,将jar包mysql-connection-java拷贝到该目录并加入到项目中
1、注册驱动-加载Driver
2、获取连接-得到Connection
3、执行增删改查-发送SQL给mysql执行
4、释放资源-关闭相关连接
五种连接方式
例子:
public static void main(String[] args) throws SQLException {
//1、注册驱动
Driver driver = new Driver();
//2、得到连接
//(1)jdbc:mysql:// 规定好表示协议,通过jdbc的方式连接到mysql
//(2)localhost or 127.0.0.1 ip地址
//(3)3306 mysql监听端口
//(4)winlin 连接数据库名称
//(5)mysql的连接的本质是socket的连接
String url = "jdbc:mysql://localhost:3306/winlin";
Properties properties = new Properties();
properties.setProperty("user","username");
properties.setProperty("password","password");
Connection connect = driver.connect(url,properties);
//3、执行sql
String sql = "insert into test_student values ('jacky',1234565);";
//执行静态sql语句返回其生成结果的对象
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql);//如果是DML语句返回影响行数
System.out.println(rows>0?"成功":"失败");
//4、关闭连接资源
statement.close();
connect.close();
}
连接数据库还有其他多种方式:
第二种:利用反射机制(动态加载、更加灵活、减少依赖):
public static void funConnect2() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//反射机制加载Driver
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver)aClass.newInstance();
//创建url和url、password
String url = "jdbc:mysql://localhost:3306/winlin";
Properties properties = new Properties();
properties.setProperty("user","username");
properties.setProperty("password","password");
Connection connect = driver.connect(url,properties);
System.out.print(connect);
connect.close();
}
第三种:使用DriverManager(用于管理一组驱动程序的基本服务),拥有方法registerDriver(Driver driver)注册给定的驱动程序。
public static void funConnect3() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//反射机制加载Driver
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver)aClass.newInstance();
//创建url和url、password
String url = "jdbc:mysql://localhost:3306/winlin";
Properties properties = new Properties();
properties.setProperty("user","username");
properties.setProperty("password","password");
DriverManager.registerDriver(driver);//注册Driver驱动
Connection connect = DriverManager.getConnection(url,properties);//这里properties可以换成两字符串
System.out.print(connect);
connect.close();
}
第四种:其实使用Class.forName自动完成注册驱动的(推荐使用)
public static void funConnect4() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//反射机制加载Driver
//在加载Driver类时完成注册
//Driver类中静态代码块,在类加载时,会执行一次
/*
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
*/
Class.forName("com.mysql.cj.jdbc.Driver");
//创建url和url、password
String url = "jdbc:mysql://localhost:3306/winlin";
Properties properties = new Properties();
String user = "username";
String password = "password";
Connection connect = DriverManager.getConnection(url,user,password);//这里也可以用properties
connect.close();
}
其实第四种连接方式Class.forName(“com.mysql.cj.jdbc.Driver”);这条语句不写也能运行,原因是:
1、mysqL驱动5.1.6可以无需CLass . forName(“com.mysql.jdbc.Driver”);
2、从jdk1.5以后使用了jdbc4,不再需要显示调用class.forName()注册驱动而是自动调用驱jar包下META-INF\services\java .sqI.Driver文本中的类名称去注册
3、不过还是建议写上Class.forName(“com.mysql.cj.jdbc.Driver”);
第五种方式:基于第四种优化,写配置文件,对于Connection connection = DriverManager.getConnection(url,user,password);中各个值,如端口号,数据库,用户名,密码为了方便,我们可以将信息写入.properties文件中,方便操作
编写配置文件mysql.properties
user=username
password=password
url=jdbc:mysql://localhost:3306/databasename
driver=com.mysql.cj.jdbc.Driver
public static void funConnect05() throws SQLException, ClassNotFoundException, IOException {
//通过properties对象获取配置文件信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
//反射机制加载Driver
Class.forName(driver);
Connection connect = DriverManager.getConnection(url,user,password);
System.out.println(connect);
connect.close();
}
注意
关于mysql驱动jar包,笔者用的是8.0.25版本,此版本使用的Driver类是com.mysql.cj.jdbc包中,对于5版本使用com.mysql.jdbc包
Connection是一种宝贵的资源,很容易成为业务系统的性能瓶颈,使用原则是:晚打开,早关闭——使用时间越短越好,尽量可以重用,数据库Connection的数量不是无限的,它受限于电脑自身硬件CPU