当前位置:网站首页>JDBC连接数据库详细步骤
JDBC连接数据库详细步骤
2022-07-29 05:19:00 【学习记录而已】
一、先创建一个项目
二、打开数据库并启动连接

(我是在IDEA中写的,提前导包)
三、不会导包就在Maven中导,在pom.xml中的dependencise放,版本号看自己要高版本还是低版本。放进去然后可以刷新重新加载一下让maven自己下依赖。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>

详细代码:
package com.example.dem02; //项目包名
import java.sql.*;
public class jdbcText01 {
public static void main(String[] args){
Connection conn = null;
Statement stmt = null;
try{
//1、注册驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Class.forName("com.mysql.jdbc.Driver");
//2、获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
//3、获取数据库操作对象
stmt = conn.createStatement();
//4、执行sql语句
String sql = "INSERT INTO class(Sid,Sname,Sage,Sgrade)VALUES ('003','小青','19','89')";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "更新成功": "更新失败");
//5、处理项目结果集
}catch (SQLException e){
e.printStackTrace();
}finally {
//6、释放资源
if (stmt != null){
try{
stmt.close();
}catch(SQLException e ){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
边栏推荐
- Character type conversion
- Sqlmap是什么以及使用方法
- 2022 mathematical modeling competition summer training lecture - optimization method: goal planning
- sql-server 数据表的简单操作
- Selenium实战案例之爬取js加密数据
- table中同一列中合并相同项
- Clickhouse learning (VIII) materialized view
- Day 1
- Hcia-r & s self use notes (27) comprehensive experiment
- [sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
猜你喜欢

DAY6:利用 PHP 编写文件上传页面

记xx公司SQL注入漏洞

Record the SQL injection vulnerability of XX company

Liang Yuqi, founder of aitalk: the link between image and virtual reality

What is wapiti and how to use it

Hcia-r & s self use notes (25) NAT technical background, NAT type and configuration

What is sqlmap and how to use it
![[typescript] in depth study of typescript functions](/img/0c/e838960c8efd6e87046d35f8942a07.png)
[typescript] in depth study of typescript functions

Li Kou 994: rotten orange (BFS)

DAY15(DAY16拓展):文件包含漏洞
随机推荐
Masscan使用教程.
Set the background color of a cell in the table
Sqlmap是什么以及使用方法
uniapp页面标题显示效果
Clickhouse learning (VIII) materialized view
[JS question solution] questions 1-10 in JS of niuke.com
Masscan tutorial
uniapp组件之倒计时(如阅读协议倒计时、完成学习倒计时)
HCIA-R&S自用笔记(24)ACL
弹性盒子flex
table中同一列中合并相同项
Flask 报错 RuntimeError: The session is unavailable because no secret key was set.
SQL修复重复数据
Wechat applet video upload component is directly uploaded to Alibaba cloud OSS
Display effect of uniapp page title
Clickhouse learning (x) monitoring operation indicators
shell基本操作(下)
Laravel服务容器(上下文绑定的运用)
Clickhouse learning (VI) grammar optimization
Seay source code audit system