pytes 132個命令列引數用法
阿新 • • 發佈:2020-11-17
一、資料庫
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>