简体   繁体   English

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

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

What is wrong in this Java JDBC program? 这个Java JDBC程序有什么问题?

I have written a method that takes a ResultSet and prints all of its records. 我编写了一个方法,该方法采用ResultSet并打印其所有记录。

My SQL query is: 我的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;

When I am executing this query in MySQL Workbench, it returns: 当我在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)

But when I am executing this query in Java and printing all data of ResultSet, it returns like: 但是,当我用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

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

My question is: 我的问题是:

Why it is giving zero (0) for column 1,9,10,11,12 and 13 为什么为列1,9,10,11,12和13赋予零(0)

Tables structure 表格结构

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)
);

My code to print this: 我的代码来打印此:

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;
    }
}

I tried both the things ... 我都尝试了两件事...

1) 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) 2)

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

But Unfortunately , Both did not worked for me... 但是不幸的是,两者都没有为我工作...

You haven't shown us how you've constructed the string sql in your Java code, so I'm going to have to guess. 您尚未向我们展示如何在Java代码中构造字符串sql ,所以我将不得不猜测。

It seems that sql contains your query from Select * onwards, without the line Set @counter := 0 ... . 看来sql包含从Select *开始的查询,而没有Set @counter := 0 ... I was able to reproduce your output if I set sql to this. 如果我将sql设置为此,则可以重现您的输出。 Perhaps you've tried putting that Set line in the query as well and that generated an error so you took it out? 也许您也尝试过在查询中放置该Set行,并且生成了一个错误,所以您将其删除了?

What you need to do instead is to execute the Set @counter := 0 ... line and then run the query. 相反,您需要执行Set @counter := 0 ...行,然后运行查询。 In other words, replace the lines 换句话说,替换行

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

with

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

I made this modification to your code and it generated the output you wanted. 我对您的代码进行了修改,并生成了所需的输出。

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

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