Java-jdbc增删改查操作
2024-08-23 12:34:53
java jdbc增删改查操作:
package com.gordon.jdbc; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet; public class jdbc01 { public static void main(String[] args) {
// selectFunction();
// insertFunction();
// updateFunction();
// deleteFunction();
} public static void deleteFunction() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
String sql = "DELETE FROM user WHERE ID = ?";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, "4");
int res = st.executeUpdate();
if (res > 0) {
System.out.println("delete success.");
} else if (res == 0) {
System.out.println("deleted.");
} else {
System.out.println("delete faild.");
}
st.close();
conn.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
} public static void updateFunction() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
String sql = "UPDATE user SET name = ? WHERE ID = ?";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, "zhangsan");
st.setString(2, "4");
int res = st.executeUpdate();
if (res > 0) {
System.out.println("update success.");
} else {
System.out.println("update faild.");
}
st.close();
conn.close(); } catch (Exception e) {
System.out.println(e.getMessage());
}
} public static void insertFunction() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
String sql = "INSERT INTO user (name) VALUES (?)";
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, "name");
int res = st.executeUpdate();
if (res > 0) {
System.out.println("insert success.");
} else {
System.out.println("insert faild.");
}
st.close();
conn.close(); } catch (Exception e) {
System.out.println(e.getMessage());
}
} public static void selectFunction() {
try {
// 1.设置驱动
Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root"); // 3.sql语句
String sql = "SELECT * FROM user"; // 3.获取SQL执行者
PreparedStatement st = conn.prepareStatement(sql); // 4.设置参数 // 5.执行sql语句
ResultSet rs = st.executeQuery(); // 6.处理数据
while (rs.next()) {
System.out.println(rs.getString("id") + ":" + rs.getString("name"));
} // 7.释放资源
rs.close();
st.close();
conn.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
最新文章
- iOS Version 和 Build 版本号
- 让你忘记 Flash 的15款精彩 HTML5 游戏
- Servlet/JSP-06 Session
- adobe pro破解说明
- Spring MVC 事务配置
- 缓存1 静态缓存-->;读库保存成php文件 mkdir-->;file_put_contents-->;var_export -->;include
- ubuntu auto mount自动挂载硬盘
- java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)
- SQL Server 2008 无法安装U890
- 虚拟机复制问题:如何解决“无法手动启动VMware Tools安装”问题
- dva构建react项目
- python 字典中 重复值去除
- yum 原理C/S原理结构图
- Python的文件处理
- 基于CSS3鼠标滑过放大突出效果
- vue 项目要使用的库
- [SQL] SQL 日常检查脚本
- [转]LAMP(Linux-Apache-MySQL-PHP)网站架构
- 2018.09.16 loj#10241. 取石子游戏 1(博弈论)
- 装饰器 and 闭包函数 未完。。。。。
热门文章
- Linux vi命令大全
- Python基本数据类型详细介绍(转)
- ngx_lua学习笔记 -- capture + proxy 实现httpclient
- 计算机科学基础知识(一)The Memory Hierarchy
- [转]HSpice仿真
- Java:多线程,线程池,ThreadPoolExecutor详解
- [sql]大型网站MySQL深度优化揭秘
- 利用命令行引用外部jar包以使程序正常执行的4种方法
- android framework-安装samba
- You have JVM property ";https.proxyHost"; set to “localhost”