新手提问,简单java编程问题。 1.使用数组保存书,CD,磁带等信息,并能实现插入,删除,查找功能。插入,删除时,要显示操作正确与否的信息;查找时按关键字进行查找,显示查找结果。2.重新设计第一题。设计一个媒体类,其中包含:书,CD及磁带三个子类。按照类的设计模式,完成它们的插入,删除及查找功能。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用数组保存书是用ArrayList吗?自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩).Object[10];我以前好像做过这个题目,哈哈class Book{属性 get & set} 使用数组保存书 是用ArrayList吗? 自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩). Object[10]; 我以前好像做过这个题目,哈哈 class Book{ 属性 get & set } 使用数组保存书 是用ArrayList吗? 自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩). Object[10]; 我以前好像做过这个题目,哈哈 class Book{ 属性 get & set } 查找频繁的话,用ArrayList;插入、删除频繁的话,用LinkedList,、 建立仓库类,要实现查找用HashMap好了。就是放一个HashMap成员。具体就是Map中Key放String 而Value中放多媒体这个类型。然后书,CD及磁带三个子类继承多媒体。就可以实现了。代码自己写。。 我忍不住要说下 shit 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; }} 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,re)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.getRe()); 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; } } } 求一正则,两个数字和两个字符组成的四个字符,无序,谢谢 java初学者求问 使用InputStream读FTP,读出的数据不正 帮忙算一下sum=1/1!+1/2!+1/3!+...+1/n! Java 编程思想到底哪一版本最好啊? 如何把一个图标加入到窗口中???? 病毒程序是靠什么机制在网上运行和传染的? 急,急,急,请问何处有jBuilder5的使用资料. 请问weblogic5.1如何配置与Oracle数据库通讯(重分恳请高手帮助) 如何重绘组件? 请教特简单的环境变量的问题 初识UDP的协议 解包问题 目前在byte[]的操作上有问题
是用ArrayList吗?自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩).Object[10];我以前好像做过这个题目,哈哈class Book{
属性 get & set
}
是用ArrayList吗? 自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩). Object[10]; 我以前好像做过这个题目,哈哈 class Book{
属性 get & set
}
是用ArrayList吗? 自己构建一个类,包装数组[],动态分配数组长度(自伸展,自收缩). Object[10]; 我以前好像做过这个题目,哈哈 class Book{
属性 get & set
}
插入、删除频繁的话,用LinkedList,
、
具体就是Map中Key放String 而Value中放多媒体这个类型。
然后书,CD及磁带三个子类继承多媒体。
就可以实现了。
代码自己写。。
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;
}
}
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,re)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.getRe());
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;
}
}
}