其他分享
首页 > 其他分享> > 一个web小项目04

一个web小项目04

作者:互联网

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