如何用myeclipse编写检索图书的代码?思路是什么?求高人指点迷津。谢谢啊!

如何用myeclipse编写检索图书的代码?思路是什么?求高人指点迷津。谢谢啊!,第1张

首先创建oracle数据库DEMO,创建图书清磨表Books

//使用java连接数据库

Connection conn = null

Statement stat = null

try{

Class.forName("oracle.jdbc.driver.OracleDriver")// 如老睁果是db2,则为侍正岁Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance()

conn = DriverManager.getConnection("jdbc:oracle:thin:@dbserver:1521:DEMO","username","password")

String sqlString = "select * from Books" // 检索图书的SQL

stat = conn.createStatement()

return stat.execute(sqlString)

}catch(Exception e){

//异常处理

}finally{

if(null != conn ){

conn.close()

}

if(null != stat ){

stat.close()

}

}

#include <iostream>正蔽

#include <string>

#include <list>

using namespace std

// 图书检索卡结构体

struct BookIndex

{

//编号

int uid

//书名

string name

//作者姓名

string author

//出版日期(格式YYYY-MM-DD)

string publicDate

//页数

int pages

//定价

float price

friend istream&operator >>(istream &is,struct BookIndex &b)

{

cout<<"书名:"

is>>b.name

cout<<"作者:"

is>>b.author

cout<<"出版日期:(格式YYYY-MM-DD) "

is>>b.publicDate

cout<<"页数:"

is>>b.pages

cout<<"定价:"

is>>b.price

return is

}

friend ostream&operator <<(ostream &os,const struct BookIndex &b)

{

os<<"-------------------------"<<endl

os<<"编号:"<<b.uid<<endl

os<<"书名:"<<b.name<<endl

os<<"作者:"<<b.author<<endl

os<<"出版日期:"<<b.publicDate<<endl

os<<"页数:"<<b.pages<<endl

os<<"定价:"<<b.price<<endl

os<<"-------------------------"指清羡<<endl

return os

}

friend bool operator <(struct BookIndex &b1,struct BookIndex &b2)

{

return b1.publicDate <b2.publicDate

}

friend bool operator >(struct BookIndex &b1,struct BookIndex &b2)

{

return b1.publicDate >b2.publicDate

}

private:

static int UID

static int getUID(){return UID++}

}

int BookIndex::UID = 1

// 图书馆类

class Library

{

public:

Library(){}

// 登记 n 本图书

void registerBooks(int n)

{

for(int i=0i<ni++)

{

struct BookIndex b

cout<<"输入第:"<<(i+1)<<"本书信息:"<<endl

cin>>b

books.push_back(b)

cout<<endl

}

}

// 按照作者姓名进行查找,

/唯拍/ 并且按照出版日期顺序从远到近排列该找到的所有著作

list<struct BookIndex>searchBooks(string author)

{

list<struct BookIndex>result

for(list<struct BookIndex>::iterator i=books.begin()i!=books.end()i++)

{

if(i->author == author)

result.push_back(*i)

}

result.sort()

return result

}

private:

list<struct BookIndex>books

}

int main(int argc, char *argv[])

{

Library library

library.registerBooks(10)

list<struct BookIndex>books=library.searchBooks("angel")

for(list<struct BookIndex>::iterator i=books.begin()i!=books.end()i++)

{

cout<<*i<<endl

}

return 0

}

/*

book_aaa

angel

2012-01-01

100

100

book_bbb

blue

2000-11-02

136

14.5

book_ccc

angel

2010-03-12

56

123.5

book_ddd

angelfish

1989-04-14

1200

210

book_eee

angel

1991-11-23

456

123

book_fff

angel

2001-07-07

789

424

book_ggg

angel

2004-09-30

321

9

book_hhh

angel

2002-06-06

999

56

book_iii

angel

2008-08-08

654

81

book_jjj

angel

1998-09-09

111

99

*/


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12496685.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存