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

相关推荐
磊 子13 小时前
多态类原理+四种类型转换+异常处理
开发语言·c++·算法
脆皮炸鸡75513 小时前
库制作与原理~动态链接
linux·开发语言·经验分享·笔记·学习方法
XMYX-013 小时前
26 - Go recover 捕获错误:优雅恢复的真正意义
开发语言·golang
小白学大数据13 小时前
基于大模型的Python智能爬虫:语义识别与数据清洗实践
开发语言·爬虫·python·数据分析
掉鱼的猫13 小时前
Spring AI 2.0 GA 倒计时:先别急,来看看 Java AI 框架的另一条路
java·openai·agent
Refrain_zc13 小时前
Android 应用内 APK 安装全方案:从静默安装到普通安装的详解
java
Dxy123931021613 小时前
Python请求方式介绍:JSON、表单及其他常见数据传输格式
数据库·python·json
迷渡13 小时前
聊一聊 Bun 用 Rust 重写这件事
开发语言·后端·rust
古怪今人13 小时前
Gradle构建工具 Groovy/Kotlin DSL的现代化自动化构建工具
开发语言·kotlin·自动化
赏金术士13 小时前
Kotlin 协程与挂起函数(Coroutines & suspend)入门到实战
android·开发语言·kotlin