ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

一个web小项目04

2022-06-14 08:34:26  阅读:156  来源: 互联网

标签:web return String 04 项目 rs bi public id


package src.bean;

import java.sql.*;
import java.util.*;

/**
* 构造BookInfo对象,对应数据库的BookInfo表 提供了多个静态方法完成BookInfo对象与对应数据库表的所有新增、查询、修改、删除等操作
*
* @author Leiyu
* @version 1.0
*
*/
public class BookInfo {

private String id;
private String bookname;
private String author;
private String price;

public String getBookname() {
return bookname;
}

public void setBookname(String bookname) {
this.bookname = bookname;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public void setId(String id) {
this.id = id;
}

public String getId() {
return id;
}

/**
* 从BookInfo表中获取所有的图书信息
*
* @return BookInfo的数组
*/
public static ArrayList<BookInfo> getBookList() {
ArrayList<BookInfo> list = new ArrayList<BookInfo>();
String sql = "select * from bookinfo";
src.bean.DBBean jdbc = new src.bean.DBBean();
ResultSet rs = jdbc.executeQuery(sql);
try {
while (rs.next()) {
BookInfo bi = new BookInfo();
bi.setId(rs.getString("id"));
bi.setBookname(rs.getString("bookname"));
bi.setAuthor(rs.getString("author"));
bi.setPrice(rs.getString("price"));
list.add(bi);
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
jdbc.close();
return list;
}

/**
* 获取指定id的图书信息
*
* @param id 图书id
* @return 一个BookInfo对象
*/
public static BookInfo getBookById(String id) {
String sql = "select * from bookinfo where id=" + id;
src.bean.DBBean jdbc = new src.bean.DBBean();
ResultSet rs = jdbc.executeQuery(sql);
BookInfo bi = new BookInfo();
try {
if (rs.next()) {
bi.setId(rs.getString("id"));
bi.setBookname(rs.getString("bookname"));
bi.setAuthor(rs.getString("author"));
bi.setPrice(rs.getString("price"));
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
jdbc.close();
return bi;
}

/**
* 更新指定id的图书信息
*
* @param bi 要更新的图书的对象
* @return 修改的结果:1代表成功,0代表没有更新
*/
public static int updateBook(BookInfo bi) {
int result = 0;
String sql = "update bookinfo set bookname='" + bi.getBookname() + "',author='" + bi.getAuthor() + "',price="
+ bi.getPrice() + " where id=" + bi.getId();
src.bean.DBBean jdbc = new src.bean.DBBean();
result = jdbc.executeUpdate(sql);
jdbc.close();
return result;
}

/**
* 删除指定id的图书
*
* @param id 图书id
* @return 删除的结果:1代表成功,0代表没有删除
*/
public static int deleteBook(String id) {
int result = 0;
String sql = "delete from bookinfo where id=" + id;
src.bean.DBBean jdbc = new src.bean.DBBean();
result = jdbc.executeUpdate(sql);
jdbc.close();
return result;
}

/**
* 增加一本图书
*
* @param bi 图书对象
* @return 新增的结果:1代表成功,0代表没有增加
*/
public static int addBook(BookInfo bi) {
int result = 0;
String sql = "insert into bookinfo values(null,'" + bi.getBookname() + "','" + bi.getAuthor() + "',"
+ bi.getPrice() + ")";
src.bean.DBBean jdbc = new src.bean.DBBean();
result = jdbc.executeUpdate(sql);
jdbc.close();
return result;
}
}


package src.bean;

import java.sql.*;

/**
* 完成与数据库的连接和数据的访问
* @author Leiyu
* @version 1.0
*
*/
public class DBBean {
private String driverStr = "com.mysql.jdbc.Driver";
private String connStr = "jdbc:mysql://127.0.0.1:3306/db?useSSL=false&useUnicode=true&characterEncoding=utf-8";
private String dbusername = "root";
private String dbpassword = "libin1214";
private Connection conn = null;
private Statement stmt = null;

public DBBean() {
try {
Class.forName(driverStr);
conn = DriverManager.getConnection(connStr, dbusername, dbpassword);
stmt = conn.createStatement();
} catch (Exception ex) {
System.out.println("数据库连接失败!");
}
}

/**
* 执行更新操作
* @param s
* SQL语句
* @return
* 更新操作的结果
*/
public int executeUpdate(String s) {
int result = 0;
try {
result = stmt.executeUpdate(s);
} catch (Exception ex) {
System.out.println("更新出现异常!");
}
return result;
}

/**
* 执行查询操作
* @param s
* SQL语句
* @return
* 查询结果
*/
public ResultSet executeQuery(String s) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(s);
} catch (Exception ex) {
System.out.println("查询出现异常!");
}
return rs;
}

/**
* 关闭数据库
*/
public void close() {
try {
stmt.close();
conn.close();
} catch (Exception e) {
}
}
}

标签:web,return,String,04,项目,rs,bi,public,id
来源: https://www.cnblogs.com/libin159/p/16373030.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有