当前位置:网站首页>JDBC and MySQL databases
JDBC and MySQL databases
2022-06-28 02:50:00 【m0_ sixty-one million eight hundred and eleven thousand three h】
Catalog
Two 、 Establish database and query data
( One ) newly build mysql database
( Two )JDBC Connect MySQL Database and query data
One 、JDBC Connect MySQL
Install first. MySQL and eclipse
( One )Eclipse New projects
1. open Eclipse

2. New projects
Click on File, And then click new Create a new one java project


3. Project named
Build one called T1 Project

Click on finish
4. Create success

( Two ) Import Mysql My bag
1. Right click T1 choice Bulid Path --Add External Archiyes

2. Import correspondence mysql Of jar package

3. Successful import

Two 、 Establish database and query data
( One ) newly build mysql database
1. open Navicat And create a new connection

The connection name is T

2. Test link

Then click OK
3. New database
Right click Connect T New name is t database


Click ok
Check whether the database is successful

4. New query

5. Create table
CREATE TABLE `websites` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL DEFAULT '' COMMENT ' Site name ',
`url` varchar(255) NOT NULL DEFAULT '',
`alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa ranking ',
`country` char(10) NOT NULL DEFAULT '' COMMENT ' Country ',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
And run


6. Add data
INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', ' TaoBao ', 'https://www.taobao.com/', '13', 'CN'), ('3', ' Novice tutorial ', 'http://www.runoob.com', '5892', ''), ('4', ' Microblogging ', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');

7. View the data

( Two )JDBC Connect MySQL Database and query data
1. New package t
Right click src


Click on finish
2. The new class
Right click t Create a new class


3. Code
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 The following versions - JDBC Driver name and database URL
Connection conn = null;
Statement stmt = null;
try{
// register JDBC drive
Class.forName(driverName);
// Open the link
System.out.println(" Connect to database ...");
conn = DriverManager.getConnection(dbUrl,userName,password);
// Execute the query
System.out.println(" Instantiation Statement object ...");
stmt = (Statement) conn.createStatement();
String sql;
sql = "SELECT id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql);
// Expand the result set database
while(rs.next()){
// Retrieve... By fields
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url");
// Output data
System.out.print("ID: " + id);
System.out.print(", Site name : " + name);
System.out.print(", Site URL: " + url);
System.out.print("\n");
}
// Close... When done
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// Handle JDBC error
se.printStackTrace();
}catch(Exception e){
// Handle Class.forName error
e.printStackTrace();
}finally{
// close resource
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// Don't do anything?
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}4. Execute the code and display the results

reference :Java MySQL Connect | Novice tutorial
边栏推荐
- 【历史上的今天】6 月 18 日:京东诞生;网店平台 Etsy 成立;Facebook 发布 Libra 白皮书
- [today in history] June 7: kubernetes open source version was released; Worldofwarcraft landed in China; Birth of the inventor of packet switching network
- 新手炒股开户选哪家证券平台办理是最好最安全的
- Win11不能拖拽圖片到任務欄軟件上快速打開怎麼辦
- 【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze
- flask基础:模板继承+静态文件配置
- Opencv -- Hough transform and some problems encountered
- LiveData 面试题库、解答---LiveData 面试 7 连问~
- 面试:Bitmap像素内存分配在堆内存还是在native中
- [2D code image correction and enhancement] simulation of 2D code image correction and enhancement processing based on MATLAB
猜你喜欢
![抓包整理外篇fiddler————了解工具栏[一]](/img/5f/24fd110a73734ba1638f0aad63c787.png)
抓包整理外篇fiddler————了解工具栏[一]

Intel Ruixuan A380 graphics card will be launched in China

数据清洗工具flashtext,效率直接提升了几十倍数
![[today in history] June 10: Apple II came out; Microsoft acquires gecad; The scientific and technological pioneer who invented the word](/img/0d/9f99eb3dcb73c912987b81fba71890.png)
[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

英特尔锐炫A380显卡即将在中国面市

【历史上的今天】6 月 1 日:Napster 成立;MS-DOS 原作者出生;谷歌出售 Google SketchUp

Win11 ne peut pas faire glisser l'image sur le logiciel de la barre des tâches

Online text batch inversion by line tool

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

MySQL optimization tips
随机推荐
Différences d'utilisation entre IsEmpty et isblank
Keil "St link USB communication error" solution
一种低成本增长私域流量,且维护简单的方法
毕业总结
榜单首发——前装搭载率站上10%大关,数字钥匙方案供应商TOP10
【二維碼圖像矯正增强】基於MATLAB的二維碼圖像矯正增强處理仿真
MFC common current path
[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
Shardingsphere-proxy-5.0.0 establish MySQL read / write separation connection (6)
[block coding] simulation of image block coding based on MATLAB
Win11无法使用动态壁纸怎么办?Win11用不了动态壁纸的解决方法
如何判断线程池已经执行完所有任务了?
Digital intelligence learning Lake Warehouse Integration Practice and exploration
Step by step interpretation of crf+bilstm code
【 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
What if win11 can't drag an image to the taskbar software to open it quickly
Snake C language
NER中BiLSTM-CRF解读Forward_algorithm
【模糊神经网络】基于matlab的模糊神经网络仿真
【方块编码】基于matlab的图像方块编码仿真