当前位置:网站首页>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 连接 | 菜鸟教程
边栏推荐
- [today in history] May 29: the pioneer of sharing software was born; Chromebox launched; VoodooPC founder was born
- MySQL query related knowledge (advanced level 7: sub query
- Mysql database foundation: DML data operation language
- 数智学习 | 流批一体实时数仓建设路径探索
- Win11如何关闭最近打开项目?Win11关闭最近打开项目的方法
- "Dadao Zhichuang" won a ten million prea+ round of financing and launched a technology consumption robot
- 【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用
- 【历史上的今天】6 月 5 日:洛夫莱斯和巴贝奇相遇;公钥密码学先驱诞生;函数语言设计先驱出生
- How to enable multi language text suggestions? Win11 method to open multilingual text suggestions
- [elevator control system] design of elevator control system based on VHDL language and state machine, using state machine
猜你喜欢
【历史上的今天】6 月 20 日:MP3 之父出生;富士通成立;谷歌收购 Dropcam
[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
【历史上的今天】6 月 1 日:Napster 成立;MS-DOS 原作者出生;谷歌出售 Google SketchUp
Win11 cannot create a new text document? Solution to win11 right click failure to create a new text document
被通知裁员后拿到5个offer
[JS reverse hundreds of examples] I love to solve 2022 Spring Festival problems and receive red envelopes
如何开启多语言文本建议?Win11打开多语言文本建议的方法
SQL injection bypass (IV)
[today in history] June 8: the father of the world wide web was born; PHP public release; IPhone 4 comes out
[block coding] simulation of image block coding based on MATLAB
随机推荐
SQL injection bypass (IV)
MySQL query related knowledge (advanced level 7: sub query
Data governance and data standards
【历史上的今天】6 月 15 日:第一个手机病毒;AI 巨匠司马贺诞生;Chromebook 发布
【电梯控制系统】基于VHDL语言和状态机实现的电梯控制系统的设计,使用了状态机
Exploration on the construction path of real-time digital warehouse integrating digital intelligence learning and streaming batch
[today in history] June 5: Lovelace and Babbage met; The pioneer of public key cryptography was born; Functional language design pioneer born
Solutions to st link USB communication error
数据清洗工具flashtext,效率直接提升了几十倍数
Step by step interpretation of crf+bilstm code
[today in history] May 29: the pioneer of sharing software was born; Chromebox launched; VoodooPC founder was born
Différences d'utilisation entre IsEmpty et isblank
math_(函数&数列)极限的含义&误区和符号梳理/邻域&去心邻域&邻域半径
SQL injection bypass (V)
"Dadao Zhichuang" won a ten million prea+ round of financing and launched a technology consumption robot
LeetCode - Easy - 197
Interpretation of bilstm-crf in NER forward_ algorithm
win11如何添加打印机和扫描仪?win11添加打印机和扫描仪的设置
启牛开户安全吗?怎么线上开户?
MySQL优化小技巧