当前位置:网站首页>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 连接 | 菜鸟教程
边栏推荐
- 面试:Bitmap像素内存分配在堆内存还是在native中
- Character interception triplets of data warehouse: substrb, substr, substring
- JS implementation of Slide Puzzle verification
- Flask基础:模板渲染+模板过滤使用+控制语句
- Stm32f1 and stm32subeide programming example - metal touch sensor driver
- [today in history] June 1: Napster was founded; MS-DOS original author was born; Google sells Google SketchUp
- [today in history] June 10: Apple II came out; Microsoft acquires gecad; The scientific and technological pioneer who invented the word "software engineering" was born
- 榜单首发——前装搭载率站上10%大关,数字钥匙方案供应商TOP10
- Résumé de la graduation
- MySQL collection, here are all the contents you want
猜你喜欢
![[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB

Cloud native (30) | kubernetes' app store Helm

Stm32f1 and stm32subeide programming example - metal touch sensor driver

Practice of low code DSL in data warehouse

数据清洗工具flashtext,效率直接提升了几十倍数

The graduation season is coming, and the number of college graduates in 2022 has exceeded 10 million for the first time
![[today in history] June 2: Apple launched swift programming language; China Telecom acquires China Unicom C network; OS X Yosemite release](/img/24/58c4ee72e067f01a4c4aa57a1cf61a.jpg)
[today in history] June 2: Apple launched swift programming language; China Telecom acquires China Unicom C network; OS X Yosemite release

High reliability application knowledge map of Architecture -- the path of architecture evolution

Flask基础:模板渲染+模板过滤使用+控制语句

如何系统学习LabVIEW?
随机推荐
[inverted pendulum control] Simulink simulation of inverted pendulum control based on UKF unscented Kalman filter
如何判断线程池已经执行完所有任务了?
[fuzzy neural network] simulation of fuzzy neural network based on MATLAB
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
在线文本按行批量反转工具
Truth table of common anode digital tube
Mysql数据库基础:DML数据操作语言
Complex and inefficient logistics? Three steps to solve problems in enterprise administration
「大道智创」获千万级preA+轮融资,推出科技消费机器人
isEmpty 和 isBlank 的用法区别
Win11新建不了文本文档?Win11右键无法新建文本文档的解决方法
Exploration on the construction path of real-time digital warehouse integrating digital intelligence learning and streaming batch
【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
win11如何添加打印机和扫描仪?win11添加打印机和扫描仪的设置
【倒立摆控制】基于UKF无迹卡尔曼滤波的倒立摆控制simulink仿真
【历史上的今天】6 月 17 日:术语“超文本”的创造者出生;Novell 首席科学家诞生;探索频道开播
Opencv -- Hough transform and some problems encountered
【 amélioration de la correction d'image de Code bidimensionnel】 simulation du traitement d'amélioration de la correction d'image de Code bidimensionnel basée sur MATLAB
Unity WebGL打包后怎么运行(火狐配置)
[JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes