gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:29:09 25 4
gpt4 key购买 nike

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

我对您的代码进行了此修改,它生成了您想要的输出。

关于java - 这个 Java JDBC 程序有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57240234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com