当前位置:网站首页>MySQL——JDBC
MySQL——JDBC
2022-08-09 21:54:00 【Xiaolock830】
我们可以利用官方为java编写的数据库驱动包,利用idea来操作MySQL数据库
概念
其全称是java database connectivity,也就是提供了一系列接口,使java程序和Mysql连接,让sql语句在java中执行
准备
我们需要去下面这个网址下载数据库驱动包
https://mvnrepository.com/artifact/mysql/mysql-connector-java
选择的版本号需要和我们的Mysql的版本号相同,例如8.0的就选择8系列的
然后将这个jar包拷贝,创建一个新的项目,和一个文件夹

然后右键点击项目名称,点击Open Module Settings
然后点击Dependencies
然后点击加号,导入我们的lib文件夹的jar包

这样我们就配置好我们的项目了,接下来就可以编写代码了
代码
通过一下代码,可以连接到我们的数据库
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class test {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
}
}
前面的代码只要使用的java项目和自己的MySQL在同一台机器上,并且没有改过MySQL的默认属性,就是一样的代码,唯一不一样的是password是我们MySQL的密码,替换即可
插入操作
public class TestJDBC {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
String sql = "insert student values(1,'张三')";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
int row = preparedStatement.executeUpdate();
System.out.println(row);
preparedStatement.close();
connection.close();
}
}
需要注意的是,用完资源后,应该断开和数据库的连接
查询操作
public class TestJDBCSelect {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSl=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
String sql = "select * from student";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("id = " + id + " name = "+ name);
}
resultSet.close();
preparedStatement.close();
connection.close();
}
}
和插入操作不一样,更新操作是用这条语句来使操作生效
preparedStatement.executeQuery();
删除操作
public class TestJDBCDelete {
public static void main(String[] args) throws SQLException {
Scanner scanner = new Scanner(System.in);
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
System.out.println("input the id which you want to delete");
int id = scanner.nextInt();
String sql = "delete from student where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
int ret = statement.executeUpdate();
System.out.println("ret = " + ret);
statement.close();
connection.close();
}
}
边栏推荐
- Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
- Leetcode 93 复原IP地址
- np中的round函数,ceil函数与floor函数
- 4D Summary: 38 Knowledge Points of Distributed Systems
- 小程序+自定义插件的关键性
- OpenMLDB + Jupyter Notebook:快速搭建机器学习应用
- 国内手机厂商曾为它大打出手,如今它却最先垮台……
- Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool
- TF生成均匀分布的tensor
- In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture
猜你喜欢

肝通宵写了三万字把SQL数据库的所有命令,函数,运算符讲得明明白白讲解,内容实在丰富,建议收藏+三连好评!

How do task flow executors work?

STC8H开发(十五): GPIO驱动Ci24R1无线模块

In-depth analysis of Apache EventMesh cloud-native distributed event-driven architecture

Reinforcement Learning Weekly Issue 57: DL-DRL, FedDRL & Deep VULMAN

Flask之路由(app.route)详解

Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool

【微服务~Nacos】Nacos之配置中心

在“企业通讯录”的盲区,融云的边界与分寸

大型分布式存储方案MinIO介绍,看完你就懂了!
随机推荐
[Microservice~Nacos] Nacos service provider and service consumer
从源码方面来分析Fragment管理中 Add() 方法
Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
UML类图五种关系的代码实现[通俗易懂]
Bean life cycle
Rust 解引用
One Pass 2074: [21CSPJ Popularization Group] Candy
第十七期八股文巴拉巴拉说(数据库篇)
SecureCRT background color
Presto Event Listener开发
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
String hashing (2014 SERC J question)
基于ABP的AppUser对象扩展
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
从产品角度看 L2 应用:为什么说这是一个游乐场?
ACM MM 2022 | Cloud2Sketch: Painting with clouds in the sky, AI brush strokes
Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
js十五道面试题(含答案)
Interviewer: How to deal with Redis big key?
一本通2074:【21CSPJ普及组】分糖果(candy)