| |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| -> 大数据 -> JDBC-1026-day01 -> 正文阅读 |
|
|
[大数据]JDBC-1026-day01 |
|
JDBC(Java DataBase Connectivity) Java数据库连接 1,注册数据库驱动 Class.forName("路径.Driver"); 2,获取数据库连接 Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hc_db?characterEncoding=utf8", "账户名是啥写啥", "密码"); 3,获取传输器 Statement stat = conn.createStatement(); 4,发送SQL到服务器执行并返回执行结果 String sql = "SELECT * FROM account"; ResultSet rs = stat.executeQuery(sql); 5,处理结果 while(rs.next()){ Integer id = rs.getInt("id"); String name = rs.getString("name"); Double salary = rs.getDouble("money"); System.out.println(id + ", " + name + ", " + salary); } 6,释放资源 rs.close(); stat.close(); conn.close(); System.out.println("释放资源完毕!"); 代码如下1、新增:往account表中添加一个名称为王飞、money为10000的记录 2、修改:将account表中名称为wmm的记录,money修改为15000 3、查询:查询account表中名称为wmm的记录 4、删除:删除account表中名称为"王飞"的记录
/**
* @author RogerJiang
* 2021/10/26 14:41
*/
public class JDBCDemo02 {
public static void main(String[] args) {
testInsert();
testUpdate();
testAccount();
testDelet();
}
public static void testInsert() {
Connection connection =null;
Statement statement =null;
try {
connection = JDBCUtil.getConnection();
statement =connection.createStatement();
String sql ="insert into account values(null,'王飞',10000.0)";
int rows= statement.executeUpdate(sql);
if (rows ==1){
System.out.println("数据保存成功");
}else{
System.out.println("保存失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
// if (statement !=null){
// try {
// statement.close();
// } catch (SQLException throwables) {
// throwables.printStackTrace();
// }
// }
// if (connection !=null){
// try {
// connection.close();
// } catch (SQLException throwables) {
// throwables.printStackTrace();
// }
// }
JDBCUtil.close(null,statement,connection);
}
}
public static void testUpdate() {
Connection connection=null;
Statement statement =null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection(
"jdbc:mysql://localhost:3306/hc_db?characterEncoding=utf8",
"root","1234"
);
String sql=" update account set money=15000 where name='wmm'";
statement=connection.createStatement();
int rows=statement.executeUpdate(sql);
System.out.println("修改数据条数"+rows);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败");
e.printStackTrace();
} catch (SQLException throwables) {
System.out.println("创建传送器失败,或者sql语法错误");
throwables.printStackTrace();
}finally {
if (statement !=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection !=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
public static void testAccount() {
Connection connection=null;
Statement statement =null;
ResultSet resultSet=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/hc_db?characterEncoding=utf8",
"root","1234"
);
statement =connection.createStatement();
String sql="select *from account where name ='wmm'";
resultSet=statement.executeQuery(sql);
if (resultSet.next()){
Integer id =resultSet.getInt(1);
String name=resultSet.getString(2);
Double money=resultSet.getDouble(3);
System.out.println(id+','+name+','+money);
}
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败");
e.printStackTrace();
} catch (SQLException throwables) {
System.out.println("创建传送器失败,或者sql语法错误");
throwables.printStackTrace();
}finally {
if (resultSet !=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement !=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection !=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
public static void testDelet() {
Connection connection=null;
Statement statement=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/hc_db?characterEncoding=utf8",
"root","1234");
String sql="delete from account where name='王飞'";
statement= connection.createStatement();
int rows=statement.executeUpdate(sql);
System.out.println("修改条数为:"+rows);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败");
e.printStackTrace();
} catch (SQLException throwables) {
System.out.println("创建传输器失败,或者语法错误");
throwables.printStackTrace();
}finally {
if (statement !=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection !=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
|
|
|
|
|
| 上一篇文章 下一篇文章 查看所有文章 |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| 360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年11日历 | -2025/11/21 9:18:59- |
|
| 网站联系: qq:121756557 email:121756557@qq.com IT数码 |