数据库
一、数据库
1.1 数据库种类
1.2 框架&工具
二、数据库在Java中的使用原理
利用多态的特性,Java提供一个接口(规范sql语句),而MySQL、SqlServer等实现该接口(将实现类打包成一个jar包)
三、JDBC的使用
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.cj.jdbc.Driver;
public class Demo {
static {
try {
//注册驱动
new Driver();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//建立连接数据库
String url = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC";
String username = "root";
String password = "123456";
connection = DriverManager.getConnection(url, username, password);
//创建执行语句平台
String sql = "SELECT * FROM student";
statement = connection.createStatement();
//执行sql操作
resultSet = statement.executeQuery(sql);
//获得数据
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println(id + "..." + name + "..." + age);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭资源
try {
if(connection != null) {
connection.close();
}
if(statement != null) {
statement.close();
}
if(resultSet != null) {
resultSet.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
pom.xml中(MySQL可以去找search.maven.org)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.kooun.db</groupId>
<artifactId>database</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
</dependencies>
</project>