当前位置:网站首页>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
边栏推荐
- 【倒立摆控制】基于UKF无迹卡尔曼滤波的倒立摆控制simulink仿真
- Digital intelligence learning Lake Warehouse Integration Practice and exploration
- Online text batch inversion by line tool
- 11 timers for STM32F103
- Interview: how do lists duplicate objects according to their attributes?
- [today in history] June 11: the co inventor of Monte Carlo method was born; Google launched Google Earth; Google acquires waze
- "Dadao Zhichuang" won a ten million prea+ round of financing and launched a technology consumption robot
- [today in history] June 15: the first mobile phone virus; AI master simahe was born; Chromebook launch
- Design e-commerce seckill system
- MFC常用 当前路径
猜你喜欢

如何系统学习LabVIEW?

What if win11 can't drag an image to the taskbar software to open it quickly

初始线性回归

Online JSON to plaintext tool

【历史上的今天】6 月 12 日:美国进入数字化电视时代;Mozilla 的最初开发者出生;3Com 和美国机器人公司合并
![[inverted pendulum control] Simulink simulation of inverted pendulum control based on UKF unscented Kalman filter](/img/17/3c93ddf8f55c63a97480a78cbf8111.png)
[inverted pendulum control] Simulink simulation of inverted pendulum control based on UKF unscented Kalman filter

What if win11 cannot use dynamic wallpaper? Solution of win11 without dynamic wallpaper

Unity WebGL打包后怎么运行(火狐配置)

把腾讯搬上云:云服务器 CVM 的半部进化史

【历史上的今天】6 月 3 日:微软推出必应搜索引擎;Larry Roberts 启动阿帕网;Visual Basic 之父出生
随机推荐
《天天数学》连载53:二月二十一日
[today in history] June 3: Microsoft launched Bing search engine; Larry Roberts starts ARPANET; The father of Visual Basic was born
Interview: how do lists duplicate objects according to their attributes?
迪赛智慧数——柱状图(折柱混合图):2021年毕业季租房价格和房租收入比
isEmpty 和 isBlank 的用法區別
【二维码图像矫正增强】基于MATLAB的二维码图像矫正增强处理仿真
[today in history] June 17: the creator of the term "hypertext" was born; The birth of Novell's chief scientist; Discovery channel on
新手炒股开户选哪家证券平台办理是最好最安全的
面试:List 如何根据对象的属性去重?
Différences d'utilisation entre IsEmpty et isblank
Solutions to st link USB communication error
[today in history] June 6: World IPv6 launch anniversary; Tetris release; Little red book established
Practice of low code DSL in data warehouse
基于STM32的编写
NER中BiLSTM-CRF解读Forward_algorithm
Livedata interview question bank and answers -- 7 consecutive questions for livedata interview~
毕业总结
Stm32f1 interrupt introduction
StaticLayout的使用详解
STM32的C语言与汇编语言混合编程