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

jdbc简单封装

实体类

package org.example;public class account {private int id;private String name;private double balance;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}@Overridepublic String toString() {return "account{" +"id=" + id +", name='" + name + '\'' +", balance=" + balance +'}';}
}

数据库读取数据并封装

package org.jdbc;import org.example.account;import java.sql.*;
import java.util.ArrayList;
import java.util.List;public class jdbcdemo3 {public static void main(String[] args){List<account> list = new jdbcdemo3().findall();System.out.println(list);}public List<account> findall(){Connection connection = null;Statement statement = null;ResultSet resultSet =null;List<account> list = null;try {//兼容低版本mysqlClass.forName("com.mysql.jdbc.Driver");String query = "select * from account";connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","password");statement = connection.createStatement();resultSet = statement.executeQuery(query);account acc = null;list = new ArrayList<>();while (resultSet.next()){int id = resultSet.getInt("id");String name = resultSet.getNString("name");double balance = resultSet.getDouble("balance");System.out.println(id +"----"+name+"-----"+balance);acc = new account();acc.setId(id);acc.setName(name);acc.setBalance(balance);list.add(acc);}}catch (ClassNotFoundException e) {throw new RuntimeException(e);} catch (SQLException e) {throw new RuntimeException(e);}finally {if(resultSet != null){try {resultSet.close();} catch (SQLException e) {throw new RuntimeException(e);}}if(statement != null){try {statement.close();} catch (SQLException e) {throw new RuntimeException(e);}}if(connection != null){try {connection.close();} catch (SQLException e) {throw new RuntimeException(e);}}}return list;}
}
http://www.dtcms.com/a/458695.html

相关文章:

  • 永宝网站建设招聘信息芜湖做网站的客户
  • BGP 状态机与报文类型详解
  • 桐庐县住房和城乡建设局网站没有网站可以icp备案
  • 免费推广网站排行榜ui视觉设计常用软件是什么
  • 便捷的大连网站建设专业网站建设官网
  • npm 扩展Vite、Element-plus 、Windcss、Vue Router
  • 程序员和网站建设妇联 网站建设情况
  • 食品网站制作点击图片直接进入网站怎么做
  • 酒店网站建设江苏省和城乡建设门户网站
  • 上海做网站的故事哪家好陕西省建设厅人力资源网站
  • 专业网站设计软件工具wordpress 插件本地化
  • 西宁集团网站建设潍坊网站建设推广公司
  • Echarts饼图示例
  • 力扣刷题——二叉树相同的树
  • 上海奉贤网站建设免费二维码制作生成器
  • 株洲企业网站建设工作网站建设一般预付比例多少
  • 嵌入式 Linux(以泰山派为例)忘记 SSH 密码怎么办?无需重装系统的重置方案
  • 餐饮行业做网站的数据广州网站建设广州网络推广公司排名
  • 织梦网络公司网站源码招投标 网站建设
  • 爱做网站网址wordpress建淘宝客
  • 汉中市网站建设公司淘宝买模板注浆做网站
  • 做网站的linux程序代码爱深圳小程序
  • Unity网络开发--套接字Socket(1)
  • 南京做网站的有哪些学生个人网页制作主题
  • 【LLM】ms-Swift大模型训练框架源码分析
  • 安全电子商务网站设计一个网站怎么绑定很多个域名
  • Docker镜像
  • 网站部署环境推广平台怎么赚钱
  • 2817网站中山网站建设制作
  • 【Algorithm】Day-2