当前位置: 首页 > news >正文

Java知识点总结(JDBC-连接步骤及CRUD)

Java知识点总结(JDBC-连接步骤及CRUD)

@(Java知识点总结)[Java, JDBC]

连接数据库步骤

clipboard.png

依序关闭使用的对象连接:

  • ResultSet -> Statement -> Connection

CRUD操作

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
 
public class StudentDao {
 
  private static Connection getConn() {
      String driver = "com.mysql.jdbc.Driver";
      String url = "jdbc:mysql://localhost:3306/test";
      String username = "root";
      String password = "123";
      Connection conn = null;
      try {
          Class.forName(driver); //classLoader,加载对应驱动
          //建立连接(连接对象内部包含了Socket对象,是一个远程连接。比较耗时!这是Connection对象管理的一个要点!)
          //真正开发中,为了提高效率,都会使用连接池来管理连接对象!
          conn = (Connection) DriverManager.getConnection(url, username, password);
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      } catch (SQLException e) {
          e.printStackTrace();
      }
      return conn;
  }
  
  private static int insert(User user) {
      Connection conn = getConn();
      int i = 0;
      String sql = "insert into users (Name,Sex,Age) values(?,?,?)";
      PreparedStatement pstmt = null;
      try {
          pstmt = (PreparedStatement) conn.prepareStatement(sql);
          pstmt.setString(1, user.getName());
          pstmt.setString(2, user.getSex());
          pstmt.setInt(3, user.getAge());
          i = pstmt.executeUpdate();
          pstmt.close();
          conn.close();
      } catch (SQLException e) {
          e.printStackTrace();
      }finally {
     try {
      pstmt.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     try {
      conn.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
   }
      return i;
  }
  
  private static int update(User user) {
      Connection conn = getConn();
      int i = 0;
      String sql = "update users set Age='" + user.getAge() + "' where Name='" + user.getName() + "'";
      PreparedStatement pstmt = null;
      try {
          pstmt = (PreparedStatement) conn.prepareStatement(sql);
          i = pstmt.executeUpdate();
          System.out.println("resutl: " + i);
          pstmt.close();
          conn.close();
      } catch (SQLException e) {
          e.printStackTrace();
      }finally {
     try {
      pstmt.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     try {
      conn.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
   }
      return i;
  }
  
  private static Integer getAll() {
      Connection conn = getConn();
      String sql = "select * from users";
      PreparedStatement pstmt =null;
      ResultSet  rs = null;
      try {
          pstmt = (PreparedStatement)conn.prepareStatement(sql);
          rs = pstmt.executeQuery();
          int col = rs.getMetaData().getColumnCount();
          System.out.println("============================");
          while (rs.next()) {
              for (int i = 1; i <= col; i++) {
                  System.out.print(rs.getString(i) + "\t");
                  if ((i == 2) && (rs.getString(i).length() < 8)) {
                      System.out.print("\t");
                  }
               }
              System.out.println("");
          }
              System.out.println("============================");
      } catch (SQLException e) {
          e.printStackTrace();
      }finally {
       try {
      rs.close();
     } catch (SQLException e1) {
      e1.printStackTrace();
     }
     try {
      pstmt.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     try {
      conn.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
   }
      return null;
  }
  
  private static int delete(String name) {
      Connection conn = getConn();
      int i = 0;
      String sql = "delete from users where Name='" + name + "'";
      PreparedStatement pstmt = null;
      try {
          pstmt = (PreparedStatement) conn.prepareStatement(sql);
          i = pstmt.executeUpdate();
          System.out.println("resutl: " + i);
          pstmt.close();
          conn.close();
      } catch (SQLException e) {
          e.printStackTrace();
      }finally {
     try {
      pstmt.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
     try {
      conn.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
   }
      return i;
  }
  
  public static void main(String[] args) {
   getAll();
       insert(new User("张柏芝", "女", 43));
       getAll();
       update(new User("张三", "", 38));
       delete("Achilles");
       getAll();
  }
}
public class User {
 
  private int id;
  private int age;
  private String name ;
  private String sex;
  
  
  public User( String name, String sex,int age) {
   this();
   this.age = age;
   this.name = name;
   this.sex = sex;
  }
  public User() {
 
  }
  public int getAge() {
   return age;
  }
  public int getId() {
   return id;
  }
  public String getName() {
   return name;
  }
  public String getSex() {
   return sex;
  }
  public void setAge(int age) {
   this.age = age;
  }
  public void setId(int id) {
   this.id = id;
  }
  public void setName(String name) {
   this.name = name;
  }
  public void setSex(String sex) {
   this.sex = sex;
  }
  
}

执行结果:

clipboard.png

相关文章:

  • 各种品牌进入Bios方式
  • WIFI搜索的到别人,却找不到自己家的wifi
  • Go第三方库
  • http和https和ssl和tcp/ip之间的关系和区别
  • 人工智能20年内取代近半职业?
  • bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp
  • HyperLedger Fabric(超级账本) 入门实战
  • MySQL多实例安装部署
  • 网络命令初步排错
  • python_正则表达式匹配ip
  • 路径中文问题
  • leetcode-849-到最近的人的最大距离
  • java中与运算,或运算,异或运算,取反运算
  • Windows后登陆没有图形界面只有cmd,explorer.exe不能启动
  • fastdfs添加新group注意事项
  • 2017 前端面试准备 - 收藏集 - 掘金
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • create-react-app做的留言板
  • download使用浅析
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • javascript数组去重/查找/插入/删除
  • JDK9: 集成 Jshell 和 Maven 项目.
  • JS变量作用域
  • orm2 中文文档 3.1 模型属性
  • python docx文档转html页面
  • tweak 支持第三方库
  • ubuntu 下nginx安装 并支持https协议
  • 初识 beanstalkd
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 扑朔迷离的属性和特性【彻底弄清】
  • 如何设计一个微型分布式架构?
  • 正则表达式
  • 进程与线程(三)——进程/线程间通信
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • 智能情侣枕Pillow Talk,倾听彼此的心跳
  • #QT(智能家居界面-界面切换)
  • #传输# #传输数据判断#
  • $.ajax()参数及用法
  • (007)XHTML文档之标题——h1~h6
  • (2.2w字)前端单元测试之Jest详解篇
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (C++)八皇后问题
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (接口封装)
  • (未解决)jmeter报错之“请在微信客户端打开链接”
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • .jks文件(JAVA KeyStore)
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .Net的C#语言取月份数值对应的MonthName值
  • [ 常用工具篇 ] POC-bomber 漏洞检测工具安装及使用详解
  • [2544]最短路 (两种算法)(HDU)
  • [BZOJ2850]巧克力王国
  • [codevs 1296] 营业额统计
  • [Java] 什么是IoC?什么是DI?它们的区别是什么?
  • [js]js设计模式小结