JDBC快速上手
JDBC快速上手
前置條件
-
安裝了mysql8.0,在mysql中建立一個字符集為utf8mb4,名字為imooc的資料庫
-
執行以下sql語句
/* Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Source Server Version : 80016 Source Host : localhost:3306 Source Schema : imooc Target Server Type : MySQL Target Server Version : 80016 File Encoding : 65001 Date: 24/02/2020 17:13:16 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for employee -- ---------------------------- DROP TABLE IF EXISTS `employee`; CREATE TABLE `employee` ( `eno` int(11) NOT NULL, `ename` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `salary` float(10, 2) NOT NULL, `dname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `hiredate` date NULL DEFAULT NULL, PRIMARY KEY (`eno`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of employee -- ---------------------------- INSERT INTO `employee` VALUES (1000, '員工1000', 5000.00, '市場部', '1992-03-04'); INSERT INTO `employee` VALUES (1001, '員工1001', 3500.00, '市場部', '1988-02-18'); INSERT INTO `employee` VALUES (1002, '員工1002', 4000.00, '市場部', '1996-02-01'); INSERT INTO `employee` VALUES (1003, '員工1003', 4000.00, '市場部', '2001-08-21'); INSERT INTO `employee` VALUES (1004, '員工1004', 4000.00, '市場部', '2003-01-02'); INSERT INTO `employee` VALUES (1005, '員工1005', 4000.00, '市場部', '1996-07-28'); INSERT INTO `employee` VALUES (1006, '員工1006', 4000.00, '市場部', '1999-12-30'); INSERT INTO `employee` VALUES (1007, '員工1007', 4000.00, '市場部', '2009-05-30'); INSERT INTO `employee` VALUES (1008, '員工1008', 4000.00, '市場部', '1984-05-30'); INSERT INTO `employee` VALUES (1009, '員工1009', 4000.00, '市場部', '2004-05-07'); INSERT INTO `employee` VALUES (3308, '張三', 6000.00, '研發部', '2011-05-08'); INSERT INTO `employee` VALUES (3420, '李四', 8700.00, '研發部', '2006-11-11'); INSERT INTO `employee` VALUES (3610, '王五', 4550.00, '市場部', '2009-10-01'); SET FOREIGN_KEY_CHECKS = 1;
3.在工程中匯入mysql驅動程式的jar包 – mysql-connector-java-8.0.16.jar
JDBC開發流程
-
載入並註冊JDBC驅動
Class.forName("com.mysql.cj.jdbc.Driver");
-
建立資料庫連線
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&character=UTF-8&serverTimezone=Asia/Shanghai","root","root");
-
建立Statement物件
Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from employee");
-
遍歷查詢結果
while (rs.next()) { Integer eno = rs.getInt(1); String ename = rs.getString("ename"); Float salary = rs.getFloat("salary"); String dname = rs.getString("dname"); System.out.println(dname + "-" + eno + "-" + ename + "-" + salary); }
-
關閉連線,釋放資源
try {
if (conn != null && conn.isClosed() == false) {
//5. 關閉連線,釋放資源
conn.close();
}
}catch (Exception ex){
ex.printStackTrace();
}
完整程式碼【例項】
package com.imooc.jdbc.sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 標準JDBC操作五步驟
* @author CubeMonkey
* @create 2020-09-27 11:27
*/
public class StandardJDBCSample {
public static void main(String[] args) {
Connection conn = null;
try {
//1. 載入並註冊JDBC驅動
Class.forName("com.mysql.cj.jdbc.Driver");
//2.建立資料庫連線
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&character=UTF-8&serverTimezone=Asia/Shanghai",
"root",
"root"
);
//3. 建立Statement物件
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from employee");
//4. 遍歷查詢結果
while (rs.next()) {
Integer eno = rs.getInt(1);
String ename = rs.getString("ename");
Float salary = rs.getFloat("salary");
String dname = rs.getString("dname");
System.out.println(dname + "-" + eno + "-" + ename + "-" + salary);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (conn != null && conn.isClosed() == false) {
//5. 關閉連線,釋放資源
conn.close();
}
}catch (Exception ex){
ex.printStackTrace();
}
}
}
}
執行效果:
相關文章
- Redis 快速上手Redis
- 快速上手BootstrapVuebootVue
- ElasticJob 快速上手AST
- Flask快速上手Flask
- docker 快速上手Docker
- Flask之旅: 快速上手Flask
- Vue框架快速上手Vue框架
- 快速上手之dockerDocker
- 快速上手前端框架layui前端框架UI
- Git和GitHub快速上手Github
- Docker 快速上手指南Docker
- Zalando Postgres Operator 快速上手
- 快速上手 vue3Vue
- LLaMA快速上手指南
- Git 快速上手精華梳理Git
- vue.js快速上手Vue.js
- react native快速上手React Native
- Vue.js 快速上手。Vue.js
- 資訊科技快速上手教程
- gdb除錯快速上手除錯
- Vue3快速上手Vue
- 【零基礎上手JavaWeb】快速上手 JavaScript DOM操作WebJavaScript
- Flutter ListView 實戰快速上手FlutterView
- SAP系統如何快速上手?
- React 教程:快速上手指南React
- Spring-Boot快速上手Springboot
- Github Action 快速上手指南Github
- Vue同構(一): 快速上手Vue
- Jenkins快速上手安裝Jenkins
- SpringBoot-快速上手Spring Boot
- 『Beego + Swagger 快速上手』GoSwagger
- ES6快速上手 —— APIAPI
- 快速上手Linux核心命令Linux
- 十分鐘快速上手NutUIUI
- 如何1小時快速上手SQL?SQL
- Vue入門指南(快速上手vue)Vue
- 快速上手最新的 Vue CLI 3Vue
- 幾個特性,快速上手KotlinKotlin