当前位置:网站首页>Database connection operations for MySQL and MyEclipse
Database connection operations for MySQL and MyEclipse
2022-08-09 10:28:00 【Zuo Mingshui】
1. The MySQL database and MyEclipse software have been installed correctly.
2. I set up the database school (name) and table (StudentInfo) under the doc
After entering the doc: refer to the relevant code
show databases;
Show all databases
exit;
Launch doc
create database School;
Create a database
use School;
Enter database
create table StudentInfo(
name char(10),
age char(3));
Create table
insert StudentInfo(name,age)
VALUES('David','18');
Insert data
select from StudentInfo;
Query
update StudentInfo set age='12' where name='HAHA';
Update data
insert into studentInfo (name,age)values('Jingzhu',20);
Insert data
delete from StudentInfo where age='20';
Delete data
3. Create a Javaproject (named Show) in MyEclipsemysql-connector-java-5.0.8-bin.jar in the downloaded JdbC compressed package)
Fourth, create a class (named Show) and related test code as follows:
import java.sql.;
public class Show
{
public static void main(String[] args)
{
try
{
String url="jdbc:mysql://localhost:3306/school";//school is the name of the database to be connected
String user="root";
Stringpwd="root";
//Load mysql driver, this sentence can also be written as: Class.forName("com.mysql.jdbc.Driver").newInstance();Class.forName("com.mysql.jdbc.Driver");//Call the method of the DriverManager object to obtain a Connection object, representing an open connection.Establish a connection with MySQL.Connection conn = DriverManager.getConnection(url, user, pwd);//Use the method of the Connection interface to create a Statement statement object, which is used to pass a simple SQL statement without parameters to the database management system for execution.Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("select * from studentInfo");// process the result setwhile (rs.next()){String name = rs.getString("name");System.out.print("name-----"+name);String age=rs.getString("age");System.out.println("age------"+age);}//close the connectionrs.close();stmt.close();conn.close();}catch (Exception ex){System.out.println("Error : " + ex.toString());}
}
}
5. Running results.
name-----HAHA age------12
name-----David age------18
If the result is as follows, the connection to the database is successful.
6. Follow-up questions?
How to solve the problem of garbled characters in the running result?
name-----HAHA age------12
边栏推荐
- Demand side power load forecasting (Matlab code implementation)
- LeetCode(剑指 Offer)- 25. 合并两个排序的链表
- 【MySQL】mysql因为字符集导致left join出现Using join buffer (Block Nested Loop)
- 2021-01-11-雪碧图做表情管理器
- Tom Morgan | Twenty-One Rules of Life
- By asking where the variables are stored, the shepherd boy laughed and said to use pointers, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the use of go lang type pointers (Poin
- 常用语言图表库总结
- 对话跨国消费品牌DPO:数据安全合规从何做起?8.11直播见!
- basic operator
- mongodb学习笔记
猜你喜欢
随机推荐
机器学习--线性回归(Linear Regression)
Tom Morgan | Twenty-One Rules of Life
使用cpolar远程连接群晖NAS(创建临时链接)
面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不上来!
unix环境编程 第十五章 15.7消息队列
判断一段文字的width
写一个通讯录小程序
公里周日
1002 写出这个数 (20 分)
unix环境编程 第十五章 15.10 POSIX信号量
如何快速打通镜像发布流程?
实现下拉加载更多
拿下跨界C1轮投资,本土Tier 1高阶智能驾驶系统迅速“出圈”
【size_t是无符号整数 (-1 > 10) -> 1】
TELNET协议相关RFC
【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
Win7 远程桌面限制IP
虚拟列表key复用问题
基本运算符
踩坑scrollIntoView