昨天未完结的问题!代码还有问题,高手给看一下!在线等。急急急!

lsh902 2009-10-28 11:14:11
SQL server数据库,用户名sa,密码123,表1中有多个字段,只取“业务”字段和“业务员编号”字段打印输出,但是“业务”字段要进行按相应的价格计算并打印输出,如第二条记录“业务”为苹果=2.5元;“业务员编号”为c006,所以打印输出为“2.5 c006”其他各项业务价格为橘子=3元;香蕉=2.5元;西瓜=3元,如第三条记录“橘子+苹果=5.5”,编号为“c007”,输出为 “5.5 c007”重点是有多个业务的要将单价相加输出,没有内容的打印为 0 。
表1
业务 | 业务员编号 | 其他字段。。。
| c003 | 。。。 打印输出 0 c003
苹果 | c006 | 。。。 2.5 c006
苹果;橘子 | c007 | 。。。 ———————》 5.5 c007
香蕉;橘子;苹果 | c010 | 。。。 8 c010
| c012 | 。。。 0 c012


import java.sql.*; //代码还有问题,大家帮忙看看。
import java.util.*;
public class test{
public static void main(String[] args) throws Exception {
String url = "";
String user = "sa";
String password = "123";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select 业务 as fA ,业务员编号 as fB from table ";
try{
Class.forName("");
con = DriverManager.getConnection(url,user,password);
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
String fa = "";
String fb = "";
while(rs.next()){
fa = rs.getString("fA");
fb = rs.getString("fB");
if("".equals(fa)){
System.out.println("0\t"+fb);
}else {
if(fa.indexOf(";") != -1){
int sum = 0;
String[] temp = fa.split(";");
for(int i = 0 ; i < temp.length; i ++){
sum += getPrice(temp[i]);
}
System.out.println(sum + "\t"+fb);
}else {
System.out.println(getPrice(fa)+"\t"+fb);
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null)
rs.close();
if(pstmt != null)
pstmt.close();
if(con != null)
con.close();
}catch(Exception e){}
}
}
int apple = 1;
int banana = 2;
int orange = 3;
int watermelon = 4;
private int getPrice(String name){//这一段看不太明白,运行时报错。
return "苹果".equals(name) ? apple : "香蕉".equals(name) ? banana : "橘子".equals(name) ? orange : watermelon;
}
}
...全文
287 22 打赏 收藏 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsh902 2009-10-31
  • 打赏
  • 举报
回复
帖子沉的好快啊!!
lsh902 2009-10-30
  • 打赏
  • 举报
回复
谢谢!帮顶!
sforiz 2009-10-30
  • 打赏
  • 举报
回复
up
lsh902 2009-10-30
  • 打赏
  • 举报
回复
麻烦大家再给看看!!!
lsh902 2009-10-30
  • 打赏
  • 举报
回复
大家给想想办法,怎么修改合适???
  • 打赏
  • 举报
回复
晕啊,这张表的设计存在严重的问题
lsh902 2009-10-30
  • 打赏
  • 举报
回复
大家帮忙看看!
lsh902 2009-10-30
  • 打赏
  • 举报
回复
大家帮忙改改!
lsh902 2009-10-30
  • 打赏
  • 举报
回复
首先,感谢大家这样热心帮我解决问题。
经过修改代码可以正常跑了,就是sum应当为double型,不然没法进行浮点运算。还有点问题麻烦大家看看。
1.多添加一个“业务员姓名”字段,我这样写是否可以成功???
2.打印的同时IO输出到“out.txt”文本,因为数据太多DOS窗口显示不过来。
下面做的修改,大家帮忙改改。能让程序更节省资源,并正常运行。

import java.sql.*;
import java.util.*;
public class test{
public static void main(String[] args) throws Exception {
String url = "";
String user = "sa";
String password = "123";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select 业务 as fA ,业务员编号 as fB,业务员姓名 as fc from table ";
try{
Class.forName("");
con = DriverManager.getConnection(url,user,password);
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
String fa = "";
String fb = "";
String fc = "";
while(rs.next()){
fa = rs.getString("fA");
fb = rs.getString("fB");
fc = rs.getString("fC");
if("".equals(fa)){
System.out.println("0\t"+fb+fc);
getFile.out.write("0\t"+fb+fc);
}else {
if(fa.indexOf(";") != -1){
double sum = 0;
String[] temp = fa.split(";");
for(int i = 0 ; i < temp.length; i ++){
sum += getPrice(temp[i]);
}
System.out.println(sum + "\t"+fb+fc);
getFile.out.write(sum + "\t"+fb+fc);
}else {
System.out.println(getPrice(fa)+"\t"+fb+fc);
getFile.out.write(getPrice(fa)+"\t"+fb+fc) ;
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null)
rs.close();
if(pstmt != null)
pstmt.close();
if(con != null)
con.close();
}catch(Exception e){}
}
}
private static Map<String,Double> price = new HashMap<String,Double>(0);
static {
price.put("apple", 2.5);
price.put("orange", 3d);
price.put("banana", 2.5);
}
private static double getPrice(String name){
Double d = price.get(name);
return d == null ? 0 : d.doubleValue();
}
private static void getFile(){
File f = new File("c:\\out.txt") ;
OutputStream out = null ;
try
{
out = new FileOutputStream(f) ;
}
catch (Exception e)
{
}
if(rs==null) {
try
{
out.close() ;
}
catch (Exception e)
{
}
}
}
}
py330316117 2009-10-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cangyingzhijia 的回复:]
搞不懂。为什么高分贴出这种毫无价值的问题
[/Quote]
小白一边去,人家问问题你牛b,倒是最先给答案啊,给不出答案还在那说风凉话,最瞧不起这样的人
冰思雨 2009-10-30
  • 打赏
  • 举报
回复
10的朋友可能对OOP理解很深,看什么代码都会想到OOP。
首先,我写的代码并不是要证明什么OOP,或者穷显什么。
其次,多写了几个方法,目的是把整个流程用分成较小的步骤,依次处理。
我觉得这样会使思路更清晰,当然,你也可能会觉得更混乱。
第三,数据库资源的使用方面,当初编码的时候确实没有想仔细,因为,我并不是要OOP,
只是想随便写点代码,把楼主的问题表述清楚就可以了。
如果真要是严格按照那个什么OOA、OOP来编程的话,一个类是搞不定的。
第四,关于那个doAction重复调用会出问题的问题。我当时倒是意识到了,但是,却懒得再改了。
毕竟,我写的代码,只是想抛砖引玉,简单运行一次,能出结果就行了,
况且,并非所有类的每一个public方法都可以重复调用2次以上的,
关键是你要懂得在什么样的情况下,怎样使用这些方法。
还有一点,你没编程,你可能没有意识,如果真要是提供一个closeDB的方法来释放资源,
那么,当doAction抛出异常以后,后面的closeDB方法,很有可能会执行不到。当然了,
你要是像我那样捕获异常,然后finally调用closeDB就能解决这个问题,
但是,这同样也是方法的使用问题。
最后,还是要真诚地感谢一下10楼兄弟的宝贵意见,没有这些珍贵的点评,我们很难真正的进步。
上面啰嗦这么多,也是怕大家瞧不起我,或者产生什么误会,把我当时的想法说清楚。

我倒是有一个比较认真构思过的代码,当然,有些地方还是有不足之处的。
欢迎10楼的朋友,有空,再给我提提意见。
地址是:关于TCP长连接的一些简单代码
lsh902 2009-10-29
  • 打赏
  • 举报
回复
哪位高手!帮帮忙给改改啊!
lz12366007 2009-10-29
  • 打赏
  • 举报
回复
mark下。。。帮你顶下。。。
冰思雨 2009-10-29
  • 打赏
  • 举报
回复
我的代码一共有两个循环结构,楼主说哪个循环有问题 ?
我想我的代码,如果楼主能够看懂的话,应该可以就实际问题进行改写了。
所以,我认为,这90分,可能会白送人了。

下面更正一下楼主说的,“没有业务内容的输出的不是0”的逻辑错误,
因为当时楼主给的数据里面没有这种情况,或者是我没有仔细看数据内容,
所以忽略了这种常见的逻辑错误。
只需更改getPriceSum方法就可以了。
   
private double getPriceSum(String services) {
String sub [] = services.split("\\;|\\;");
if(sub==null || sub.length<=0)return 0;
double ps = 0;
for(String s :sub){
if(null==price.get(s))continue;//没有所需业务不进行价格累加
ps+=price.get(s);
}
return ps;
}

lsh902 2009-10-29
  • 打赏
  • 举报
回复
没有价格表!单价都是人为规定的!第二段代码,可以跑起来,可是如果业务没有内容的记录,也会输出2.5,而不是0,循环写的也有问题,输出有很多重复的!
xiandulina 2009-10-29
  • 打赏
  • 举报
回复
public class ActionService {

private Map<String, Double> price = new HashMap<String, Double>(0);
private Connection conn = null;
private PreparedStatement pstm = null;
private ResultSet rs = null;

private String driverClass = "com.mysql.jdbc.Driver";// 可根据实际情况更改
private String url = "jdbc:mysql://localhost:3306/test"; // 可根据实际情况更改
private String user = "root"; // 可根据实际情况更改
private String password = "123"; // 可根据实际情况更改

public ActionService() throws ClassNotFoundException, SQLException {
initDataBase();
initPrice();
}

private void initDataBase() throws ClassNotFoundException, SQLException {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
}

private void initPrice() {
// 苹果=2.5元;橘子=3元;香蕉=2.5元;西瓜=3元
price.put("apple", 2.5);
price.put("orange", 3d);
price.put("banana", 2.5);
}

public void doAction() throws SQLException {
try {
doService();
} catch (SQLException e) {
try {
if (rs != null)
rs.close();
} catch (SQLException e1) {
}
try {
if (pstm != null)
pstm.close();
} catch (SQLException e1) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException e1) {
}
throw e;
}
}

private void doService() throws SQLException {
String sql = "select service,num from SERIVCE";
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
double priceSum = 0;
String services = rs.getString(1);// 业务
String serviceNumber = rs.getString(2);// 业务员编号
if (services == null || services.length() <= 0)
priceSum = 0;
priceSum = getPriceSum(services);
System.out.println(priceSum + "\t" + serviceNumber);
}
}

private double getPriceSum(String services) {
double ps = 0;
if (services != null && !"".equals(services)) {
String sub[] = services.split("\\;|\\;");
for (String s : sub) {
ps += price.get(s);
}
}
return ps;
}

public static void main(String[] args) throws Exception {
ActionService as = new ActionService();
as.doAction();
}

}
lsh902 2009-10-29
  • 打赏
  • 举报
回复
感谢yuzuru的详细讲解!明天给分!
yuzuru 2009-10-29
  • 打赏
  • 举报
回复
1、错误在于getPrice()方法不是static,不能直接在main里调用。static方法里只能用class的static成员和局部变量。把getPrice()和上面那4个int价格定义成static就可以了。
2、getPrice()方法里是三目运算符“? :”的嵌套,相当于if - elseif - elseif - else。推荐不要使用运算符嵌套,代码可读性太差。这里要注意:写代码,不是行数越少越好。
3、把watermelon赋值为0,不要用4。
4、1楼的代码,其实是有问题的,你大概被他封装了几个方法、又调来调去的唬住了。
首先,你这只是个sql的练习而已,没有封装成类和方法的必要,不是弄个类写几个方法就是OOP了。
其次,就那里面的方法而言,如果是在构造里打开Connection,那就必须提供一个方法用于close,而不是在执行一个查询方法里close。如果在main方法里调用2次as.doAction(),第一次之后Connection已经close,第二次怎么办?
5、1楼的代码有一个你可以借鉴的地方,就是用Map存放price。用这个来改造你的getPrice方法。

private static Map<String,Double> price = new HashMap<String,Double>(0);
static {
price.put("apple", 2.5);
price.put("orange", 3d);
price.put("banana", 2.5);
}
private static double getPrice(String name){
Double d = price.get(name);
return d == null ? 0 : d.doubleValue();
}

苍蝇①号 2009-10-29
  • 打赏
  • 举报
回复
搞不懂。为什么高分贴出这种毫无价值的问题
秦π 2009-10-28
  • 打赏
  • 举报
回复
private void initPrice() {
//苹果=2.5元;橘子=3元;香蕉=2.5元;西瓜=3元
price.put("苹果", 2.5);
price.put("橘子", 3d);
price.put("香蕉", 2.5);
price.put("西瓜", 3d);
}
不知道你这些东西的价格存在什么地方 为什么固定在程序里hard code方式
单一张表几率物品的价格
业务和这个表多对多的关系 通过业务编码 找到对应的一个或是多个的price 进行sum group by 业务不就行了吗
加载更多回复(2)

62,620

社区成员

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

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