当前位置:网站首页>JDBC MySQL基本操作
JDBC MySQL基本操作
2022-07-22 18:43:00 【lzhNox】
JDBC基本操作(增删改查)
首先,JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
在这我们使用的是MySQL,所以我们需要一个驱动包,帮助java与mysql服务器建立联系
操作步骤:
public static void main(String[] args) {
//1.描述数据库位置
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("760121");
}
解释一下setURL中的字符串
jdbc:mysql --------这个是协议名,这个网址就是java连接mysql用的
127.0.0.1 ---------这个是ip地址,描述主机位置,该ip地址为特殊地址,代指本机
3306 --------------这个是端口号,用于区分该主机上的哪个程序,3306是mysql服务器默认的端口号
characterEncoding=utf8 ----------用于设置客户端连接服务器使用的字符集,在这设置为utf8
useSSL=false ----------设置是否要加密,在这设置为false
续:
//1.描述数据库位置
public static void main(String[] args) {
//1.描述数据库位置
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("760121");
//2.建立数据库连接
Connection connection = dataSource.getConnection(); //注意这里的的Connection类使用java.sql内提供的类
//3.构造sql语句
String sql = "写sql";
PrepareStatement prepareStatement = connection.prepareStatement(sql);
//4.执行sql语句
int n=prepareStatement.excuteUpdate();
/在这,增删改操作使用excuteUpdate,查询操作需使用excuteQuery
//5.断开连接
connection.close();
preparedStatement.close();
}
边栏推荐
- OWA邮件系统登录双因子认证(短信认证)方案
- 【黄啊码】MySQL入门—3、我用select *,老板直接赶我坐火车回家去,买的还是站票
- MNIST数据集
- Transformer
- What if win11 uses CAD jam or fatal error? Win11 cannot use CAD normally
- Stack overflow basic exercise question - 3 (with a comparison of 32 and 64 bit differences)
- Conditions affecting interface query speed
- 华为首席开源联络官任旭东:深耕基础软件开源,协同打造数字世界根技术
- R语言绘制日历热图
- Linux环境下oracle切换用户并查询数据库命令
猜你喜欢

Stack overflow basic exercise - 5 (string vulnerability)

中国开源走向第二梯队!

centos7安装和卸载mysql5.7

Intel(中国)云基础设施软件研发总监王庆:Intel在云原生里的技术发展和展望

After daily bookkeeping, the project chart shows various revenue and expenditure categories

Evolution Atlas of interface documents. People who have used the first interface document tool are exposed to their age

影响接口查询速度的情况

重磅!《2022中国开源发展蓝皮书》正式发布

pwn1_ sctf_ two thousand and sixteen

How to configure a cute little shark theme for typera?
随机推荐
JS complex data type
剑指offer专项突击版第7天
NLP language model
兆易创新GD25WDxxK6 SPI NOR Flash产品系列问世
NLP学习路线图(思维导图),非常的全面和清晰!
中国工程院院士倪光南:拥抱开源 与世界协同创新
Zhang Li, President of China Electronic Information Industry Development Research Institute: building China's leading open source value chain
PWN stack overflow basic exercise - 1
Transformer
Brief analysis of mobile app security testing of software testing, shared by Beijing third-party software testing institutions
Stack overflow basic exercise - 5 (string vulnerability)
R语言箱线图添加 t.test 显著性-
Centos7 installing and uninstalling mysql5.7
[foundation 2] - container
DB207-ASEMI整流桥一般用在什么地方,DB207参数尺寸
微信小程序开发:第一个helloWorld
Linear regression and logical regression and their implementation
Pytoch realizes text emotion analysis
[foundation 3] - structure and function
2019_ IJCAI_ Adapting BERT for Target-Oriented Multimodal Sentiment Classification