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());
}
}
}

  

最新文章

  1. iOS Version 和 Build 版本号
  2. 让你忘记 Flash 的15款精彩 HTML5 游戏
  3. Servlet/JSP-06 Session
  4. adobe pro破解说明
  5. Spring MVC 事务配置
  6. 缓存1 静态缓存-->读库保存成php文件 mkdir-->file_put_contents-->var_export -->include
  7. ubuntu auto mount自动挂载硬盘
  8. java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)
  9. SQL Server 2008 无法安装U890
  10. 虚拟机复制问题:如何解决“无法手动启动VMware Tools安装”问题
  11. dva构建react项目
  12. python 字典中 重复值去除
  13. yum 原理C/S原理结构图
  14. Python的文件处理
  15. 基于CSS3鼠标滑过放大突出效果
  16. vue 项目要使用的库
  17. [SQL] SQL 日常检查脚本
  18. [转]LAMP(Linux-Apache-MySQL-PHP)网站架构
  19. 2018.09.16 loj#10241. 取石子游戏 1(博弈论)
  20. 装饰器 and 闭包函数 未完。。。。。

热门文章

  1. Linux vi命令大全
  2. Python基本数据类型详细介绍(转)
  3. ngx_lua学习笔记 -- capture + proxy 实现httpclient
  4. 计算机科学基础知识(一)The Memory Hierarchy
  5. [转]HSpice仿真
  6. Java:多线程,线程池,ThreadPoolExecutor详解
  7. [sql]大型网站MySQL深度优化揭秘
  8. 利用命令行引用外部jar包以使程序正常执行的4种方法
  9. android framework-安装samba
  10. You have JVM property "https.proxyHost" set to “localhost”