简体   繁体   中英

There's something wrong with my JDBC, but I don't know why

My MySQL JDBC doesn't run. I setup a MySQL database and tried to borrow books from it, but the program terminated before I typed card_ number . But there is a console output with a stack trace.

Stack trace:

java.sql.SQLException: Illegal operation on empty result set.

My JDBC class:

package dblab;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.PreparedStatement;

public class dblab5 {

    static Connection conn;

    public static void main(String args[]){

        start("root","991108");
    }

    public static void start(String userid, String passwd) { 
        String url = "jdbc:mysql://localhost:3306/lab5?"
                + "user=root&password=991108&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT";

        try{ 
            Class.forName ("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url);
            Statement stmt = conn.createStatement();

            System.out.println("********************************************************");
            System.out.println("\t\t\t图书管理系统");
            System.out.println("********************************************************");

            Scanner reader=new Scanner(System.in);

            while(true){
            System.out.println("1.图书查询 2.借书 3.还书 4.图书入库 5.借阅证管理 0.退出系统");
            System.out.println("请输入需要的服务编号:");
            int choice=reader.nextInt();
            switch(choice)
            {
                case 0 :
                    conn.close();
                    return;
                case 1:
                    check_Book();
                    break;
                case 2 :
                    borrow_Book();
                    break;
                case 3:
                    return_Book();
                    break;
                case 4:
                    add_Book();
                    break;
                case 5:
                    proof_Manag();
                    break;
                default:
                    System.out.println("服务编号错误");
            }
            }
        }catch (Exception sqle){ 
            System.out.println("Exception : " + sqle);
        }
    }

    static void check_Book() throws SQLException
    {
        String query;//查询语句
        int choice=0;//存放用户选项

        Scanner reader=new Scanner(System.in);

        while(true){
        System.out.println("1.查询全部 2.按书名查询 3.按书号查询 0.退出");
        System.out.println("请输入需要的服务编号");
        choice=reader.nextInt();
        switch(choice)
        {
            case 0 :
                return;
            case 1:
                //执行SQL语句
                query="SELECT * FROM book";

                Statement stmt = conn.createStatement();        
                ResultSet rset = stmt.executeQuery(query);

                System.out.println("查询结果如下");
                System.out.println("********************************************************************************");
                System.out.println("书号\t类别\t书名\t出版社\t年份\t作者\t价格\t总藏书量\t库存");
                System.out.println("********************************************************************************");

                while (rset.next()) { 
                    System.out.println(rset.getString("bno")+"\t"+rset.getString("category")+
                            "\t"+rset.getString("title")+"\t"+rset.getString("press")+
                            "\t"+rset.getInt("year")+"\t"+rset.getString("author")+
                            "\t"+rset.getDouble("price")+"\t"+rset.getInt("total")+"\t"+rset.getInt("stock"));

                }
                stmt.close();

                break;  
            default:
                System.out.println("服务编号错误");

        }
        }
    }
    static void borrow_Book() throws SQLException
    {
        String query;//查询语句
        int choice=0;//存放用户选项

        Scanner reader=new Scanner(System.in);

        while(true){
        System.out.println("1.继续借书 0.退出");
        System.out.println("请输入需要的服务编号");
        choice=reader.nextInt();
        switch(choice)
        {
            case 0 :
                return;
            default:
                System.out.println("请输入书名及借阅证编号");
                String bookname=reader.nextLine();
                String card_number=reader.nextLine();
                //执行SQL语句
                query="SELECT bno FROM borrow WHERE cno='"+card_number+"'";
                Statement stmt = conn.createStatement();
                ResultSet rset = stmt.executeQuery(query);

                System.out.println("已借出");
                System.out.println("********************************************************************************");
                System.out.println("书号");
                System.out.println("********************************************************************************");
                while (rset.next()) { 
                    System.out.println(rset.getString("bno"));
                }
                query="SELECT* FROM book WHERE title='"+bookname+"'";
                rset = stmt.executeQuery(query);
                int stock=rset.getInt("stock");
                System.out.println(stock);
                if(stock>0){
                    stock--;
                    query="UPDATE TABLE book SET stock=? WHERE bno=?";
                    try{
                    PreparedStatement ps=conn.prepareStatement(query);
                    ps.setInt(1,stock);
                    ps.setString(2,bookname);
                    ps.executeUpdate();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                    query="SELECT *FROM book WHERE title='"+bookname+"'";
                    rset=stmt.executeQuery(query);
                    String bookid=rset.getString("bno");
                    query="INSERT INTO borrow VALUES(?,?,?,?)";
                    try{
                    PreparedStatement ps=conn.prepareStatement(query);
                    ps.setString(1,card_number);
                    ps.setString(2,bookid);
                    System.out.println("请输入当前日期");
                    int date =reader.nextInt();
                    ps.setInt(3,date);
                    //ps.setInt(4,null);
                    ps.executeUpdate();
                }
                catch (SQLException e){
                    e.printStackTrace();
                }
                    System.out.println("借书成功");
                }

                if(stock==0){
                    System.out.println("库存不足");
                    rset=stmt.executeQuery("SELECT max(return_date) FROM borrow");
                    int date =rset.getInt("return_date");
                    System.out.println("最近归还日期为"+date);
                }

                stmt.close();
                break;
        }
        }
    }
    static void return_Book() throws SQLException
    {
        String query;//查询语句
        int choice=0;//存放用户选项

        Scanner reader=new Scanner(System.in);

        while(true){
        System.out.println("1.继续还书 0.退出");
        System.out.println("请输入需要的服务编号");
        choice=reader.nextInt();
        switch(choice)
        {
            case 0 :
                return;
            default:
                System.out.println("请输入书名及借阅证编号");
                String bookname=reader.nextLine();
                String card_number=reader.nextLine();
                //执行SQL语句
                query="SELECT bno FROM borrow WHERE return_date=null";
                Statement stmt = conn.createStatement();        
                ResultSet rset = stmt.executeQuery(query);
                System.out.println("已借出");
                System.out.println("********************************************************************************");
                System.out.println("书号");
                System.out.println("********************************************************************************");
                while (rset.next()) { 
                    System.out.println(rset.getString("bno"));
                }

                query="SELECT stock FROM book WHERE title='"+bookname+"'^cno='"+card_number+"'";
                rset = stmt.executeQuery(query);
                if(rset.wasNull())
                System.out.println("数据错误");
                else{
                    query="SELECT *FROM book WHERE title='"+bookname+"'";
                    rset = stmt.executeQuery(query);
                    int stock=rset.getInt("stock");
                    System.out.println("请输入当前日期");
                    int date=reader.nextInt();
                    query="UPDATE TABLE book SET stock=? WHERE bno=?";
                    try{
                    PreparedStatement ps=conn.prepareStatement(query);
                    ps.setInt(1,stock);
                    ps.setString(2,bookname);
                    ps.executeUpdate();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                    query="UPDATE TABLE borrow SET return_date=? WHERE title=?^cno=?";
                    try{
                    PreparedStatement ps=conn.prepareStatement(query);
                    ps.setInt(1,date);
                    ps.setString(2,bookname);
                    ps.setString(3,card_number);
                    ps.executeUpdate();
                    }
                    catch (SQLException e){
                        e.printStackTrace();
                    }
                    System.out.println("还书成功");
                }
                stmt.close();
                break;
        }
        }
    }
    static void add_Book() throws SQLException
    {
        String query;//查询语句
        int choice=0;//存放用户选项

        Scanner reader=new Scanner(System.in);

        while(true){
        System.out.println("1.单本入库 2.批量入库 0.退出");
        System.out.println("请输入需要的服务编号");
        choice=reader.nextInt();
        switch(choice)
        {
            case 0 :
                return;
            case 1:
                System.out.println("请输入书号,书名,作者,出版社,价格,类别,年份(0退出系统)");
                //书号, 类别, 书名, 出版社, 年份, 作者, 价格, 总藏书量, 库存
                String bno_in=reader.nextLine();
                String title_in=reader.nextLine();
                String author_in=reader.nextLine();
                String press_in=reader.nextLine();
                float price_in=reader.nextFloat();
                String category_in=reader.nextLine();
                int year_in=reader.nextInt();
                //执行SQL语句
                query="SELECT *FROM book WHERE bno='"+bno_in+"'";
                Statement stmt = conn.createStatement();
                ResultSet rset = stmt.executeQuery(query);
                int total=rset.getInt("total");
                int stock=rset.getInt("stock");
                query="INSERT INTO book VALUES(?,?,?,?,?,?,?,?,?)";
                try{
                    PreparedStatement ps=conn.prepareStatement(query);
                    ps.setString(1,bno_in);
                    ps.setString(2,category_in);
                    ps.setString(3,title_in);
                    ps.setString(4,press_in);
                    ps.setInt(5,year_in);
                    ps.setString(6,author_in);
                    ps.setFloat(7,price_in);
                    ps.setInt(8,total+1);
                    ps.setInt(9,stock+1);
                    ps.executeUpdate();
                }
                catch (SQLException e){
                    e.printStackTrace();
                }
                stmt.close();
                break;
            case 2:
                 System.out.println("请输入路径");

            default:
                System.out.println("服务编号错误");
        }
        }
    }
    static void proof_Manag() throws SQLException
    {
        String query;//查询语句
        int choice=0;//存放用户选项

        Scanner reader=new Scanner(System.in);

        while(true){
        System.out.println("1.删除借阅证 2.增加借阅证 3.借阅证修改 0.退出系统");
        System.out.println("请输入需要的服务编号");
        choice=reader.nextInt();
        switch(choice)
        {
            case 0 :
                return;
            case 1:
                //删除借阅证
                System.out.println("请输入卡号");
                String cno_in=reader.nextLine();
                Statement stmt = conn.createStatement();        
                query="DELETE FROM card WHERE cno=?";
                try{
                    PreparedStatement ps=conn.prepareStatement(query);
                    ps.setString(1,cno_in);
                    ps.executeUpdate();
                }
                catch (SQLException e){
                    e.printStackTrace();
                }
                stmt.close();
                break;
            case 2:
            //增加借阅证
                System.out.println("请输入借阅证信息");
                System.out.println("卡号\t姓名\t单位\t类别");
                Statement stmt2 = conn.createStatement();
                String cno_in2=reader.nextLine();
                String name_in2=reader.nextLine();
                String dept_in2=reader.nextLine();
                String type_in2=reader.nextLine();
                query="INSERT INTO card VALUES(?,?,?)";//cno,name,department
                try{
                    PreparedStatement ps=conn.prepareStatement(query);
                    ps.setString(1,cno_in2);
                    ps.setString(2,name_in2);
                    ps.setString(3,dept_in2);
                    ps.setString(4,type_in2);
                    ps.executeUpdate();
                }
                catch (SQLException e){
                    e.printStackTrace();
                }
                stmt2.close();
                break;
            case 3:
            //修改借阅证
                Statement stmt3 = conn.createStatement();
                System.out.println("请输入借阅证信息");
                System.out.println("卡号\t姓名\t单位\t类别");
                String cno_in3=reader.nextLine();
                String name_in3=reader.nextLine();
                String dept_in3=reader.nextLine();
                String type_in3=reader.nextLine();
                query="UPDATE TABLE card SET name='"+name_in3+
                "',department='"+dept_in3+"',type='"+type_in3+"' WHERE cno="+cno_in3;
                try{
                    PreparedStatement ps=conn.prepareStatement(query);
                    ps.executeUpdate();
                }
                catch (SQLException e){
                    e.printStackTrace();
                }
                stmt3.close();
                break;
            default:
                System.out.println("服务编号错误");
        }   
        }
    }
}

Why does this empty result set appear?

You are using ResultSet without invoking next()

That pattern in your code is wrong:

query="SELECT* FROM book WHERE title='"+bookname+"'";
rset = stmt.executeQuery(query);
int stock=rset.getInt("stock");

You need to execute rset.next() probably as a loop condition and get your field values after the call to next() .

You should address the following things in your code:

  1. Use PreparedStatement instead of Statement in case of parametrized query to avoid SQL injection . Apart from preventing the possibility of SQL injection, it also helps you get rid of using '' explicitly as you have done in query="SELECT bno FROM borrow WHERE cno='"+card_number+"'"; .
query="SELECT bno FROM borrow WHERE cno=?";
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, card_number);
ResultSet rset = pstmt.executeQuery();
While(rset.next()) {
    //...
}
  1. Do not use Class.forName ("com.mysql.jdbc.Driver"); . It is not needed since JDBC 4.0.
  2. Make sure to check rset.next() before trying to access any data from rset as shown in the code above. You have missed this check in the following code:
query="SELECT* FROM book WHERE title='"+bookname+"'";
rset = stmt.executeQuery(query);
int stock=rset.getInt("stock");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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