简体   繁体   中英

How to handle null database using mysql query

This method is working fine. Here I'm fetching max bookid from book table but I'm getting null pointer exception when database is null. So, how to handle this exception.

public void insertData() {        
            Transaction tx = null;
            Session session = getSession();
            try {
                tx = session.beginTransaction();
                Book book = new Book();
                SQLQuery query = session.createSQLQuery("select MAX(bookid) from books");
                List<Book> rows = query.list();
                for (Object row : rows) {
                    book.setBookid(Integer.parseInt(row.toString()));
                    Integer id1 = book.getBookid() + 1;                
                    selbookid = id1;
                }        

                book.setBookname("Java");            
                book.setDateofcreation(new Date());
                book.setLastmodifieddate(new Date());           

                session.save(book);
                tx.commit();
                session.flush();
                session.close();
            } catch (RuntimeException e) {   
                e.printStackTrace();
            } 
        }

When your query returns a single row/column value, use uniqueResult() instead of list().

Hope the exception raises when there are no rows in table. I've made few changes to your code.

Try this

       public void insertData() 
       {        
        Transaction tx = null;
        Session session = getSession();
        int result =0;
        try {
            tx = session.beginTransaction();
            Book book = new Book();
            SQLQuery query = session.createSQLQuery("select MAX(bookid) from books");
           try
           {
            result = (int)query.uniqueResult();   
           }
           catch(Exception e)
           {               
           //do whatever you want with exception
           }
            book.setBookid(result+1);
            book.setBookname("Java");            
            book.setDateofcreation(new Date());
            book.setLastmodifieddate(new Date());           

            session.save(book);
            tx.commit();
            session.flush();
            session.close();
        } 
        catch (RuntimeException e) {   
            e.printStackTrace();
        } 
       }

Your question is not clear but what can I see in your question you are saying if database is null then only you are getting execption.

I check this method in my system and found why you are getting exception because in SQL query you are fetching max value of bookid from books table but if database is null then how will be fetch max value so you need to do some changes in query. You should use IFNULL condition like:

select IFNULL(MAX(bookid),1) from books

It is working for me even database is null because inserting bookid 1 if books table is empty.

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