当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
猜你喜欢

Clickhouse learning (V) cluster operation

QPalette学习笔记

Qt设置背景图片方法

Do students in the science class really understand the future career planning?

Qt布局管理--部件拉伸(Stretch)原理及大小策略(sizePolicy)

使用Qss设置窗体样式
![[C language series] - print prime numbers between 100 and 200](/img/61/5b9dea72e7a3fd450a87fe35fb94eb.png)
[C language series] - print prime numbers between 100 and 200

Laravel服务容器(上下文绑定的运用)

Win10 搭建MSYS2环境

Playwright实战案例之爬取js加密数据
随机推荐
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
Detailed explanation of typical application code of C language array - master enters by mistake (step-by-step code explanation)
DAY15(DAY16拓展):文件包含漏洞
[C language series] - constants and variables that confuse students
Clickhouse learning (V) cluster operation
弹性盒子相关知识
Clickhouse learning (XI) clickhouseapi operation
365 day challenge leetcode1000 question - day 036 binary tree pruning + subarray and sorted interval sum + delete the shortest subarray to order the remaining arrays
[typescript] type reduction (including type protection) and type predicate in typescript
What is nmap and how to use it
微信小程序-组件传参,状态管理
[C language series] - print prime numbers between 100 and 200
基础爬虫实战案例之获取游戏商品数据
Thinkphp6管道模式Pipeline使用
AR虚拟增强与现实
Clickhouse learning (IV) SQL operation
Record the SQL injection vulnerability of XX company
Fvuln automated web vulnerability detection tool
QPalette学习笔记
Clickhouse learning (VII) table query optimization