当前位置:网站首页>JDBC的连接
JDBC的连接
2022-07-26 20:00:00 【Camellia——】
package com.lin.jdbc_01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/*
* JDBC基本入门使用
* 1)加载驱动
* 2)获取数据库连接对象
* 3)准备sql语句
* 4)获取执行对象
* 5)执行对象sql语句
* 6)返回结果
* 7)释放
* */
public class JDBCDemo {
public static void main(String[] args) throws Exception {
// 1)加载数据库驱动:com.mysql.jdbc:驱动类底层实现的是sun公司Driver接口
Class.forName("com.mysql.jdbc.Driver");
// 2)获取数据库连接对象
/*
public static Connection getConnection
(String url,String user,String password)throws SQLException
参数1:url:连接数据库的url地址(统一资源定位符)"jdbc:mysql://localhost:3306/数据库名"
参数2:连接数据库的用户名root
参数3:连接数据库的密码
*/
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb_02", "root", "123456");
// 3)准备好sql语句
String sql="update emp set salary=salary+500 where id=6;";
// 4)通过数据库连接对象获取执行对象
// Statement createStatement()throws SQLException 获取执行对象并将sql发送到数据库
// Statement:操作静态SQL语句
Statement stmt=conn.createStatement();
// 5)通过执行对象执行sql语句
// int executeUpdate(String sql)
int count=stmt.executeUpdate(sql);
System.out.println("更新成功:"+count);
// 6)释放资源
stmt.close();//关闭执行对象
conn.close();//关闭连接对象
}
}

边栏推荐
- Easy gene | introduction to macrovirus sequencing technology
- SSM整合实例
- leetcode 哈希表类
- Face recognition and fingerprint recognition are weak? Pentagon develops long-distance heartbeat recognition
- 2022 pole technology communication - anmou technology opens a new chapter of commercialization
- BUU刷题记1
- Bean injection and lifecycle
- Green and sustainable development of data center
- 数据块的存储系统中缓存的作用是什么?
- 创建一个自身类的静态对象变量,究竟会如何执行?
猜你喜欢

Buu brush inscription 3

serializable接口的作用是什么?

SSM整合实例
![Shell函数、系统函数、basename [string / pathname] [suffix] 可以理解为取路径里的文件名称 、dirname 文件绝对路径、自定义函数](/img/3d/d7276d2010f1d77a3bd572cc66eced.png)
Shell函数、系统函数、basename [string / pathname] [suffix] 可以理解为取路径里的文件名称 、dirname 文件绝对路径、自定义函数

Kotlin - coroutinecontext

Bean注入和生命周期

Experiment 6 BGP federal comprehensive experiment

BTC和ETH不确定性增强 因加息逼近?美国经济将面临更多痛苦

消息队列——引入的问题:重复消费&顺序消费&分布式事务

营销与销售文件管理以及工作流程解决方案
随机推荐
twenty million two hundred and twenty thousand seven hundred and twenty-six
Sword finger offer46 translates numbers into strings
连接池快速入门
【问题篇】将集合[‘‘,‘‘]处理成(‘‘,‘‘)
Execution context and Lexical Environment
【面试必刷101】动态规划1
【PyQt5基本控件使用解析】
Solve the horizontal segmentation of iBGP and BGP routing principles
全球最聪明50家公司公布:中国厂商占据近半,华为名列第一
Leetcode刷题之——链表总结
每日练习------有一组学员的成绩,将它们按降序排列,要增加一个学员的成绩,将它插入成绩序列,并保持降序
「企业管理」精诚CRM+——一体化管理企业业务流程
李彦宏遭“泼冷水”热情不减!百度结盟华为麒麟,发布“鸿鹄”芯片
Establishment of APP automation testing framework (VIII) -- establishment of ATX server2 multi device cluster environment
The 50 Smartest Companies in the world announced that Chinese manufacturers account for nearly half, and Huawei ranks first
Auto. JS rotation Icon
What is the origin of CNEX labs, which let Huawei lose the lawsuit?
Do employees have to compensate the company for losses when they resign? The 34 year old captain resigned and was claimed 10.66 million yuan by the company
What is the role of cache in the storage system of data blocks?
Leetcode's question brushing -- List summary