新手提问,简单java编程问题。

yintoki 2008-11-07 08:47:54
1.使用数组保存书,CD,磁带等信息,并能实现插入,删除,查找功能。插入,删除时,要显示操作正确与否的信息;查找时按关键字进行查找,显示查找结果。
2.重新设计第一题。设计一个媒体类,其中包含:书,CD及磁带三个子类。按照类的设计模式,完成它们的插入,删除及查找功能。
...全文
819 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
East271536394 2008-11-13
  • 打赏
  • 举报
回复
package com.failthshopping.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import com.failthshopping.beans.UserInfoBean;

/**
* @date 2008-8-4
* @author East(张栋芳)
* @UserInfoDao(用户信息表)的数据库操作
*/
public class UserInfoDao
extends BaseDao {
public UserInfoDao() {
}
/****
* 增加用户信息
*/
public boolean insertUserInfo(UserInfoBean user) {
Connection con = null;
PreparedStatement pstmt = null;
int result = 0;
String strSql = "insert into userInfo(userName,password,sex,age,card,qq,email,telephone,address,buyPoint,sellPoint,headImg,isLogin,isVip,pwdQuestion,pwdAnswer,remark)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getSex());
pstmt.setInt(4, user.getAge());
pstmt.setString(5, user.getCard());
pstmt.setString(6, user.getQq());
pstmt.setString(7, user.getEmail());
pstmt.setString(8, user.getTelephone());
pstmt.setString(9, user.getAddress());
pstmt.setInt(10, user.getBuyPoint());
pstmt.setInt(11, user.getSellPoint());
pstmt.setString(12, user.getHeadImg());
pstmt.setInt(13, user.getIsLogin());
pstmt.setInt(14, user.getIsVip());
pstmt.setString(15, user.getPwdQuestion());
pstmt.setString(16, user.getPwdAnswer());
pstmt.setString(17, user.getRemark());
result = pstmt.executeUpdate();
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt);
return result > 0 ? true : false;
}
}
/****
* 通过用户名修改用户基本信息
*/
public boolean updateUserInfoByUserName(UserInfoBean user) {
Connection con = null;
PreparedStatement pstmt = null;
int result = 0;
String strSql = "update userInfo set password=?,sex=?,age=?,card=?,qq=?,email=?,telephone=? where userName=?";
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);
pstmt.setString(1, user.getPassword());
pstmt.setString(2, user.getSex());
pstmt.setInt(3, user.getAge());
pstmt.setString(4, user.getCard());
pstmt.setString(5, user.getQq());
pstmt.setString(6, user.getEmail());
pstmt.setString(7, user.getTelephone());
pstmt.setString(8, user.getUserName());
result = pstmt.executeUpdate();
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt);
return result > 0 ? true : false;
}
}
/****
* 通过用户名修改用户密码
*/
public boolean updateUserPwdByUserName(UserInfoBean user) {
Connection con = null;
PreparedStatement pstmt = null;
int result = 0;
String strSql = "update userInfo set password=? where userName=?";
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);
pstmt.setString(1, user.getPassword());
pstmt.setString(2, user.getUserName());
result = pstmt.executeUpdate();
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt);
return result > 0 ? true : false;
}
}

/****
* 通过用户名删除用户信息
*/
public boolean deleteUserInfoByUserName(UserInfoBean user) {
Connection con = null;
PreparedStatement pstmt = null;
int result = 0;
String strSql = "delete userInfo where userName=?";
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);
pstmt.setString(1, user.getUserName());
result = pstmt.executeUpdate();
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt);
return result > 0 ? true : false;
}
}
/***
* 通过用户名修改是否登录
*/
public boolean updateByUserNameIsLogin(UserInfoBean user) {
Connection con = null;
PreparedStatement pstmt = null;
String strSql = "update userInfo set isLogin = ? while userName=?";
int result = 0;
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);
pstmt.setInt(1, user.getIsLogin());
pstmt.setString(2, user.getUserName());
result = pstmt.executeUpdate();
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt);
return result > 0 ? true : false;
}
}
/***
* 通过用户名修改是否是会员
*/
public boolean updateByUserNameIsVip(UserInfoBean user) {
Connection con = null;
PreparedStatement pstmt = null;
String strSql = "update userInfo set isVip = ? where userName=?";
int result = 0;
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);
pstmt.setInt(1, user.getIsVip());
pstmt.setString(2, user.getUserName());
result = pstmt.executeUpdate();
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt);
return result > 0 ? true : false;
}
}
/****
* 查询是否存在用户名和密码
*/
public boolean isExistsUserName(UserInfoBean user) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String strSql = "select * from userInfo where userName=? and password = ?";
int result = 0;
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);

pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
rs = pstmt.executeQuery();
if (rs.next()) {
result++;
}
else {
return false;
}
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt, rs);
return result > 0 ? true : false;
}
}

/***
* 通过用户名修改用户的密码问题和密码答案
*/
public boolean updatePwdQuestion(UserInfoBean user) {
int result = 0;
Connection con = null;
PreparedStatement pstmt = null;
String strSql =
"update userInfo set pwdQuestion =?,pwdAnswer=? where userName=?";
try {
con = super.createConnection();
pstmt = con.prepareStatement(strSql);
pstmt.setString(1, user.getPwdQuestion());
pstmt.setString(2, user.getPwdAnswer());
pstmt.setString(3, user.getUserName());
result = pstmt.executeUpdate();
}
catch (SQLException se) {
se.printStackTrace();
}
finally {
super.closeAll(con, pstmt);
return result > 0 ? true : false;
}
}
}
wonflay 2008-11-11
  • 打赏
  • 举报
回复
结帖率:22.22% 但愿这次有分--!!
时光瞄 2008-11-11
  • 打赏
  • 举报
回复
17楼的方法还行...
「已注销」 2008-11-10
  • 打赏
  • 举报
回复
结帖率:22.22%


靠,数还真顺
lybjust 2008-11-09
  • 打赏
  • 举报
回复
package com.lyb.csdn;

import java.util.ArrayList;
import java.util.Iterator;

public class SaveInfo {

/**
* @author lybjust
*/
ArrayList al = new ArrayList();

public void save(Book book) {
al.add(book);
System.out.println(book.getBookId() + "插入成功!");
}

public void del(Book book) {
al.remove(book);
System.out.println("删除成功 " + book.getBookId() + " "
+ book.getBookName());
}

public Book search(int id) {
Book b = null;
Iterator iter = al.iterator();
while (iter.hasNext()) {
b = (Book) iter.next();
if (b.getBookId() == id) {
break;
}
}
return b;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Book b1 = new Book(1001, "CSDN_1");
Book b2 = new Book(1002, "CSDN_2");
Book b3 = new Book(1003, "CSDN_3");
SaveInfo sa = new SaveInfo();
sa.save(b1);
sa.save(b2);
sa.save(b3);
sa.del(b2);
Book bs = (Book) sa.search(1001);
System.out.println("查询结果 " + bs.getBookId() + " " + bs.getBookName());
}
}

/*class CD {
private int cdId;
private String cdName;

public CD(int cdId, String cdName) {
this.cdId = cdId;
this.cdName = cdName;
}

public int getCdId() {
return cdId;
}

public void setCdId(int cdId) {
this.cdId = cdId;
}

public String getCdName() {
return cdName;
}

public void setCdName(String cdName) {
this.cdName = cdName;
}
}*/

class Book {
private int bookId;
private String bookName;

public Book(int bookId, String bookName) {
this.bookId = bookId;
this.bookName = bookName;
}

public int getBookId() {
return bookId;
}

public void setBookId(int bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}
}
echoabs 2008-11-09
  • 打赏
  • 举报
回复
我觉得这个求解应该发到百度上面去!!!
echoabs 2008-11-09
  • 打赏
  • 举报
回复
我觉得这个求解应该发到百度上面去!!!
chendabian 2008-11-09
  • 打赏
  • 举报
回复
我还 以为要用数据库呢!
17楼的 用集合 好像还不错
markspain 2008-11-09
  • 打赏
  • 举报
回复
......作业啊!
autosky 2008-11-09
  • 打赏
  • 举报
回复
初学者,不太懂,过来看看。
xxxholic001 2008-11-09
  • 打赏
  • 举报
回复
路过~~来看看~~
edison2553532 2008-11-09
  • 打赏
  • 举报
回复
不懂..等高人解答..
tiyuzhongxin789 2008-11-08
  • 打赏
  • 举报
回复
使用数组保存书
是用ArrayList吗?

自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩).

Object[10];

我以前好像做过这个题目,哈哈

class Book{
属性 get & set
}
wyyl00 2008-11-08
  • 打赏
  • 举报
回复
我忍不住要说下 shit
henry_fuzr 2008-11-08
  • 打赏
  • 举报
回复
建立仓库类,要实现查找用HashMap好了。就是放一个HashMap成员。
具体就是Map中Key放String 而Value中放多媒体这个类型。
然后书,CD及磁带三个子类继承多媒体。
就可以实现了。
代码自己写。。
sourpuss 2008-11-08
  • 打赏
  • 举报
回复
猛将兄!!!!
shenjie1989 2008-11-08
  • 打赏
  • 举报
回复
要代码是不不行的,自己写吧,写完了再问。
yintoki 2008-11-08
  • 打赏
  • 举报
回复
没人给点代码吗?第二题的?
qianmengzhao 2008-11-08
  • 打赏
  • 举报
回复
使用数组保存书
是用ArrayList吗?

自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩).

Object[10];

我以前好像做过这个题目,哈哈

class Book{
属性 get & set
}
ceres0503 2008-11-08
  • 打赏
  • 举报
回复
晕。。。。这东西不是和c/c++实现一样么
加载更多回复(5)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧