繁体   English   中英

这个Java JDBC程序有什么问题?

[英]What is wrong in this Java JDBC program?

这个Java JDBC程序有什么问题?

我编写了一个方法,该方法采用ResultSet并打印其所有记录。

我的SQL查询是:

Set @counter := 0, @counterQty := 0, @counterAvb := 0, @counterIss := 0, @counterRep := 0 ,@counterDes := 0;
Select *
From (SELECT 
       (Select (@counter := (@counter + 1) ) ) 'Sr.No.',
       testt.BookName,
       testt.BookQty,
       testt.Code, 
       testt.Available, 
       testt.Issued, 
       testt.Repair, 
       testt.Destroyed,
       (@counterQty := @counterQty + testt.BookQty ) TotalQty,
       (@counterAvb := @counterAvb + testt.Available ) TotalAvb,
       (@counterIss := @counterIss + testt.Issued ) TotalIss,   
       (@counterRep := @counterRep + testt.Repair ) TotalRep,   
       (@counterDes := @counterDes + testt.Destroyed ) TotalDest
     From (Select a.b_name BookName, a.b_qty BookQty, a.b_acc_id Code,
            SUM(case  when b.status='A' then 1 else 0 end) as Available,
            SUM(case  when b.status='I' then 1 else 0 end) as Issued, 
            SUM(case  when b.status='R' then 1 else 0 end) as Repair, 
            SUM(case  when b.status='D' then 1 else 0 end) as Destroyed      
          From tbl_book_info a left join tbl_books b on a.b_acc_id = b.accid 
          GROUP BY a.b_name, a.b_qty, a.b_acc_id order by a.b_acc_id
     )testt
)Main;

当我在MySQL Workbench中执行此查询时,它返回:

    +--------+--------------+---------+-------+-----------+--------+--------+-----------+----------+----------+----------+----------+-----------+
    | Sr.No. | BookName     | BookQty | Code  | Available | Issued | Repair | Destroyed | TotalQty | TotalAvb | TotalIss | TotalRep | TotalDest |
    +--------+--------------+---------+-------+-----------+--------+--------+-----------+----------+----------+----------+----------+-----------+
    |      1 | Java book    |       3 | 10001 |         0 |      0 |      1 |         2 |        3 |        0 |        0 |        1 |         2 |
    |      2 | Cpp Book     |       5 | 10002 |         3 |      1 |      0 |         1 |        8 |        3 |        1 |        1 |         3 |
    |      3 | Cpp 1.17     |       5 | 10003 |         3 |      1 |      0 |         1 |       13 |        6 |        2 |        1 |         4 |
    |      4 | Visual Basic |       4 | 10004 |         4 |      0 |      0 |         0 |       17 |       10 |        2 |        1 |         4 |
    +--------+--------------+---------+-------+-----------+--------+--------+-----------+----------+----------+----------+----------+-----------+
    4 rows in set (0.25 sec)

但是,当我用Java执行此查询并打印ResultSet的所有数据时,它返回如下:

        1       2               3         4         5           6       7         8         9           10          11        12          13                    
    +--------+--------------+---------+-------+-----------+--------+--------+-----------+----------+----------+----------+----------+-----------+
    | Sr.No. | BookName     | BookQty | Code  | Available | Issued | Repair | Destroyed | TotalQty | TotalAvb | TotalIss | TotalRep | TotalDest |
    +--------+--------------+---------+-------+-----------+--------+--------+-----------+----------+----------+----------+----------+-----------+

       0       Cpp 1.17          5       10003          3     1          0          1          0          0          0          0          0

       0       Cpp Book          5       10002          3     1          0          1          0          0          0          0          0

       0       Java book         3       10001          0     0          1          2          0          0          0          0          0

       0       Visual Basic      4       10004          4     0          0          0          0          0          0          0          0

    +--------+--------------+---------+-------+-----------+--------+--------+-----------+----------+----------+----------+----------+-----------+

我的问题是:

为什么为列1,9,10,11,12和13赋予零(0)

表格结构

create table if not exists tbl_book_info(
    b_acc_id int(5) not null auto_increment,
    b_name varchar(50) not null,
    b_qty int(2) not null,
    b_type varchar(30) not null,
    b_auth1 varchar(50) not null,
    b_auth2 varchar(50),
    b_pub varchar(50) not null,
    b_pages int(4) not null,
    b_rack int(5) not null,
    b_price  Decimal(6,2) not null,
    b_about text,
    primary key(b_acc_id)
);

create table if not exists tbl_books(   
    accid int(5) references tbl_book_info.b_acc_id,
    accno int(3),
    status varchar(1) default "A",
    primary key(accid,accno)
);

我的代码来打印此:

Connection con = getDbConnObj();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery( sql );
printRsLast(rs);

public void printRsLast(ResultSet rs){
/*  +---1----+-----2--------+----3----+--4----+-----5-----+----6---+---7----+-----8-----+-----9----+----10----+----11----+----12----+-----13----+
    | Sr.No. | BookName     | BookQty | Code  | Available | Issued | Repair | Destroyed | TotalQty | TotalAvb | TotalIss | TotalRep | TotalDest |
rs= +--------+--------------+---------+-------+-----------+--------+--------+-----------+----------+----------+----------+----------+-----------+
    |      1 | Java book    |       3 | 10001 |         1 |      0 |      0 |         2 |        3 |        1 |        0 |        0 |         2 |
    |...........................................................................................................................................|
    |...........................................................................................................................................|
    |...........................................................................................................................................|
    +--------+--------------+---------+-------+-----------+--------+--------+-----------+----------+----------+----------+----------+-----------+   */


    String separator = "          ";
    try{
        rs.beforeFirst();
        int n=0;

        p("$$$ RS Attr. are : \n\n Sr.No. | BookName     | BookQty | Code  | Available | Issued | Repair | Destroyed | TotalQty | TotalAvb | TotalIss | TotalRep | TotalDest , Row are...\n");

        while(rs.next()){
            n++;
            String nm , m;

            m = rs.getInt(1)+ separator + rs.getString(2)+ separator + rs.getInt(3)+ separator + rs.getInt(4)+ separator + rs.getInt(5)+ separator + rs.getInt(6)+ separator + rs.getInt(7)+ separator + rs.getInt(8)+ separator + rs.getInt(9)+ separator + 
                rs.getInt(10)+ separator + rs.getInt(11)+ separator + rs.getInt(12)+ separator + rs.getInt(13);
            p(m+"\n");
        }
        p("\nTotal Rows = "+n);
    } catch(Exception e){
        p("\n!!! Excep in 'printRsLast(ResultSet rs), msg = '"+e.getMessage());
    }
}

public Connection getDbConnObj() {
    // Creating 'Connection' class' Reference Variable ...
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/librarydb";
    String dbUname = "root";
    String dbPass = "";
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        con = DriverManager.getConnection(url, dbUname, dbPass);
    } catch (Exception e) {
        con = null;
    } finally {
        return con;
    }
}

我都尝试了两件事...

1)

sql = "Set @counter := 0...";
    rs = st.execute(sql);       //  Execute this 'Set @counter...' Stmt first Than

    sql = "Select * From...";
    rs = st.executeQuery(sql);  //  Executing this 'Select * from...' Stmt to get the Tabular Data...
    printRsLast(rs);

2)

  sql = "Set @counter := 0...
            ....
            ....
            )Main;" 
    rs = st.executeQuery(sql);  //  Executing these Entire Stmt to get the Tabular Data...
    printRsLast(rs);

但是不幸的是,两者都没有为我工作...

您尚未向我们展示如何在Java代码中构造字符串sql ,所以我将不得不猜测。

看来sql包含从Select *开始的查询,而没有Set @counter := 0 ... 如果我将sql设置为此,则可以重现您的输出。 也许您也尝试过在查询中放置该Set行,并且生成了一个错误,所以您将其删除了?

相反,您需要执行Set @counter := 0 ...行,然后运行查询。 换句话说,替换行

Connection con = getDbConnObj();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery( sql );
printRsLast(rs);

Connection con = getDbConnObj();
Statement st = con.createStatement();
st.execute("Set @counter := 0 ...");
ResultSet rs = st.executeQuery( sql );
printRsLast(rs);

我对您的代码进行了修改,并生成了所需的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM