SQL 数据库中有“表1”有多个字段,现在提取出一下字段-->有N种产品,日期为datepart()出来的一个月内(1—31)的日期,但也有区间和断开的日期,员工编号每天不固定,员工单日业务量有多有少。需求:1.将单日的所有员工的业务量相加,单独一个字段体现。2.IO输出.txt文件。
表1
A B C D
产品 | 日期 |员工编号 | 员工单日业务量| 产品 | 日期 |员工编号 | 单日员工业务量|当日业务总量|
DELL 1 002 15 DELL 1 002 15 53
DELL 1 003 16 DELL 1 003 16 53
DELL 1 004 22 DELL 1 004 22 53
DELL 2 002 7 DELL 2 002 7 50
DELL 2 003 22 DELL 2 003 22 50
DELL 2 005 21 DELL 2 005 21 50
DELL ..至31 ... ... IO输出.txt文件 DELL ...至31 ... ... ...
IBM 5 003 30 ——————>> IBM 5 003 30 54
IBM 5 004 10 IBM 5 004 10 54
IBM 5 005 14 IBM 5 005 14 54
IBM 6 003 5 IBM 6 003 5 26
IBM 6 006 21 IBM 6 006 21 26
IBM ...至25 ... ... IBM ...至25 ... ... ...
苹果 21 004 17 苹果 1 004 17 33
苹果 21 005 10 苹果 1 005 10 33
苹果 21 006 6 苹果 1 006 6 33
苹果 23 002 7 苹果 2 002 7 13
苹果 23 003 6 苹果 2 003 6 13
苹果 ...至27 ... ... 苹果 ...至27 ... ... ... 思路:首先sql="select 产品 as A,datepart(day,日期) as B,员工编号 as C,员工单日业务量 as D from 表1 where ......"得出上面的结构。然后再sql2="select 产品 as E,datepart(day,日期) as F,sum(员工单日业务量) as G from 表1 where ......"得出以下结构
E | F | G |
DELL 1 53
DELL 2 50
DELL 3 ...
DELL ... ...
DELL 31 ...
IBM 5 54
IBM 6 26
IBM ... ...
苹果 21 33
苹果 23 13
苹果 ... ...
之后判断当A=E&&B=F时将G循环输出。大家帮忙写写方法。有什么好的写法能提高效能。
表1
A B C D
产品 | 日期 |员工编号 | 员工单日业务量| 产品 | 日期 |员工编号 | 单日员工业务量|当日业务总量|
DELL 1 002 15 DELL 1 002 15 53
DELL 1 003 16 DELL 1 003 16 53
DELL 1 004 22 DELL 1 004 22 53
DELL 2 002 7 DELL 2 002 7 50
DELL 2 003 22 DELL 2 003 22 50
DELL 2 005 21 DELL 2 005 21 50
DELL ..至31 ... ... IO输出.txt文件 DELL ...至31 ... ... ...
IBM 5 003 30 ——————>> IBM 5 003 30 54
IBM 5 004 10 IBM 5 004 10 54
IBM 5 005 14 IBM 5 005 14 54
IBM 6 003 5 IBM 6 003 5 26
IBM 6 006 21 IBM 6 006 21 26
IBM ...至25 ... ... IBM ...至25 ... ... ...
苹果 21 004 17 苹果 1 004 17 33
苹果 21 005 10 苹果 1 005 10 33
苹果 21 006 6 苹果 1 006 6 33
苹果 23 002 7 苹果 2 002 7 13
苹果 23 003 6 苹果 2 003 6 13
苹果 ...至27 ... ... 苹果 ...至27 ... ... ... 思路:首先sql="select 产品 as A,datepart(day,日期) as B,员工编号 as C,员工单日业务量 as D from 表1 where ......"得出上面的结构。然后再sql2="select 产品 as E,datepart(day,日期) as F,sum(员工单日业务量) as G from 表1 where ......"得出以下结构
E | F | G |
DELL 1 53
DELL 2 50
DELL 3 ...
DELL ... ...
DELL 31 ...
IBM 5 54
IBM 6 26
IBM ... ...
苹果 21 33
苹果 23 13
苹果 ... ...
之后判断当A=E&&B=F时将G循环输出。大家帮忙写写方法。有什么好的写法能提高效能。
解决方案 »
- 关于实现资源管理器界面的问题
- 帮我分析下这个程序
- swing问题,textfield可不可以做table单元格的渲染器
- 请教java的学习方向问题~谢谢高手指点~
- 请教一个关于Socket问题, 急, 谢谢!
- 高分请教一些关于MVC模式的问题﹐关注有分﹐会再开贴子加分﹐加分无上限
- 忘了二维数组怎么取下标? 请赐教
- 一个图形文件,如一个JPG图形文件,怎么才能通过流读进来,然后转化成一个字节数组。
- 求问诸位大虾及英语高手
- 我想学JAVA,我C++自认还可以,哪本书可以介绍给我?我希望它偏向实际应用的至少要让我知道JAV能干什么?
- 问一个用java编写三子棋游戏的问题
- Java Swing JTable 添加标题
public static void main(String args[]){
String url = "";
String user = "";
String password = "";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql="select 产品 as A,datepart(day,日期) as B,员工编号 as C,员工单日业务 量 as D from 表1 where ......";
String sql2="select 产品 as E,datepart(day,日期) as F,sum(员工单日业务量) as G from 表1 where ......";
try{
Class.forName("");
con = DriverManager.getConnection(url,user,password);
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
String A = "";
String B = "";
String C = "";
String D = "";
String E = "";
String F = "";
String G = "";
while(rs.next()){
//大家帮忙看看循环怎么写合适??? }catch(Exception e){
e.printStackTrace();
}finally{
//统一出口怎么写??
}
static void Outio(){}//IO输出的方法怎么写合适??
static void Getall(){}//运算方法??
}
最好写批量的 SQL 条件语句。
爱默难助啊
一是,把两个SQL语句,整合成一条SQL语句。
(可以选择自连接查询,或嵌套查询。推荐采用连接查询)
二是,创建一个临时表,作为辅助查询使用。
等数据出来之后,将临时表删除(SQL Server创建的系统临时表可以交给数据库系统自行维护,
其他的数据库我不太清楚)。
SQL语句能合成一句就合成一句
最好优化下sql语句
我认为你的 需求完全可以拿一条复合 查询就能查出来的。
看看这个
select 表1.产品,表1.日期,表1.员工编号,表1.员工单日业务量,t.sumall
from 表1,(
select 产品,日期,sum(员工单日业务量) as sumall
from 表1
group by 产品,日期) t
where 表1.产品 = t.产品
and 表1.日期 =t.日期
order by 表1.产品,表1.日期,表1.员工编号;
from 表1,(
select 产品,日期,sum(员工单日业务量) as sumall
from 表1
group by 产品,日期) t
where 表1.产品 = t.产品
and 表1.日期 =t.日期
order by 表1.产品,表1.日期,表1.员工编号;
//大家帮忙看看循环怎么写合适???String password = "";//定义一个lst来存所有取出来的数据
ArrayList<ClassObj> lst = new ArrayList<ClassObj>();Connection con = null;while (rs.next()) {
//ClassObj 可以对应于你那个表格的每一行数据的一个类。
ClassObj obj = new ClassObj(); obj.setClassId(String.valueOf(rs
.getBigDecimal("A")));
obj.setClassGroup(rs.getString("B"));
obj.setClassNo(String.valueOf(rs.getInt("C")));
obj.setClassName(rs.getString("D"));
obj.setClassValue(rs.getString("E"));
obj.setStatus(rs.getString("F").trim()); lst.add(obj);
}
}finally{
//统一出口怎么写??
finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
cs.close();
}
if (con != null){
con.close();
}
} catch (SQLException e) {
throw new ComicException(e);
}
}static void Outio(){}//IO输出的方法怎么写合适??
另外建一个类
//fileName要保存的文件名,全路径名 contents你根据lst组织的内容。
public static void saveFile(String fileName, String contents)
throws IOException {
File f = new File(fileName); if (f.exists()){
throw new IOException("file exists...");
}
if (!f.createNewFile()) {
throw new IOException("file create failure...");
} try {
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(contents);
output.close();
} catch (Exception e) {
throw new IOException(e);
}
} //将lst转为contents
public static String formatLst(ArrayList<ClassObj> lst){
String contents = "";
for(int i=0;i<lst.size();i++){
contents +=lst.get(i).getClassId+...+"\n";
}
return contents;
} static void Getall(){}//运算方法?? 不论你要什么运算方法,基本上都可以从数据库层面来考虑做到的。如果没有必要,还是不要用循环在代码里面去做的好。
import java.util.*;
import java.io.*;public class test{
public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:study";
String user = "sa";
String password = "";
ArrayList <ClassObj> lst = new ArrayList <ClassObj>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select 业务 as A,日期 as B,员工编号 as C from 表1";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url,user,password);
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery(); while(rs.next()){
ClassObj obj = new ClassObj();
obj.setClassId(String.valueOf(rs.getBigDecimal("A")));
obj.setClassGroup(rs.getString("B"));
obj.setClassNo(String.valueOf(rs.getInt("C")));
lst.add(obj);
}
}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){}
}
}
static void saveFile(String fileName, String contents) //怎么保存文件路径和文件名?
throws IOException {
File f = new File(fileName); if (f.exists()){
throw new IOException("file exists...");
}
if (!f.createNewFile()) {
throw new IOException("file create failure...");
} try {
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(contents);
output.close();
} catch (Exception e) {
throw new IOException(e);
}
} //将lst转为contents
static String formatLst(ArrayList <ClassObj> lst){
String contents = "";
for(int i=0;i <lst.size();i++){
contents +=lst.get(i).getClassId+"\t"+lst.get(i).getClassGroup+"\t"+lst.get(i).getClassNo+"\n";
}
return contents;
}
}
}
SQLserver的话,url= jdbc:microsoft:sqlserver://172.16.7.167:1433;selectMethod =cursor;databaseName=test1
ip要变,test1改为你自己的数据库名。这里也要改:
class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");这里也要改。你的数据库里面,字段对应的类型在这里要一致!
varchar类型用getString,int类型用getInt,大数字用getBigDecimal
obj.setClassId(String.valueOf(rs.getBigDecimal("A")));
obj.setClassGroup(rs.getString("B"));
obj.setClassNo(String.valueOf(rs.getInt("C")));
我用ODBC连的数据库,以前也是这么联的,应该没问题。数据类型也清楚,关键是IO输出???
运行起来编译出错,说是好像缺少‘class’‘interface’之类的错误。麻烦给从总体上仔细改改。
你有没有写ClassObj类啊??
按照这一段,你应该定义自己的数据bean啊:
while(rs.next()){
ClassObj obj = new ClassObj();
obj.setClassId(String.valueOf(rs.getBigDecimal("A")));
obj.setClassGroup(rs.getString("B"));
obj.setClassNo(String.valueOf(rs.getInt("C")));
lst.add(obj);
}public class ClassObj {
private String classId; private String classGroup; private String classNo; private String className; private String classValue; private String status; public String getClassGroup() {
return classGroup;
} public void setClassGroup(String classGroup) {
this.classGroup = classGroup;
} public String getClassId() {
return classId;
} public void setClassId(String classId) {
this.classId = classId;
} public String getClassName() {
return className;
} public void setClassName(String className) {
this.className = className;
} public String getClassNo() {
return classNo;
} public void setClassNo(String classNo) {
this.classNo = classNo;
} public String getClassValue() {
return classValue;
} public void setClassValue(String classValue) {
this.classValue = classValue;
} public String getStatus() {
return status;
} public void setStatus(String status) {
this.status = status;
} public String toString() {
return "ClassObj{classId=" + this.classId + ",classGroup="
+ this.classGroup + ",classNo=" + this.classNo + ",className="
+ this.className + ",classValue=" + this.classValue
+ ",status=" + this.status + "}";
}
}
test6.java:66: 找不到符号
符号: 变量 getClassId
位置: 类 ClassObj
contents +=lst.get(i).getClassId+lst.get(i).getClassGroup+lst.get(i).getClassNo+"\n";
^
test6.java:66: 找不到符号
符号: 变量 getClassGroup
位置: 类 ClassObj
contents +=lst.get(i).getClassId+lst.get(i).getClassGroup+lst.get(i).getClassNo+"\n";
^
test6.java:66: 找不到符号
符号: 变量 getClassNo
位置: 类 ClassObj
contents +=lst.get(i).getClassId+lst.get(i).getClassGroup+lst.get(i).getClassNo+"\n";
^
3 错误
import java.sql.*;
import java.util.*;
import java.io.*;public class test6 extends ClassObj{
public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:study";
String user = "sa";
String password = "";
ArrayList <ClassObj> lst = new ArrayList <ClassObj>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select callstatus as A,opcode as B,rid as C from gxyd_cyt_result where wdate>'2009-11-16 12:30' and wdate<'2009-11-16 13:00'";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url,user,password);
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery(); while(rs.next()){
ClassObj obj = new ClassObj();
obj.setClassId(rs.getString("A"));
obj.setClassGroup(rs.getString("B"));
obj.setClassNo(rs.getString("C"));
lst.add(obj);
}
}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){}
}
}
static void saveFile(String fileName, String contents)
throws IOException {
File f = new File(fileName); if (f.exists()){
throw new IOException("file exists...");
}
if (!f.createNewFile()) {
throw new IOException("file create failure...");
} try {
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(contents);
output.close();
} catch (Exception e) {
}
} //将lst转为contents
static String formatLst(ArrayList <ClassObj> lst){
String contents = "";
for(int i=0;i <lst.size();i++){
contents +=lst.get(i).getClassId+lst.get(i).getClassGroup+lst.get(i).getClassNo+"\n";
}
return contents;
}
} class ClassObj {
private String classId; private String classGroup; private String classNo; public String getClassGroup() {
return classGroup;
} public void setClassGroup(String classGroup) {
this.classGroup = classGroup;
} public String getClassId() {
return classId;
} public void setClassId(String classId) {
this.classId = classId;
}
public String getClassNo() {
return classNo;
} public void setClassNo(String classNo) {
this.classNo = classNo;
} public String toString() {
return "ClassObj{classId=" + this.classId + ",classGroup="
+ this.classGroup + ",classNo=" + this.classNo + "}";
}
}
return "ClassObj{classId=" + this.classId + ",classGroup="
+ this.classGroup + ",classNo=" + this.classNo + "}";
}然后在main的最后加入:
try{
String contents = formatLst(lst);
test6.saveFile("D:/输出.txt",contents );
}catch(Exception ex1){
ex1.printStackTrace();
}你要在main函数里面调用你写的方法啊
符号: 变量 getClassGroup
位置: 类 ClassObj
contents +=lst.get(i).getClassId+lst.get(i).getClassGroup+lst.get(i).getClassNo+"\n";
^
不知道是不是位置加的不对??
import java.sql.*;
import java.util.*;
import java.io.*;public class test6 {
public static void main(String[] args) throws Exception {
try {
ArrayList<ClassObj> lst = test6.getResultLst();
String contents = test6.formatLst(lst);
String fileName = "E:\\result.txt";
test6.saveFile(fileName, contents);
} catch (Exception e) {
e.printStackTrace();
}
} static ArrayList<ClassObj> getResultLst() throws Exception {
String url = "jdbc:odbc:study";
String user = "sa";
String password = "";
ArrayList<ClassObj> lst = new ArrayList<ClassObj>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "select callstatus as A,opcode as B,rid as C from gxyd_cyt_result where wdate>'2009-11-16 12:30' and wdate<'2009-11-16 13:00'";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, user, password);
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery(); while (rs.next()) {
ClassObj obj = new ClassObj();
obj.setClassId(rs.getString("A"));
obj.setClassGroup(rs.getString("B"));
obj.setClassNo(rs.getString("C"));
lst.add(obj);
}
} catch (Exception e) {
throw e;
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return lst;
} static void saveFile(String fileName, String contents) throws IOException {
File f = new File(fileName); if (f.exists()) {
throw new IOException("file exists...");
}
if (!f.createNewFile()) {
throw new IOException("file create failure...");
} try {
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(contents);
output.close();
} catch (Exception e) { }
} static String formatLst(ArrayList<ClassObj> lst) {
String contents = "";
for (int i = 0; i < lst.size(); i++) {
contents += lst.get(i).getClassId() + lst.get(i).getClassGroup()
+ lst.get(i).getClassNo() + "\n";
}
return contents;
}}class ClassObj {
private String classId; private String classGroup; private String classNo; public String getClassGroup() {
return classGroup;
} public void setClassGroup(String classGroup) {
this.classGroup = classGroup;
} public String getClassId() {
return classId;
} public void setClassId(String classId) {
this.classId = classId;
} public String getClassNo() {
return classNo;
} public void setClassNo(String classNo) {
this.classNo = classNo;
} public String toString() {
return "ClassObj{classId=" + this.classId + ",classGroup="
+ this.classGroup + ",classNo=" + this.classNo + "}";
}
}
contents += lst.get(i).getClassId() +"\t"+ lst.get(i).getClassGroup()
+"\t"+ lst.get(i).getClassNo() + "\n";
IO可以正常输出了,但是数据没有换行,制表可以实现,可是\n没有任何作用,数据全都在以行显示。
另外我还有别的问题,我另外开帖提问了,还要麻烦您看看。