DAO Java

DAO: Data Access Object

1.PgAdmin4-->new table

tablename: bookinfo

2.DAO

/Daotest/src/com/daotest/SampleDAO1.java

package com.daotest;

import java.sql.*;

import java.util.ArrayList;

public class SampleDAO1 {

private static final String RDB_DRIVER = "org.postgresql.Driver";

private static final String URL = "jdbc:postgresql://localhost:5432/postgres";

private static final String USER = "postgres";

private static final String PASSWORD = "******";

private static Connection getConnection() {

try {

Class.forName(RDB_DRIVER);

Connection con = DriverManager.getConnection(URL, USER, PASSWORD);

return con;

} catch (Exception e) {

throw new IllegalStateException(e);

}

}

public ArrayList<String> selectIsbnAll() {

Connection con = null;

Statement smt = null;

ArrayList<String> list = new ArrayList<String>();

String sql = "SELECT isbn FROM bookinfo ORDER BY isbn";

try {

con = SampleDAO1.getConnection();

smt = con.createStatement();

ResultSet rs = smt.executeQuery(sql);

while (rs.next()) {

list.add(rs.getString("isbn"));

}

} catch (SQLException e) {

System.out.println("Error!\n" + e);

} finally {

if (smt != null) {

try {

smt.close();

} catch (SQLException ignore) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ignore) {

}

}

}

return list;

}

public ArrayList<String> selectTitleAll() {

Connection con = null;

Statement smt = null;

ArrayList<String> list = new ArrayList<String>();

String sql = "SELECT title FROM bookinfo ORDER BY isbn";

try {

con = SampleDAO1.getConnection();

smt = con.createStatement();

ResultSet rs = smt.executeQuery(sql);

while (rs.next()) {

list.add(rs.getString("title"));

}

} catch (SQLException e) {

System.out.println("Error!\n" + e);

} finally {

if (smt != null) {

try {

smt.close();

} catch (SQLException ignore) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ignore) {

}

}

}

return list;

}

public ArrayList<Integer> selectPriceAll() {

Connection con = null;

Statement smt = null;

ArrayList<Integer> list = new ArrayList<Integer>();

String sql = "SELECT price FROM bookinfo ORDER BY isbn";

try {

con = SampleDAO1.getConnection();

smt = con.createStatement();

ResultSet rs = smt.executeQuery(sql);

while (rs.next()) {

list.add(rs.getInt("price"));

}

} catch (SQLException e) {

System.out.println("Error!\n" + e);

} finally {

if (smt != null) {

try {

smt.close();

} catch (SQLException ignore) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ignore) {

}

}

}

return list;

}

public int insertBook(String isbn, String title, int price) {

Connection con = null;

Statement smt = null;

int rowsCount = 0;

String sql = "INSERT INTO bookinfo(isbn, title, price) " + "VALUES('" + isbn + "','" + title + "'," + price

  • ")";

try {

con = SampleDAO1.getConnection();

smt = con.createStatement();

rowsCount = smt.executeUpdate(sql);

}

catch (SQLException e) {

System.out.println("Error happened\n" + e);

} finally {

if (smt != null) {

try {

smt.close();

} catch (SQLException ignore) {

}

}

if (con != null) {

try {

con.close();

} catch (SQLException ignore) {

}

}

}

return rowsCount;

}

}

postgresql-42.7.2.jar

/Daotest/src/com/daotest/InsertProgram1.java

package com.daotest;

import java.util.ArrayList;

public class InsertProgram1 {

private static ArrayList<String> isbnList = null;

private static ArrayList<String> titleList = null;

private static ArrayList<Integer> priceList = null;

public static void main(String\[\] args) {

try {

SampleDAO1 objDao = new SampleDAO1();

isbnList = objDao.selectIsbnAll();

titleList = objDao.selectTitleAll();

priceList = objDao.selectPriceAll();

System.out.println("---Before SQL execute---");

display();

int rowsCount = objDao.insertBook("9", "Photoshop", 8200);

if (rowsCount > 0) {

System.out.println(rowsCount + "record registered\n");

}

isbnList = objDao.selectIsbnAll();

titleList = objDao.selectTitleAll();

priceList = objDao.selectPriceAll();

System.out.println("---After SQL execute---");

display();

} catch (Exception e) {

System.out.println("Error happened" + e);

}

}

private static void display() {

for (int i = 0; i < isbnList.size(); i++) {

System.out.println("ISBN->" + isbnList.get(i) + "\t");

System.out.println("Title->" + titleList.get(i) + "\t");

System.out.println("Price->" + priceList.get(i) + "\t");

}

System.out.println();

}

}

4.Run

---Before SQL execute---

ISBN->1

Title->Java

Price->2000

ISBN->2

Title->C

Price->3000

ISBN->3

Title->C

Price->3000

ISBN->4

Title->HTML

Price->1000

ISBN->5

Title->Spring

Price->1200

ISBN->7

Title->VF

Price->5200

ISBN->8

Title->CSS

Price->6200

1record registered

---After SQL execute---

ISBN->1

Title->Java

Price->2000

ISBN->2

Title->C

Price->3000

ISBN->3

Title->C

Price->3000

ISBN->4

Title->HTML

Price->1000

ISBN->5

Title->Spring

Price->1200

ISBN->7

Title->VF

Price->5200

ISBN->8

Title->CSS

Price->6200

ISBN->9

Title->Photoshop

Price->8200

相关推荐
笃行3505 小时前
数据库性能优化的五处内核改造,KingbaseES 配了四件诊断工具
数据库
非优秀程序员7 小时前
第04章 邮箱大战,163 与 QQ 的暗战|《互联网产品恩仇录》
java·vue.js·架构
非优秀程序员7 小时前
第02章 杭电的英语老师与一台服务器|《互联网产品恩仇录》
数据库·node.js·代码规范
知已难寻7 小时前
Docker 入门:容器技术与 Docker 基础
java·运维·服务器·docker·云原生·容器
沐晓时光8 小时前
C语言入门,内存函数知识讲解
c语言·开发语言
文丑颜不良啊9 小时前
LeetCode2481~2500题目解析
java·leetcode
Flynn Liang11 小时前
同一需求三套写法:用 Spring AI、LangChain4j、AgentScope Java 各实现一个带工具调用的 RAG 助手(实战对比)
java·人工智能·大模型·架构设计·spring ai·langchain4j·agentscope
聪明的笨猪猪11 小时前
实际工程问题:AI 生成 Spring Cache(@Cacheable / @CachePut / @CacheEvict)缓存注解踩坑
java·经验分享·面试·ai编程
愿有所息11 小时前
记一次 ARM64 + JDK8 上 fastjson2 的诡异数据错乱
java
SelectDB12 小时前
SelectDB 是哪个公司的?是国产数据库吗?一篇讲清飞轮科技与 Apache Doris 的关系
数据库