当前位置:网站首页>JDBC与MySQL数据库
JDBC与MySQL数据库
2022-06-28 01:03:00 【m0_61811389】
目录
一、JDBC 连接MySQL
先安装好MySQL和eclipse
(一)Eclipse新建项目
1.打开Eclipse
2.新建项目
点击File,然后点击new新建一个java project
3.项目命名
建一个名为T1的项目
点击finish
4.创建成功
(二)导入Mysql的包
1.右击T1选择Bulid Path --Add External Archiyes
2.引入对应mysql的jar包
3.导入成功
二、建立数据库并查询数据
(一)新建mysql数据库
1.打开Navicat并新建连接
连接名为T
2.测试链接
然后点击确定
3.新建数据库
右击连接T新建名为t数据库
点击确定
查看数据库是否成功
4.新建查询
5.创建表
CREATE TABLE `websites` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
`url` varchar(255) NOT NULL DEFAULT '',
`alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
`country` char(10) NOT NULL DEFAULT '' COMMENT '国家',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
并运行
6.添加数据
INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', '淘宝', 'https://www.taobao.com/', '13', 'CN'), ('3', '菜鸟教程', 'http://www.runoob.com', '5892', ''), ('4', '微博', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
7. 查看数据
(二)JDBC 连接MySQL 数据库并查询数据
1.新建包t
右击src
点击finish
2.新建类
右击t新建一个类
3.代码
package t;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
public class MYSQLDemo {
static final String driverName="org.gjt.mm.mysql.Driver";
static final String dbUrl="jdbc:mysql://localhost:3306/t";
static final String userName="root";
static final String password="123456";
public static void main(String[] args) {
// TODO Auto-generated method stub
// MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
Connection conn = null;
Statement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName(driverName);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(dbUrl,userName,password);
// 执行查询
System.out.println(" 实例化Statement对象...");
stmt = (Statement) conn.createStatement();
String sql;
sql = "SELECT id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while(rs.next()){
// 通过字段检索
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url");
// 输出数据
System.out.print("ID: " + id);
System.out.print(", 站点名称: " + name);
System.out.print(", 站点 URL: " + url);
System.out.print("\n");
}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
4.执行代码并显示结果
参考文献:Java MySQL 连接 | 菜鸟教程
边栏推荐
- 如何开启多语言文本建议?Win11打开多语言文本建议的方法
- 把腾讯搬上云:云服务器 CVM 的半部进化史
- 【历史上的今天】6 月 13 日:分组交换网路的“亲子纠纷”;博弈论创始人出生;交互式电视初现雏形
- 【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze
- Exploration on the construction path of real-time digital warehouse integrating digital intelligence learning and streaming batch
- Livedata interview question bank and answers -- 7 consecutive questions for livedata interview~
- 畢業總結
- [today in history] May 31: the father of Amiga was born; The co developer of basic language was born; BlackBerry BBM shutdown
- 字节跳动面试官:一张图片占据的内存大小是如何计算
- Complex and inefficient logistics? Three steps to solve problems in enterprise administration
猜你喜欢
Starting sequence of Turing machine
Desai wisdom number - histogram (column folding mixed graph): ratio of rental price to rental income in the graduation quarter of 2021
【历史上的今天】6 月 19 日:iPhone 3GS 上市;帕斯卡诞生;《反恐精英》开始测试
Opencv -- Hough transform and some problems encountered
win11如何添加打印机和扫描仪?win11添加打印机和扫描仪的设置
[JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes
[today in history] June 1: Napster was founded; MS-DOS original author was born; Google sells Google SketchUp
把腾讯搬上云:云服务器 CVM 的半部进化史
How technicians become experts in technical field
【历史上的今天】5 月 29 日:共享软件先驱诞生;ChromeBox 推出;VoodooPC 创始人出生
随机推荐
简单ELK配置实现生产级别的日志采集和查询实践
Prometheus 2.27.0 new features
数仓的字符截取三胞胎:substrb、substr、substring
OSI 7层模型讲解(大白话 通俗易懂)
在线JSON转PlainText工具
Leetcode topic [array] -228- summary interval
[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
Step by step interpretation of crf+bilstm code
云原生(三十) | Kubernetes篇之应用商店-Helm
Win11不能拖拽圖片到任務欄軟件上快速打開怎麼辦
NER中BiLSTM-CRF解读Forward_algorithm
[today in history] May 31: the father of Amiga was born; The co developer of basic language was born; BlackBerry BBM shutdown
Opencv——几何空间变换(仿射变换和投影变换)
【历史上的今天】6 月 6 日:世界 IPv6 启动纪念日;《俄罗斯方块》发布;小红书成立
Architecture high reliability application knowledge map ----- microservice architecture map
【二維碼圖像矯正增强】基於MATLAB的二維碼圖像矯正增强處理仿真
[inverted pendulum control] Simulink simulation of inverted pendulum control based on UKF unscented Kalman filter
【历史上的今天】6 月 20 日:MP3 之父出生;富士通成立;谷歌收购 Dropcam
数智学习|湖仓一体实践与探索