62,620
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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)
{
}
}
}
}
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;
}
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();
}
}
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();
}